οΏ½Dr. S. Sridevy
Associate Professor (CS)
Dr. M. Ramanan
Teaching Assistant (CS)
Dept. of PS&IT, AEC&RI, TNAU, Coimbatore -3.
Decision Making/Control Statements - BranchingΒ If - If... Else β Nesting ifβ¦ Else - Else .. If ladder - Switch - Goto
COM 102 Theory Schedule 6
Decision Making Statements
1) if statement
2) switch statement
3) conditional operator statement
4) goto statement
S. Sridevy
5/30/2022
if Statement
The βifβ statement may be implemented in different forms depending on the complexity of conditions to be tested.
a) Simple if statement
b) if β¦ else statement
c) Nested ifβ¦. else statement
d) else if ladder
S. Sridevy
5/30/2022
if Statement - Simple if statement
Syntax :
if (condition)
{
Statement 1;
-----
Statement n;
}
Statement x;
S. Sridevy
5/30/2022
Meaning :
If <condition> is non-zero (true), <statement1 to statement n > are executed.
If < condition > is false <statement x > is executed .
Simple if statement - Example
printf (β\n Enter two numbers β);
scanf (β%d%dβ,&a,&b);
if (a > b)
{ printf (βA is biggest = %dβ, a);
}
if (b > a)
{ printf (βB is biggest = %dβ,b);
}
S. Sridevy
5/30/2022
If β¦ else statement
Syntax :
if (condition)
{
True block Statement (s);
}
else
{
False block statement (s);
}
Statement x;
S. Sridevy
5/30/2022
Meaning :
If <condition> is non-zero (true), < True block statement (s) > is executed.
If < condition> is false < False block statement (s) > is executed
If Else - Flow chart
S. Sridevy
5/30/2022
if else statement - Example
printf (β\n Enter two numbers β);
scanf (β%d%dβ,&a,&b);
if (a > b)
{ printf (βA is biggest = %dβ, a);
}
else
{ printf (βB is biggest = %dβ,b);
}
S. Sridevy
5/30/2022
Nested If β¦ else statement
Syntax :
if (condition 1)
{
if ( condition 2)
{ Statement 1;
}
else
{Statement 2;
}
}
else
{ Statement 3;
}
Statement x;
S. Sridevy
Meaning :
If the condition 1 is false, the statement 3 will be executed.
If the condition 1 is true, then control goes to test the condition 2, If the condition 2 is true , the statement 1 will be executed otherwise the statement 2 will be executed and then the control is transferred to the statement x.
Nested If - Flow chart
S. Sridevy
5/30/2022
if else statement - Example
if (a > b)
{
if (a > c)
{
printf (βA is biggest = %dβ, a);
}
else
{
printf (βC is biggest = %dβ,c);
}
}
S. Sridevy
if else statement - Example
else
{
if (b > c )
{
printf (βB is biggest = %dβ,b);
}
else
{
printf (βC is biggest = %dβ,c);
}
}
else If β¦ ladder statements
Syntax :
if (condition 1)
Statement 1;
else if (condition 2)
Statement 2;
--------
else if (condition n-1)
Statement n β 1;
else
Statement n;
Statement x;
S. Sridevy
else If β¦ ladder Example
Sales Commission calculation
S. Sridevy
Sales amount Commission
<= 10000 5 % of sales
> 10000 and <= 30000 10 % of sales
> 30000 and <= 50000 15 % of sales
> 50000 20 % of sales
if (samt <= 10000)
com = samt * 5 /100;
else if (samt > 10000 && samt <= 30000)
com = samt * 10 /100;
else if (samt > 30000 && samt <= 50000)
com = samt * 15 /100;
else
com = samt * 20 /100;
switch statement
switch (expression)
{
case value-1 :
block 1;
break;
case value-2 :
block 2;
break;
----------
default :
default block ;
break;
}
Statement x;
S. Sridevy
Meaning :
The switch statement tests the value of given variable (or expression) against a list of case values and when a match is found, a block of statement associated with the case is executed.
switch statement
S. Sridevy
Goto Statement
S. Sridevy
5/30/2022
/* print your name up to 5 times using Backward goto jump */οΏ½
void main( )
{
int i = 1 ;
fot:
printf(β\n FOT 2022 Batchβ);
i = i + 1;
if (i <= 5)
goto fot;
}
S. Sridevy
5/30/2022
/* print your name up to 5 times using Forward goto jump */οΏ½
void main( )
{
int i = 1 ;
x:
if (i > 5)
goto fot:
printf(β\n FOT 2022 Batchβ);
i = i + 1;
goto x;
fot:
}
S. Sridevy
5/30/2022