0% found this document useful (0 votes)
6 views13 pages

Lect 10

The document discusses selection statements in C programming, including if-else statements, conditional operators, and switch-case statements. It provides examples of using if-else to compare dates and perform arithmetic operations. It also discusses solving dangling else issues and using macros with conditional operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Lect 10

The document discusses selection statements in C programming, including if-else statements, conditional operators, and switch-case statements. It provides examples of using if-else to compare dates and perform arithmetic operations. It also discusses solving dangling else issues and using macros with conditional operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

C Programming

Selection of alternatives
If else statement

Examples Using if
Example 21 (contd)

#i n c l u d e < s t d i o . h>
i n t main ( ) {
i n t y1 , m1 , d1 , y2 , m2 , d2 , same , earlier ;

p r i n t f ( ” E n t e r f i r s t d a t e (mm/ dd / yy ) : ” ) ;
s c a n f ( ”%d/%d/%d” , &m1 , &d1 , &y1 ) ;
p r i n t f ( ” E n t e r s e c o n d d a t e (mm/ dd / yy ) : ” ) ;
s c a n f ( ”%d/%d/%d” , &m2 , &d2 , &y2 ) ;

same = y1 == y2 && m1 == m2 && d1 == d2 ;


e a r l i e r = ( y1 < y2 ) | | ( y1 == y2 && m1 < m1) | |
( y1 == y2 && m1 == m2 && d1 < d2 ) ;

// R e s t o f t h e program
}

R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 2/5


C Programming
Selection of alternatives
If else statement

Examples Using if

Example 21 (contd)

if ( earlier )
p r i n t f ( ”%d/%d/%d p r e c e d e s %d/%d/%d\n” ,m1 , d1 , y1 , m2 , d2 , y2 ) ;
e l s e i f ( same )
p r i n t f ( ” D a t e s a r e same \n” ) ;
else
p r i n t f ( ”%d/%d/%d p r e c e d e s %d/%d/%d\n” ,m2 , d2 , y2 , m1 , d1 , y1 ) ;

R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 2/5


C Programming
Selection of alternatives
If else statement

Examples Using if

Example 22
read operator (o)

o == ’q’ ? yes quit

no

read operands invalid


(op1, op2) operation
no
no
no no no yes
o == ’+’? o == ’−’? o == ’*’? o == ’/’? if op2 != 0?

yes yes yes


yes
print op1+op2 print op1−op2 print op1*op2 print op1/op2

R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 2/5


C Programming
Selection of alternatives
If else statement

Examples Using if

Example 22 (contd)

#i n c l u d e < s t d i o . h>

i n t main ( ) {
char o ;
i n t op1 , op2 ;

p r i n t f ( ” Enter o p e r a t o r (+ , − ,∗ ,/) , q to q u i t : ” ) ;
s c a n f ( ”%c ” , &o ) ;
i f ( o == ’ q ’ ) r e t u r n 0 ;
else {
p r i n t f (” Enter operands : ” ) ;
s c a n f ( ”%d %d” , &op1 , &op2 ) ;
}
// P l u g t h e r e s t p a r t o f t h e program b e l o w t h i s l i n e
}

R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 2/5


C Programming
Selection of alternatives
If else statement

Examples Using if

Example 22 (contd)

i f ( o == ’+ ’ )
p r i n t f ( ”%d %c %d = %d\n” , op1 , o , op2 , op1 + op2 ) ;
e l s e i f ( o == ’− ’ )
p r i n t f ( ”%d %c %d = %d\n” , op1 , o , op2 , op1 − op2 ) ;
e l s e i f ( o == ’ ∗ ’ )
p r i n t f ( ”%d %c %d = %d\n” , op1 , o , op2 , op1 ∗ op2 ) ;
e l s e i f ( o == ’ / ’ && op2 !=0)
p r i n t f ( ”%d %c %d = %d\n” , op1 , o , op2 , op1 / op2 ) ;
else
p r i n t f ( ” i n v a l i d o p e r a t i o n \n” ) ;

R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 2/5


C Programming
Selection of alternatives
If else statement

Dangling else

