Conditional Statements Set A

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Conditional

Statements
C# uses Comparison and Logical Operators for the conditional
statements:
Comparison Operators
Operator​ Name​ Example​

==​ Equal to​ x == y​

!=​ Not equal​ x != y​

>​ Greater than​ x > y​

<​ Less than​ x < y​

>=​ Greater than or equal to​ x >= y​

<=​ Less than or equal to​ x <= y​


Logical Operators
Operator​ Name​ Description​ Example​

&& ​ Logical and​ Returns true if both statements x < 5 && x < 10​
are true​

|| ​ Logical or​ Returns true if one of x < 5 || x < 4​


the statements is true​

!​ Logical not​ Reverse the result, returns false !(x < 5 && x < 10)​
if the result is true​
You can use these conditions to perform different actions for
different decisions.​

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 condition is false​.
• Use else if to specify a new condition to test if the first condition is false​.
• Nested if statements - You can use one if or else if statement inside another if
or else if statement(s).
• Use switch to specify many alternative blocks of code to be executed​.
• Nested switch statements - You can use one switch statement inside another
switch statement(s).
IF Statement
SYNTAX:
if (condition) {
// block of code to be executed if the
condition is true
}

EXAMPLE:
if (20 > 18) {
Console.WriteLine(“20 is greater than 18”);
}
----------------------------------
int x = 20;
int y = 18;
if (x > y) {
Console.WriteLine(“x is greater than y”);
}
ELSE Statement
SYNTAX:
if (condition) {
// block of code to be executed if the
condition is true
} else {
// block of code to be executed if the
condition is false
}
EXAMPLE:
int x = 20, y = 10;
if (x > y) {
Console.WriteLine(“x is greater than y”);
} else {
Console.WriteLine(“x is less than y”);
}
ELSE IF Statement
SYNTAX:
if (condition1) {
// block of code to be executed if condition1 is true
}
else if (condition2) {
// block of code to be executed if the condition1 is false and condition2
is true
}
else {
// block of code to be executed if the condition1 is false and
condition2 is false
}
EXAMPLE:
int time = 22;
if (time < 10) {
Console.WriteLine(“Less than 10”);
} else if (time < 20) {
Console.WriteLine(“Less than 20”);
} else {
Console.WriteLine(“Greater than 20”);
}
SWITCH Statement
Use switch statement to select one
of many code blocks to be executed.

SYNTAX:
switch(expression) {
case <value1>:
// code block
break;
case <value2>:
// code block
break;
default:
// code block
}
This is how it works:
• The switch expression is evaluated once
• The value of the expression is compared with the values of each case
• If there is a match, the associated block of code is executed

When C# reaches a break keyword, it breaks out of the switch block. This
will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it’s time for a break. There is
no need for more testing.
EXAMPLE:
int day = 5;
switch (day) {
case 1:
Console.WriteLine(“Monday”);
break;
case 2:
Console.WriteLine(“Tuesday”);
break;
case 3:
Console.WriteLine(“Wednesday”);
break;
case 4:
Console.WriteLine(“Thursday”);
break;
case 5:
Console.WriteLine(“Friday”);
break;
case 6:
Console.WriteLine(“Saturday”);
break;
case 7:
Console.WriteLine(“Sunday”);
break;
}
The default keyword specifies some code to run if there is no case match.
EXAMPLE:
int day = 5;
switch (day) {
case 6:
Console.WriteLine(“Saturday”);
break;
case 7:
Console.WriteLine(“Sunday”);
break;
default:
Console.WriteLine(“Not found”);
break;
}
Rules for Applying Switch Statements
1. Switch case allows only integer and character constants in case
expression.
2. You can use any number of case statements within a switch statement.
3. Value for a case must be the same as the variable in a switch statement.

Limitations of Switch Statement


Logical / Comparison operators cannot be used with a switch statement
(case k >= 20).
Nested if – else statement
It is always legal in C# to nest if-else statements, which means you can use one if or
else if statement inside another if or else if statement(s).

SYNTAX
if(condition1) {
// block of code to be executed if condition1 is true
if(condition2) {
// block of code to be executed if condition2 is true
}
}

You can nest else if...else in the similar way as you have nested if statement.
Example

int a = 100;
int b = 200;

if (a == 100) {
if (b == 200) {
Console.WriteLine("Value of a is 100 and b is 200");
}
}
Console.WriteLine("Exact value of a is " + a);
Console.WriteLine("Exact value of b is " + b);
Nested switch statement
SYNTAX
switch(expression) {
case <value>:
switch(expression) {
case <value>:
// code block
break;
case <value>:
// code block
break;
}
break;
case <value>:
// code block
Break;
}
Example
int a = 100, b = 200;
switch (a) {
case 100:
Console.WriteLine("This is part of outer switch ");
switch (b) {
case 200:
Console.WriteLine("This is part of inner switch ");
break;
}
break;
}
Console.WriteLine("Exact value of a is " + a);
Console.WriteLine("Exact value of b is " + b);
Console.ReadLine();

You might also like