Upload
-
View
769
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
IF Statement
Conditional Structure (1)
Type 1 Type 2
2
Cond T
F
D
C
B
A
E
if( Cond ){
A ;B ;
}else{
C ;D ;
}
E ;
Cond T
F
D
C
B
A
if( Cond ){
A ;B ;
}C ;D ;
Conditional Structure (2)
if statement
3
if( Cond ){
A ;B ;
}else{
C ;D ;
}
E ;
If cond is true,Statements are executed
If cond is false,Statements are executed
Statements are executedafter if statement
Cond T
F
D
C
B
A
E
Conditional Structure (3)
Example
4
Cond1 T
F
B C
D
A
Cond2
E
if(Cond1 ){
A;}else{
if( Cond2 ){
C ;}else{
B ;}D ;
}
E ;
TF
Example 2
Read a number and print “Yes” if it is 2 or “No”
5
Start
x
x
“Yes”
Stop
x == 2
“No”
T F
#include <stdio.h>
main() {
int x ;
scanf( “%d”, &x ) ;
if( x == 2 ){
printf( “Yes\n” ) ;}else{
printf( “No\n” ) ;}
return ;}
Example 3
Find the maximum among 3 numbers
6
Start
Var a, b, c
Stop
a > b
a
T F
a > c
Input a, b, c
b > c
c b c
#include <stdio.h>
main() {
int a, b, c ;
scanf( %d%d%d”, &a, &b, &c ) ;
if(a > b ) {
if(a > c ) {printf( “%d\n”, a ) ;
} else {printf( “%d\n”, c ) ;
}}else{
if( b > c ) {printf( “%d\n”, b ) ;
} else {printf( “%d\n”, c ) ;
}}
return ;}
Example 4
Sort 3 numbers
7
Start
a, b, c
Stop
a > b
a, b, c
T F
b > c
a, b, c
a > c
a, c, b b, a, c b, c, a
a > c b > c
c, a, b c, b, a
T F T F
T F T F
#include <stdio.h>
main() {
int a, b, c ;
scanf( %d%d%d”, &a, &b, &c ) ;
if( a > b ) {
if( b > c ) {printf( “%d %d %d\n”, a, b, c ) ;
} else {if( a > c ) {
printf( “%d %d %d\n”, a, c, b ) ;} else {
printf( “%d %d %d\n”, c, a, b ) ;}
}}else{
if( a > c ) {printf( “%d %d %d\n”, b, a, c ) ;
} else {if( b > c ) {
printf( “%d %d %d\n”, b, c, a ) ;} else {
printf( “%d %d %d\n”, c, b, a ) ;}
}}
return ;}
8
if and if-else Statements Nested if statement if ( Condition1 ) {
A;}else {
if( Condition2 ) {B;
}else {
if( Condition3 ) {C;
}else {
D;}
}}E;
Condition1
Condition2
Condition3
D
E
A
B
C
T
FT
FT
F
9
if and if-else Statements Nested if statement
if ( Condition1 ) {A;
}else if( Condition2 ) {
B;}else if( Condition3 ) {
C;}else {
D;}E;
Condition1
Condition2
Condition3
D
E
A
B
C
T
FT
FT
F
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( “Number\n” ) ;
else if ( ‘A’ <= c && c <= ‘Z’) printf( “Upper case\n” ) ;
else if (‘a’ <= c && c <= ‘z’) printf( “Low case\n” ) ;
else printf( “Outside of that\n” ) ;
switch Statement
Special case of nested if statement
11
var==value1
var==value2
var==value3
D
E
A
B
C
T
FT
FT
F
switch( variable ) {case value1 : A ;
break ;case value2 : B ;
break ;case value3 : C ;
break ;default :
D ;}
switch statement
Change swtich to nested if statements
12
switch( variable ) {case value1 : A ;
break ;case value2 : B ;
break ;case value3 : C ;
break ;default : D ;}
if( var==value1 ) {A ;
}else if(var==value2 ) {
B ;}else if(var==value3 ) {
C ;}else {
D ;}
switch statement
Role of break in switch statement– When grade == 4
13
switch (grade){case 4 : printf(“A”) ;
break;case 3 : printf(“B”) ;
break;case 2 : printf(“C”) ;
break;case 1 : printf(“D”) ;
break;default : printf(“Illegal grade”);}
switch (grade){case 4 : printf(“A”) ;case 3 : printf(“B”) ;case 2 : printf(“C”) ;case 1 : printf(“D”) ;default : printf(“Illegal grade”);}
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”
14
switch (grade) {case ‘A’ : case ‘B’ : case ‘C’ :case ‘D’ : printf(“pass\n”); break; case ‘F’ : pritnf(“fail\n”); break;default : printf(“error\n”); break;
}
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( “pass\n” ) ;
else if( grade == ‘F’ )printf( “fail\n” ) ;
elseprintf( “error\n” ) ;
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.
17
Short-Circuit Example of Short-Circuit Evaluation
int i=2, j=3;if( (i == 2) && ( j++ == 3) )
printf( “True\n” ) ;else
printf( “False\n” ) ;printf(“%d %d\n”, i, j);
int i=2, j=3;if( (i == 2) || (j++ == 3) )
printf( “True\n” ) ;else
printf( “False\n” ) ;printf(“%d %d\n”, i, j);
int i=2, j=3;if( (i == 3) && ( j++ == 3) )
printf( “True\n” ) ;else
printf( “False\n” ) ;printf(“%d %d\n”, i, j);
int i=2, j=3;if( (i == 3) || (j++ == 3) )
printf( “True\n” ) ;else
printf( “False\n” ) ;printf(“%d %d\n”, i, j);
18
Conditional Operator Conditional Operator Syntax
– 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
expr1 ? expr2 : expr3
if ( y < z)x = y;
elsex = z
x = ( y < z ) ? y : z
19
Conditional Operator
Exampleint a = 5, b = 6 ;
int max = (a < b) ? b : a ;int min = (a > b) ? b : a ;