When nested ifs are used, the problem of dangling else should be
addressed.
For example, consider
i f ( t e s t s c o r e >= 5 0 )
i f ( t e s t s c o r e > 90)
p r i n t f ( ”You g o t an A g r a d e \n” ) ;
e l s e // t e s t s c o r e i s b e t w e e n 50 t o 90
p r i n t f ( ”You f a i l e d \n” ) ; // WRONG

Indentation imply else is meant to be paired with outer if, but it pairs with
inner if.

R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 3/5


C Programming
Selection of alternatives
If else statement

Dangling else

Solution to Dangling Else

Enclose inner if by braces as shown in order to avoid dangling else.


i f ( t e s t s c o r e >= 5 0 ) {
i f ( t e s t s c o r e > 90)
p r i n t f ( ”You g o t an A g r a d e \n” ) ;
else
// t e s t s c o r e i s b e t w e e n 50 t o 90
} else
// t e s t s c o r e i s l e s s 5 0 .
p r i n t f ( ”You f a i l e d \n” ) ;

It is safer to enclose the statement(s) within if block by braces to


avoid dangling else.

R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 3/5


C Programming
Selection of alternatives
Conditional Operator

Conditional Operator

Conditional Operator

Sometime instead of if else, the conditional operator ? : could be used to


perform one of the two actions.

expression 1 ? expresssion 2 : expression 3

int i , j , k;
i = 1;
j = 2;
k = i > j ? i : j; // k = 2 h e r e
k = ( i >= 0 ? i : 0 ) + j ; // k = 3 h e r e

R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 4/5


C Programming
Selection of alternatives
Conditional Operator

Conditional Operator

Conditional Operator

Another simple effective use of conditional operator is:


#i n c l u d e < s t d i o . h>
#d e f i n e MAX(X , Y) X > Y ? X : Y

i n t main ( ) {
i n t i = 18 , j = 57;
p r i n t f ( ”Max i s : %d\n ” , MAX( i , j ) ) ;
}

R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 4/5


C Programming
Selection of alternatives
Conditional Operator

Conditional Operator

Conditional Operator

Similarly we can define macro for leap year:


y%100 !=0 && y%4 ==0

yes yes
y%100!=0? y%4== 0?

no no

y%400== 0? no leapyear = false leapyear = true

yes y%100 ==0 && y%400 ==0

#define LEAP(Y) ((Y%100 != 0) && (Y%4 == 0) || (Y% 400 == 0) ? 1 : 0;

R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 4/5


C Programming
Selection of alternatives
Conditional Operator

Conditional Operator
Example 23
#i n c l u d e < s t d i o . h>
#d e f i n e LEAP (Y) ( ( Y%100 != 0 ) && (Y%4 == 0 ) ) | | (Y%400 == 0 ) ? 1 : 0
i n t main ( ) {
i n t d , m, y , nd ;

p r i n t f ( ” E n t e r month ( 1 − 1 2 ) : ” ) ;
s c a n f ( ”%d” , &m) ;
p r i n t f (” Enter year : ” ) ;
s c a n f ( ”%d” , &y ) ;

nd = (m == 4 | | m == 6 | | m == 9 | | m == 1 1 )
i f (m != 2 )
i f ( nd ) d = 3 0 ;
else d = 31;
e l s e i f ( LEAP ( y ) ) d = 2 9 ;
else d = 28;
p r i n t f ( ”No . o f d a y s i n month %d o f y e a r %d i s : %d\n” , m, y , d ) ;
}

R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 4/5


C Programming
Selection of alternatives
Switch-case & Break

Switch-Case & Break


Multiple Alternatives
Switch-case is used for multiple alternatives.
For example, in case of mini calculator, 4 alternatives exists of
operator.
Instead of writing deep nested if else it is natural to use switch-case.
Switch-case, however needs break.
Break is a jump outside the switch-case block to sequentially next
statement.

R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 5/5


C Programming
Selection of alternatives
Switch-case & Break

Switch-Case & Break


Syntax

no no no
stars==3 stars==5 stars==7 default

yes yes yes

discount=2.5% discount=5% discount=10% no discount


break break break

out of switch−case

R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 5/5

You might also like