C supports Five types of Conditional Control Statements. These are
1) simple if statement
2) if-else statement
3) Nested if statement
4) else if statement
5) switch statement
1. simple if statement:
It is a conditional controlled statement and is used to control the flow of execution.
Syntax:
if(expression)
{
statements;
}
In this statement, first the expression will be evaluated. If it is true then the statement block will be executed and the control transfer to the next statement. Otherwise if the expression is false, then the control directly goes to the next statement.
2. if-else statement:
The if-else statement is an extension statement of simple if statement.
Syntax:
if(expression)
{
statements-1;
}
else
{
statements-2;
}
In this statement, first the expression will be evaluated, and if it is true then the if block statements(statements-1) are executed and the else block statements(statements-2) are ignored. Otherwise if the expression will is false, then else statements are executed and if block statements are ignored.
3. Nested if statements:
Using a if statement within another if is called as nested if. If a series of decisions are involved, we use nested if statement.
Syntax:
Form-1:
if(expression-1)
{
if(expression-2)
{
.......................
if(expression-n)
{
statements;
}
.......................
}
}
Form-2:
if(expression-1)
{
if(expression-2)
{
Statement-1;
}
else
{
Statements-2;
}
}
else
{
if(expression-3)
{
Statement-3;
}
else
{
Statements-4;
}
}
4. else-if ladder:
This statement is also used for a series of decisions are involved.
Syntax:
if(expression-2)
{
Statement-1;
}
else if(expression-2)
{
Statements-2;
}
....................
else if(expression-n)
{
Statements-n;
}
else
{
else block statements;
}
In this statement, the expressions are evaluated from top to bottom, if the condition is true then the statements associated that block is executed and the control transfers to the next statement. Otherwise when all expressions are false then the final else block statements will be executed.
5. Switch statement:
It is a multiway conditional control statement used in C Language. It is mainly used in situations where there is need to pick one alternative among many alternatives.
Syntax:
switch(variable/expression)
{
case value-1:
statements-1;
break;
case value-2:
statements-2;
break;
......................
......................
case value-n:
statements-n;
break;
default:
default statements;
}
The switch statement tests the value of given variable or expression against a list of case values. when a match is found, the statement associated to that case is executed and the control transfer to the next statement, otherwise the default statements will be executed.
Next Topic : Unconditional Control statements
Previous Topic : Control Statements (or) Control Structures
No comments:
Post a Comment