conditional stm nts
TRANSCRIPT
-
8/12/2019 Conditional Stm Nts
1/13
-
8/12/2019 Conditional Stm Nts
2/13
-
8/12/2019 Conditional Stm Nts
3/13
(also known as block) Group of statements enclosed by braces, namely,
{ and }
The individual statements enclosed by braces are
executed in the order in which they appear
The general form of the compound statement is
{ S1;S2;
.
.
Sn;}
7/25/2014 3
-
8/12/2019 Conditional Stm Nts
4/13
if statement if else statement onditional operators switch statement goto statement
7/25/2014 4
-
8/12/2019 Conditional Stm Nts
5/13
The general form of the if statement asfollows
if( expresson )
statement;Eg: if(a
-
8/12/2019 Conditional Stm Nts
6/13
The statement if(a>b) largest=a; is known as a conditionalstatement
The general conditional statement is
if (logical expression){ S1t;
S2t;..Snt;}
else{ S1e;
S2e;..Sme;}
7/25/2014 6
-
8/12/2019 Conditional Stm Nts
7/13
In the above general form if the logicalexpression true then the compoundstatement {S1t;S2t;;Snt;} is executed andcontrol jumps to the next statement following
the if statement. Thus the compoundstatement following else is ignored
If the logical expression is false then thecompound statement appearing first isignored and the compound statement{S1e;S2e;;Sme;} following elseis executed
7/25/2014 7
-
8/12/2019 Conditional Stm Nts
8/13
if (logical expression){ S1t;
S2t:
Snt;}In the above statement if the logical expression is
true then the compound statement{S1t;S2t:;Snt;} is executed.
If the logical expression is false then the controljumps to the statement following the right bracesignoring the compound statement
7/25/2014 8
-
8/12/2019 Conditional Stm Nts
9/13
-
8/12/2019 Conditional Stm Nts
10/13
#includemain(){int i;printf(enter either 1 or 2);scanf(%d,&i);
If(i==1)printf(success);else{if(i==2)printf(failure);elseprintf(sorry it is a wrong number);}}
7/25/2014 10
-
8/12/2019 Conditional Stm Nts
11/13
To pick the largest of 3 numbers
7/25/2014 11
-
8/12/2019 Conditional Stm Nts
12/13
#includemain(){int a,b,c;
scanf(%d %d %d,&a, &b, &c);printf(a=%d, b=%d, c=%d\n, a, b, c);if(a>b){if(a>c)printf(a=%d\n, a);elseprintf(c=%d\n, c);}else{
if(b>c)printf(b=%d\n, b);elseprintf(c=%d\n, c);}
} 7/25/2014 12
-
8/12/2019 Conditional Stm Nts
13/13
#includemain(){int a,b,c, big;scanf(%d %d %d,&a, &b, &c);
printf(a=%d, b=%d, c=%d\n, a, b, c);big=a;if(b>big)big=b;if(c>big)big=c;print(the largest number is %d, big);}
7/25/2014 13