3. INTRODUCTION
‘C’ language processes decision making capabilities supports the flowing
statements known as control or decision making statements
There are 4 Types:
●If statement
●switch statement
●conditional operator statement
●goto statement
3
4. If statement
The if statement is powerful decision making
statement and is used to control the flow of execution
of statements.
if statement is the most simple decision-making
statement. It is used to decide whether a certain
statement or block of statements will be
executed or not.
5. “
5
Types of if Statement
(a) Simple if statement
(b) If else statement
(d) Else –If ladder
5
(c) Nesting of If-else statement
6. Simple if Statement
If the test expression is true then a true block statement are executed,
otherwise the false – block statement are executed. In both cases
either true-block or false-block will be executed not both.
Syntax:
● if (test expression)
● {
statement block;
● }
statement-x ;
8. If the student belongs to the
sports category then additional bonus
marks are added to his marks before
they are printed. For others bonus
marks are not added .
8
Simple If Statement
Example
if (category = sports)
{
marks = marks + bonus marks;
}
printf(“%d”,marks);
9. If Else Statement
If the test expression is true then a true block statement are executed, otherwise the false block
statement are executed. In both cases either true-block or false-block will be executed not both.
Syntax:
if (test expression)
{
true-block statements;
}
else
{
false-block statements;
}
statement – x;
11. Here if the code is equal to ‘1’ the
statement boy=boy+1; Is executed and the control is
transferred to the statement st-n, after skipping the
else part. If code is not equal to ‘1’ the statement boy
=boy+1; is skipped and the statement in the else
part girl =girl+1; is executed before the control
reaches the statement st-n.
11
If else Statement
Example
If (code == 1)
boy = boy + 1;
else
girl = girl + 1;
st-x;
12. Nesting of if else statement
When a series of decisions are involved we may have to use more than one
if-else statement in nested form of follows.
Syntax: if ( Test Condition1) {
if ( Test Condition2) {
Statement -1;
}
else {
Statement -2; }
else {
Statement -3;}
}
15. Else-If ladder
A multi path decision is charm of its in which the statement associated with
each else is an If. It takes the following general form.
This construct is known as the wise-If ladder. The conditions are evaluated
from the top of the ladder to down wards. As soon as a true condition is
found the statement associated with it is executed and the control the is
transferred to the st-X (i.e.., skipping the rest of the ladder). when all the n-
conditions become false then the final else containing the default – st will be
executed.
16. 16
Else-If ladder
SYNTAX
if (Test Condition -1) {
Statement -1; }
else if ( Test Condition -2) {
Statement -2;}
else if ( Test Condition -3) {
Statement -3; }
else if ( Test Condition –n) {
Statement –n;
}else {
default statement; }
Statements-X;
18. “
18
18
The above construction is known as else if ladders.
The conditions are evaluated from top to bottom.
As soon as a true condition is found, the statement associated
with it is executed and
The control is transferred to the Rest of the Program
Statement–X (skipping rest of the ladder).
When all the “n” conditions become false, then the final else
containing the default statement will be executed.
Ex : If (code = = 1) Color = “red”;
Else if ( code = = 2) Color = “green”
Else if (code = = 3) Color = “white”;
Else Color = “yellow”;
If code number is other than 1,2 and 3 then color is yellow.
Else-If ladder