tailieunhanh - Lecture Introduction to Computer Programming - Lecture 9
The contents of this chapter include all of the following: Nested if else statement, logical operator in C, how logical operator can be used to make compound conditions, conditional operator, nested conditional operator, nested if-else, nested conditional operator, loops. | CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 9 Thanks to Detal and Detal official website for some lecture slides Switch Statement continued from the last Lecture Decisions Using switch The control statement that allows us to make a decision from the number of choices is called a switch, or Switch is also called a switch-case-default, since these three keywords go together to make up the control statement 5/26/2012 3 Switch --- Features The integer expression following the keyword switch could be: an integer constant like 1, 2 or 3, or a character constant like ‘a’ or an expression that evaluates to an integer like 2*4 The constant in each case must be different from all the others. The “do this” lines in the body of switch construct represent any valid C statements 5/26/2012 4 Switch --- General Format switch ( integer expression ) { case constant 1 : do this ; case constant 2 : do this ; case constant 3 : do this ; default : do this ; } 5/26/2012 5 Switch statement ---- incorrect void main( ) { int i = 2 ; switch ( i ) { case 1 : printf ( "I am in case 1 \n" ) ; case 2 : printf ( "I am in case 2 \n" ) ; case 3 : printf ( "I am in case 3 \n" ) ; default : printf ( "I am in default \n" ) ; } /* What is the out put? */ } 5/26/2012 6 Switch statement ---- incorrect output I am in case 2 I am in case 3 I am in default Why ? Correct program would be 5/26/2012 7 5/26/2012 8 void main( ) { int i = 2 ; switch ( i ) { case 1 : printf ( "I am in case 1 \n" ) ; break ; case 2 : printf ( "I am in case 2 \n" ) ; break ; case 3 : printf ( "I am in case 3 \n" ) ; break ; default : printf ( "I am in default \n" ) ; } } Switch statement ---- correct Output: I am in case 2 5/26/2012 9 void main( ) { int i = 22 ; switch ( i ) { case 121 : printf ( "I am in case 121 \n" ) ; break ; case 7 : printf ( "I am in case 7 \n" ) ; break ; case 22 : printf ( "I am in case 22 \n" ) ; . | CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 9 Thanks to Detal and Detal official website for some lecture slides Switch Statement continued from the last Lecture Decisions Using switch The control statement that allows us to make a decision from the number of choices is called a switch, or Switch is also called a switch-case-default, since these three keywords go together to make up the control statement 5/26/2012 3 Switch --- Features The integer expression following the keyword switch could be: an integer constant like 1, 2 or 3, or a character constant like ‘a’ or an expression that evaluates to an integer like 2*4 The constant in each case must be different from all the others. The “do this” lines in the body of switch construct represent any valid C statements 5/26/2012 4 Switch --- General Format switch ( integer expression ) { case constant 1 : do this ; case constant 2
đang nạp các trang xem trước