The document discusses various conditional structures in C programming like if, if-else, nested if, switch statements and conditional operators. It provides examples of using these conditional statements to check conditions, compare values and execute code blocks conditionally. It also explains concepts like short-circuit evaluation and the role of break in switch statements.
2. Conditional Structure (1)
Type 1 Type 2
if( Cond ) if( Cond )
Cond T T {
{ Cond
A; A;
F A F
B; B;
} C A }
C; else
B
D; {
D B C;
D;
C }
E
E;
D
2
3. Conditional Structure (2)
if statement
if( Cond ) T
Cond
{ If cond is true,
A; F
Statements are executed C A
B;
}
D B
else
{ If cond is false,
C; Statements are executed
E
D;
}
E; Statements are executed
after if statement
3
4. Conditional Structure (3)
Example if(Cond1 )
{
A;
Cond1 T
}
F else
A
{
Cond2 if( Cond2 )
F T {
C;
B C }
else
{
B;
D }
D;
}
E
E;
4
5. Example 2
Read a number and print “Yes” if it is 2 or “No”
#include <stdio.h>
Start main() {
int x ;
x scanf( “%d”, &x ) ;
if( x == 2 )
x {
printf( “Yesn” ) ;
}
x == 2 else
T F {
printf( “Non” ) ;
“Yes” “No” }
return ;
}
Stop
5
6. Example 3
Find the maximum among 3 numbers
#include <stdio.h>
Start
main() {
int a, b, c ;
Var a, b, c
scanf( %d%d%d”, &a, &b, &c ) ;
Input a, b, c if(a > b )
{
if(a > c ) {
printf( “%dn”, a);
a >b } else {
T F printf( “%dn”, c);
}
a >c b >c }
else
{
a c b c if( b > c ) {
printf( “%dn”, b);
} else {
printf( “%dn”, c);
Stop }
}
return ;
} 6
7. #include <stdio.h>
main() {
Example 4 int a, b, c ;
scanf( %d%d%d”, &a, &b, &c ) ;
Sort 3 numbers
if( a > b )
Start {
if( b > c ) {
printf( “%d %d %dn”, a, b, c ) ;
a, b, c } else {
if( a > c ) {
printf( “%d %d %dn”, a, c, b );
a, b, c } else {
printf( “%d %d %dn”, c, a, b );
}
}
a >b
T F }
else
b >c a >c {
T F T F
if( a > c ) {
printf( “%d %d %dn”, b, a, c ) ;
a >c b >c } else {
T F T F if( b > c ) {
printf( “%d %d %dn”, b, c, a );
a, b, c a, c, b c, a, b b, a, c b, c, a c, b, a } else {
printf( “%d %d %dn”, c, b, a );
}
}
}
Stop
return ;
} 7
8. if and if-else Statements
Nested if statement if ( Condition1 ) {
A;
T }
Condition1 A else {
F if( Condition2 ) {
T B B;
Condition2
}
F
T
else {
Condition3 C
if( Condition3 ) {
F C;
D }
else {
D;
E
}
}
}
E;
8
9. if and if-else Statements
Nested if statement
if ( Condition1 ) {
T
A;
Condition1 A }
F else if( Condition2 ) {
T B B;
Condition2
}
F
T
else if( Condition3 ) {
Condition3 C
C;
F }
D else {
D;
}
E
E;
9
10. if and if-else Statements
Nested if statement
– Identify whether a character is number, capital letter or
small letter
scanf( “%c”, &ch ) ;
if (‘0’ <= c && c <= ‘9’)
printf( “Numbern” ) ;
else if ( ‘A’ <= c && c <= ‘Z’)
printf( “Upper casen” ) ;
else if (‘a’ <= c && c <= ‘z’)
printf( “Low casen” ) ;
else
printf( “Outside of thatn” ) ;
10
11. switch Statement
Special case of nested if statement
T switch( variable ) {
var==value1 A
case value1 : A ;
F
T break ;
var==value2 B
case value2 : B ;
F
break ;
T
var==value3 C
case value3 : C ;
F break ;
D
default :
D ;
E }
11
12. switch statement
Change swtich to nested if statements
switch( variable ) { if( var==value1 ) {
case value1 : A ; A ;
break ; }
case value2 : B ; else if(var==value2 ) {
break ; B ;
case value3 : C ; }
break ; else if(var==value3 ) {
default : D ; C ;
} }
else {
D ;
}
12
13. switch statement
Role of break in switch statement
– When grade == 4
switch (grade) switch (grade)
{ {
case 4 : printf(“A”) ; case 4 : printf(“A”) ;
break; case 3 : printf(“B”) ;
case 3 : printf(“B”) ; case 2 : printf(“C”) ;
break; case 1 : printf(“D”) ;
case 2 : printf(“C”) ; default : printf(“Illegal grade”);
break; }
case 1 : printf(“D”) ;
break;
default : printf(“Illegal grade”);
}
13
14. switch statement
Role of break in switch statement
– If grade is ‘A’, ‘B’, ‘C’ or ‘D’ then “pass”
– If grade is ‘F’ then “fail”, or “error”
switch (grade) {
case ‘A’ :
case ‘B’ :
case ‘C’ :
case ‘D’ : printf(“passn”); break;
case ‘F’ : pritnf(“failn”); break;
default : printf(“errorn”); break;
}
14
15. switch statement
Role of break in switch statement
– Change previous example to if-else statements
if ( grade == ‘A’ || grade == ‘B’ || grade == ‘C’ || grade == ‘D’ )
printf( “passn” ) ;
else if( grade == ‘F’ )
printf( “failn” ) ;
else
printf( “errorn” ) ;
15
16. Short-Circuit
Short-Circuit Evaluation
– Boolean operators(&&, ||, etc.) in which the second
argument is only executed or evaluated if the first argument
does not suffice to determine the value of the expression.
expr1 && expr2
– If expr1 is False, then expr2 is not executed.
expr1 || expr2
– If expr1 if True, then expr2 is not executed.
16
17. Short-Circuit
Example of Short-Circuit Evaluation
int i=2, j=3; int i=2, j=3;
if( (i == 2) && ( j++ == 3) ) if( (i == 2) || (j++ == 3) )
printf( “Truen” ) ; printf( “Truen” ) ;
else else
printf( “Falsen” ) ; printf( “Falsen” ) ;
printf(“%d %dn”, i, j); printf(“%d %dn”, i, j);
int i=2, j=3; int i=2, j=3;
if( (i == 3) && ( j++ == 3) ) if( (i == 3) || (j++ == 3) )
printf( “Truen” ) ; printf( “Truen” ) ;
else else
printf( “Falsen” ) ; printf( “Falsen” ) ;
printf(“%d %dn”, i, j); printf(“%d %dn”, i, j);
17
18. Conditional Operator
Conditional Operator Syntax
expr1 ? expr2 : expr3
– Conditional operator ? : is ternary operator
– Evaluate expr1 first,
If it is true then expr2 is executed.
Or if it is false then expr3 is executed.
if-else statement Conditional Operator
if ( y < z) x=(y<z)?y:z
x = y;
else
x=z
18