Conditional Statements
Conditional Statements
1. If statement
2. If – else statement
3. Nested if statement
4. Switch case statement
If statement
• If statement is the simplest conditional statement.
• If enables us to check conditional is true or false.
• When the condition is true it will execute the statement
and when false it will not execute the statement.
• If syntax :
If (condition if true)
{
execute the statement
}
If else statement
• More complex and useful condition statement.
• Execute the one branch if the condition is true , and another if it is
false.
• The simplest form of the if else statement:
• If (expression)
{
statement 1;
}
Else
{
statement 2;
}
• The condition evaluated
if it is true ,the first statement is executed
if it is false, the second statement is executed
Nested if else
• Nested if else Statement in C# – Nested if else statement in C# means, if else
statement inside another if else statement.
• Syntax:
if (expression)
{
if (expression)
{
statement;
}
else
{
statement;
}
} else
statement;
Switch statement
• The switch statement is used when we have
multiple condition but only one condition true
and only that code will be executed.
In other word we can say execute one of
multiple blocks of code to be executed.
• The switch statement works same as if-else if-
else statement.
• switch (a)
{
case statement1:
if a=statement1 then code to be executed;
break;
case statement2:
if a=statement2 then code to be executed;
break;
case statement3:
if a=statement3 then code to be executed;
break;
…….
default:
if “a” is not matches with any statement then code to be executed.
}
String