1 of 19

οΏ½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

2 of 19

Decision Making Statements

  • It allows to decide the order/flow of execution of specific statements in the program

1) if statement

2) switch statement

3) conditional operator statement

4) goto statement

S. Sridevy

5/30/2022

3 of 19

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

4 of 19

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 .

5 of 19

Simple if statement - Example

  • Maximum of two numbers

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

6 of 19

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

7 of 19

If Else - Flow chart

S. Sridevy

5/30/2022

8 of 19

if else statement - Example

  • Maximum of two numbers

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

9 of 19

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.

10 of 19

Nested If - Flow chart

S. Sridevy

5/30/2022

11 of 19

if else statement - Example

  • Maximum of three numbers

if (a > b)

{

if (a > c)

{

printf (β€œA is biggest = %d”, a);

}

else

{

printf (β€œC is biggest = %d”,c);

}

}

S. Sridevy

12 of 19

if else statement - Example

else

{

if (b > c )

{

printf (β€œB is biggest = %d”,b);

}

else

{

printf (β€œC is biggest = %d”,c);

}

}

13 of 19

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

14 of 19

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;

15 of 19

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.

16 of 19

switch statement

S. Sridevy

17 of 19

Goto Statement

  • It is unconditional control statement.

S. Sridevy

5/30/2022

18 of 19

/* 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

19 of 19

/* 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