Decision Making Statements
Decision Making Statements
The if statement may be implemented in different forms depending on the complexity of conditions to
be tested. The different forms are,
1. Simple if statement
2. if....else statement
3. Nested if....else statement
4. switch statement
1. Simple if statement
if(expression)
{
statement inside;
}
statement outside;
If the expression returns true, then the statement-inside will be executed, otherwise statement-inside is
skipped and only the statement-outside is executed
2. if...else statement
if(expression)
{
statement block1;
}
else
{
statement block2;
}
If the expression is true, the statement-block1 is executed, else statement-block1 is skipped
and statement-block2 is executed.
if( expression )
{
if( expression1 )
{
statement block1;
}
else
{
statement block2;
}
}
else
{
statement block3;
}
if expression is false then statement-block3 will be executed, otherwise the execution continues and
enters inside the first if to perform the check for the next if block, where if expression 1 is true
the statement-block1 is executed otherwise statement-block2 is executed.
4. Switch statement
Switch statement is a control statement that allows us to choose only one choice among the many given
choices. The expression in switch evaluates to return an integral value, which is then compared to the
values present in different cases. It executes that block of code which matches the case value. If there is
no match, then default block is executed(if present). The general form of switch statement is,
switch(expression)
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
case value-3:
block-3;
break;
case value-4:
block-4;
break;
default:
default-block;
break;
}
The expression (after switch keyword) must yield an integer value i.e the expression should be an
integer or a variable or an expression that evaluates to an integer.
The next line, after the case statement, can be any valid C statement.