Conditional Statements Set A
Conditional Statements Set A
Conditional Statements Set A
Statements
C# uses Comparison and Logical Operators for the conditional
statements:
Comparison Operators
Operator Name Example
&& Logical and Returns true if both statements x < 5 && x < 10
are 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.
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.
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();