0% found this document useful (0 votes)
21 views20 pages

Decision Making

The document provides an overview of decision-making statements in C#, including simple if statements, else statements, else if statements, and switch statements. It explains the syntax and provides examples for each type of conditional statement, including nested if statements and the shorthand ternary operator. Additionally, it includes a flowchart and code examples to illustrate the concepts discussed.

Uploaded by

sohanxt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views20 pages

Decision Making

The document provides an overview of decision-making statements in C#, including simple if statements, else statements, else if statements, and switch statements. It explains the syntax and provides examples for each type of conditional statement, including nested if statements and the shorthand ternary operator. Additionally, it includes a flowchart and code examples to illustrate the concepts discussed.

Uploaded by

sohanxt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Decision Making

Sumaiya Tanjil Khan


Lecturer
Department of CSE
Uttara University
Simple if Statement

🞆 In a simple ‘if’ statement, a condition is tested


🞆 If the condition is true, a set of statements are executed
🞆 If the condition is false, the statements are not executed
and the program control goes to the next statement that
immediately follows if block
C# has the following conditional statements:
● Use if to specify a block of code to be executed, if a specified condition is true
● Use else to specify a block of code to be executed, if the same condition is false
● Use else if to specify a new condition to test, if the first condition is false
● Use switch to specify many alternative blocks of code to be executed
Syntax

The syntax of the if statement in C# programming is:

if (condition)
{
// statements to be executed if the test expression is true
}
Flow Chart
Example
else Statement

🞂 In simple ‘if’ statement, when the condition is true, a set


of statements are executed. But when it is false, there is no
alternate set of statements
🞂 The statement ‘else’ provides the same
Syntax

if (condition)
{
Statement-1;
}
else
{
Statement-2;
}
Example
Example
int time = 20;
if (time < 18)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");

}
else if Statement

The else..if statement is useful when you need to check multiple


conditions within the program, nesting of if-else blocks can be
avoided using else..if statement.
Syntax
if (condition-1)
{
Statement-1;
}
else if (condition-2)
{
Statement-2;
}
………………………………
else if (condition-n)
{
Statement-n;
}
else
{
Statement set-x;
Example
int time = 22;
if (time < 10)
{
Console.WriteLine("Good morning.");
}
else if (time < 20)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");

}
Nested if

• When an if else statement is present inside the


body of another “if” or “else” then this is
called nested if else.
Syntax of Nested if else statement

if (condition)
{
if (condition 1)
{
Statement-1;
}
else
{
Statement -2;
}
}
else
{
if (condition 3)
{
Statement-3;
}
else
{
Statement -4;
}
}
using System;
namespace DecisionMaking {
class Program {
static void Main(string[] args) {
//* local variable definition */
int a = 100;
int b = 200;

/* check the boolean condition */


if (a == 100) {

/* if condition is true then check the following */


if (b == 200) {
/* if condition is true then print the following */
Console.WriteLine("Value of a is 100 and b is 200");
}
}
Console.WriteLine("Exact value of a is : {0}", a);
Console.WriteLine("Exact value of b is : {0}", b);
Console.ReadLine();
Shorthand If...Else (Ternary Operator)

There is also a shorthand if else, which is known as the ternary operator


because it consists of three operands. It can be used to replace multiple lines
of code with a single line. It is often used to replace simple if else statements:
variable = (condition) ? expressionTrue : expressionFalse;
Example:
int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
Console.WriteLine(result);
C# Switch Statements
Use the switch statement to select one of many code blocks to be executed.
Syntax:
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
break;

}
int day = 4;
switch (day)
{
case 1: case 5:
Console.WriteLine("Monday"); Console.WriteLine("Friday");
break; break;
case 2: case 6:
Console.WriteLine("Tuesday"); Console.WriteLine("Saturday");
break; break;
case 3: case 7:
Console.WriteLine("Wednesday"); Console.WriteLine("Sunday");
break; break;
case 4:
Console.WriteLine("Thursday"); }
break;
Thank You

You might also like