You are on page 1of 13

C Programming Selection of alternatives Switch-case & Break

Switch-Case & Break


Example 24
s w i t c h ( month ) case 1 : case 2 : case 3 : case 4 : case 5 : case 6 : case 7 : case 8 : case 9 : case 10: case 11: case 12: default : } { p r i n t f ( J a n u a r y \n ) ; break ; p r i n t f ( F e b r u a r y \n ) ; break ; p r i n t f ( March \n ) ; break ; p r i n t f ( A p r i l \n ) ; break ; p r i n t f ( May\n ) ; break ; p r i n t f ( June \n ) ; break ; p r i n t f ( J u l y \n ) ; break ; p r i n t f ( Au g us t \n ) ; break ; p r i n t f ( S e p t e m b e r \n ) ; b r e a k ; p r i n t f ( O c t o b e r \n ) ; break ; p r i n t f ( November \n ) ; break ; p r i n t f ( December \n ) ; break ; p r i n t f ( No s u c h month \n ) ; // B r e a k i s n o t n e e d h e r e

R. K. Ghosh (IIT-Kanpur)

C Programming

January 19, 2011

5/5

C Programming Selection of alternatives Switch-case & Break

Switch-Case & Break


Example 25
#i n c l u d e < s t d i o . h> i n t main ( ) { int n; p r i n t f ( Enter the t e l e p h o n e code : ) ; s c a n f ( %d , &n ) ; p r i n t f ( A rea switch (n) { case 11: case 22: case 33: case 40: default : } } code C i t y \n ) ;

p r i n t f ( 11 D e l h i \n ) ; b r e a k ; p r i n t f ( 22 Mumbai\n ) ; b r e a k ; p r i n t f ( 33 K o l k a t a \n ) ; b r e a k ; p r i n t f ( 40 C h e n n a i \n ) ; b r e a k ; p r i n t f ( Ar ea c o d e i s n o t r e c o g n i z e d \n ) ; // b r e a k n o t n e c e s s a r y h e r e

R. K. Ghosh (IIT-Kanpur)

C Programming

January 19, 2011

5/5

C Programming Selection of alternatives Switch-case & Break

Switch-Case & Break


Example 26
#i n c l u d e < s t d i o . h> i n t main ( ) { int n , ndigit = 0; p r i n t f ( E n t e r a number ( <=999): ) ; s c a n f ( %d , &n ) ; i f ( n < 10) n d i g i t = 1 ; e l s e i f ( n < 100) n d i g i t = 2 ; e l s e i f ( n < 1000) n d i g i t = 3 ; // R e s t o f t h e program }

R. K. Ghosh (IIT-Kanpur)

C Programming

January 19, 2011

5/5

C Programming Selection of alternatives Switch-case & Break

Switch-Case & Break


Example 26 (contd)
switch ( n d i g i t ) { c a s e 1 : p r i n t f ( I n p u t : %d o u t p u t : %d\n , n , n ) ; break ; c a s e 2 : p r i n t f ( I n p u t : %d o u t p u t : %d\n , n , n%1010 + n / 1 0 ) ; break ; c a s e 3 : p r i n t f ( I n p u t : %d o u t p u t : %d\n , n , ( n % 10 10 + ( n / 1 0 ) % 1 0 ) 10 + n / 1 0 0 ) ; break ; d e f a u l t : p r i n t f ( I n v a l i d i n p u t \n ) ; }

R. K. Ghosh (IIT-Kanpur)

C Programming

January 19, 2011

5/5

C Programming Selection of alternatives Switch-case & Break

Switch-Case & Break


Example 27
#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 ; // E x i t from main ( t e r m i n a t e program ) else { p r i n t f ( Enter operands : ) ; s c a n f ( %d %d , &op1 , &op2 ) ; } // s w i t c h c a s e s t a t e m e n t f o r t h e o p e r a t i o n s }

R. K. Ghosh (IIT-Kanpur)

C Programming

January 19, 2011

5/5

C Programming Selection of alternatives Switch-case & Break

Switch-Case & Break


Example 28 (contd)
switch (o) { c a s e + : c a s e : case : case / : p r i n t f ( %d %c %d = %d\n , op1 , o , op2 , op1 + op2 ) ; break ; p r i n t f ( %d %c %d = %d\n , op1 , o , op2 , op1 op2 ) ; break ; p r i n t f ( %d %c %d = %d\n , op1 , o , op2 , op1 op2 ) ; break ; i f ( 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 ( D i v i s i o n by 0 n o t p o s s i b l e \n ) ; break ; p r i n t f ( n o t a v a l i d o p e r a t o r \n ) ; // b r e a k i s n o t n e e d e d h e r e

default }

R. K. Ghosh (IIT-Kanpur)

C Programming

January 19, 2011

5/5

C Programming Selection of alternatives Switch-case & Break

Switch-Case & Break


Sequential Fall Through

stars==3 yes discount=2.5% no break

no

stars==5 yes discount=5% break

no

stars==7 yes discount=10% break

no

default

no discount

sequential fall through

R. K. Ghosh (IIT-Kanpur)

C Programming

January 19, 2011

5/5

C Programming Selection of alternatives Switch-case & Break

Switch-Case & Break


Example 29
#i n c l u d e < s t d i o . h> i n t main ( ) { char c ; s c a n f ( %c , &c ) ; switch ( c ) { c a s e 0 : // S e q u e n t i a l c a s e 1 : // . c a s e 2 : // . c a s e 3 : // . c a s e 4 : // F a l l c a s e 5 : // . c a s e 6 : // . c a s e 7 : // . c a s e 8 : // Through c a s e 9 : p r i n t f ( %c i s a D i g i t \n , c ) ; b r e a k ; d e f a u l t : p r i n t f ( %c i s n o t a d i g i t \n , c ) ; } }
R. K. Ghosh (IIT-Kanpur) C Programming January 19, 2011 5/5

C Programming Loops and Repetitive Computations

Loops

Why Loops To automate repetition of computation. To iterate until the occurrence of an event To attempt operation until successful or limit of attempts exceeded.

R. K. Ghosh (IIT-Kanpur)

C Programming

January 19, 2011

1 / 10

C Programming Loops and Repetitive Computations

Loops

Examples of Repetitiveness Repetitive computation is a major requirement. Arithmetic computation on a sequence:


determine next number of the sequence, compute sum or product of the sequence.

Statistical computation on sequence of numbers:


max, min, average, std deviation.

R. K. Ghosh (IIT-Kanpur)

C Programming

January 19, 2011

1 / 10

C Programming Loops and Repetitive Computations

Loops

Types of Loops Counter controlled loops: control variable counting up/down (normal loops) Event controlled loops: until special value is encountered. (E.g., terminate loop when input is q) Result controlled loops: continues until a test determines that the desired result is reached (eg, numerical approximations)

R. K. Ghosh (IIT-Kanpur)

C Programming

January 19, 2011

1 / 10

C Programming Loops and Repetitive Computations While

While Loop
Control Flow while (expr) { body }

initialization initialization

pretest

repeat block

repeat block exit


R. K. Ghosh (IIT-Kanpur) C Programming

posttest exit
January 19, 2011 2 / 10

C Programming Loops and Repetitive Computations While

While Loop
Example 30: Counter Controlled While
#i n c l u d e < s t d i o . h> i n t main ( ) { int i = 0 , n; d o u b l e sum = 0 . 0 , x ; p r i n t f ( E n t e r number o f v a l u e s t o r e a d : ) ; s c a n f ( %d , &n ) // DON T FORGET t o i n i t i a l i z e i b e f o r e e n t e r i n g l o o p . while ( i < n) { p r i n t f ( Enter next value : ) ; s c a n f ( % l f , &x ) ; // R e a d i n g a d o u b l e sum += x ; i ++; // DON T FORGET t o i n c r e m e n t i } p r i n t f ( A v e r a g e o f 10 v a l u e s = %.3 f \n , sum / 1 0 . 0 ) ; }

R. K. Ghosh (IIT-Kanpur)

C Programming

January 19, 2011

2 / 10