1、cs202ch2COURSE : C Language (CS102)UNIT : 2.1 CONTROL STRUCTURE SEQUENCE* Instructions are executed one after another without branching.* E.g. # include main() int a, b ; printf ( Enter the first integer ) ; scanf ( %d , &a); printf ( Enter the second integer n ); scanf ( %d , &b); printf ( Sum is %
2、d n , a+b ); return 0; SELECTION STRUCTUREA selection structure is used to choose among different actions. Single Selection ( IF Selection Structure ) * It performs an action only when a condition is true. * Format : if (condition) statement Problem : print the word Passed if the grade is greater th
3、an 60 Coding : if (grade 60) printf ( Passed n ); (print only if grade 60) Warning : if (grade60); - if (grade60) ; printf( “ Passed n” ); printf( “ Passed n” ); (print with any grade)Double Selection (IF/ ELSE Selection Structure)* It performs an action when a condition is true, perform a different
4、 action when the condition is false * Format : if (condition) statement-1 else statement-2 Problem : print the word Passed if the grade is greater than 60 print the word Failed if the grade is below or equal 60 Coding : if (grade 60) printf ( Passed n ); else printf( Failed n ); Multiple Selection N
5、ested IF/ELSE Selection Structure * Format : if (condition-1) statement-1 else if (condition-2) statement-2 . * Problem(1) : print A for the marks greater than or equal to 80. print B for the mark greater than or equal to 70. print C for the mark greater than or equal to 60. And F for other marks *
6、Coding : if (grade = 80) /80-100 printf ( A n ); else if (grade = 70 ) /70-79 printf ( B n ); else if (grade = 60 ) /60-69 printf ( C n ); else /59 or below printf ( F n ); *if selection structure expects only one statement in its body, to include several statements in the body, enclose the set of s
7、tatements using * E.g. if (grade 60) printf( Failed n ); printf( You must work hard n ); else if . * Problem (2): Convert an integer into the corresponding day of a week. * Coding : if (day = 1) printf ( Today is Monday n ); else if (day = 2 ) printf ( Today is Tuesdayn ); else if (day = 3) printf (
8、 Today is Wednesday n ); else if (day = 4 ) printf ( Today is Thursday n ); else if (day = 5) printf ( Today is Friday n ); else if (day = 6 ) printf (Today is Saturdayn); else printf(“Today is Sundayn”)Switch structure ( multiple selection) Problem : Convert an integer into the corresponding day of
9、 a week. Coding : main() int day ; printf(Enter an integer between 1 to 7. Enter -1 to quit : ); scanf(%d , &day) ; switch (day) case 1 : printf(Today is Mondaynn) ; break ; case 2 : printf(Today is Tuesdaynn) ; break ; case 3 : printf(Today is Wednesdaynn) ; break ; case 4 : printf(Today is Thursda
10、ynn) ; break ; case 5 : printf(Today is Fridaynn) ; break ; case 6 : printf(Today is Saturdaynn) ; break ; case 7 : printf(Today is Sundaynn) ; break ; default : printf(Incorrect Number Entered!) ; break ; return 0; Enter an integer between 1 to 7. Enter -1 to quit : 1 Today is Monday Enter an integ
11、er between 1 to 7 . Enter -1 to quit : 6 Today is Saturday Enter an integer between 1 to 7 . Enter -1 to quit : 9 Incorrect Number Entered! Enter an integer between 1 to 7 . Enter -1 to quit : -1 Break * The switch structure is exited immediately with the break statement. * If break is not used, eve
12、n a match is found, the statements for all the remaining cases will still be executed. * break can /cannot be used in default statement (last statement) Default * If no match occurs, the default case is executed * place the default clause at the end of the switch structure. Case * Each case can have
13、 one or more action and braces are not required around multiple action in a case of a switch Switch *switch structure can only be used for testing a constant integral expression, that is, any combination of character constants and integer constants that evaluates to a constant integer value. switch(
14、day) E.g. case 1 : . case 2 : . E.g. case a : . case b : . E.g. case abc : . WRONG USE! case 123 : . REPETITION (looping) STRUCTURE* An action will be repeated while some condition remains true.* The action will stop only if the condition become false. WHILE Repetition Structure * Format : while (co
15、ndition) statement * Problem(1): sum all the integer from 1 to 10 * Coding : main() int sum, number; sum=0; number = 1 ; while (number = 10) sum = sum+number ; number+; printf(“Sum is %d ”, sum); Sum is 55 * There will be an infinite loop if the condition always true. * Must be an action that eventu
16、ally cause the condition to become false *Probem(2): Determine the class average of a test taken by 5 student . * Coding : main() int student_counter, total_mark, mark ; double average ; total_mark = 0 ; student_counter = 1; while (student_counter = 5) printf (Please enter the mark : ); scanf ( %d ,
17、 &mark ) ; total_mark = total_mark + mark ; student_counter = student_counter +1 ; average = total_mark / 5 ; printf ( The class average is %lf n , average) ; return 0 ; * OUTPUT : Please enter the mark : 50 Please enter the mark : 10 Please enter the mark : 90 Please enter the mark : 93 Please ente
18、r the mark : 50 The class average is 58.000000 * Actual average should be (50+50+10+90+93)/5 58.6 Why %f prints 58.000000 instead of 58.6 ? NOTE : average = total_mark /10 ; (float) (int) (int) Integer division yields an integer result Significant digits are cut. *Problem(2) Revised Program * Coding
19、 : main() int student_counter, mark ; double average, total_mark ; total_mark = 0 ; student_counter = 0 ; printf( Enter the mark , or enter -1 to end : ) ; scanf( %d , &mark ) ; while (mark != -1) total_mark = total_mark + mark ; student_counter = student_counter +1 ; printf (Pls enter the next mark
20、,or enter -1 to end:); scanf ( %d , &mark ); if (student_counter != 0) average = total_mark /student_counter ; printf ( The class average is %.3lf n , average) ; else printf( No marks were entered n ); return 0 ; * OUTPUT Enter the mark , or enter -1 to end : 50 Please enter the next mark , or enter
21、 -1 to end : 10 Please enter the next mark , or enter -1 to end : 90 Please enter the next mark , or enter -1 to end : 93 Please enter the next mark , or enter -1 to end : 50 Please enter the next mark , or enter -1 to end : -1 The class average is 58.600 * OUTPUT Enter the mark , or enter -1 to end
22、 : -1 No marks were entered * Format string %.3f .3 - a floating-point with 3 decimal digits to the right of decimal point. eg. printf( %.2f , 9.348) ; 9.35 eg. printf( %.1f , 9.348) ; 9.3 eg. printf( %.0f , 9.348) ; 9 The default precision is (%.6f = %f ) * Precision are only used in printf, not sc
23、anf . eg. scanf(“%.5lf n” , &a) wrong ! scanf(“%lf ” , &a) * Do not compare floating-point value for equality.DO/ WHILE Structure It test the loop-continuation condition after the loop body is performed, so loop body will be executed at least once. * Format : do statements ; while (condition); * Pro
24、blem : Find the no.of times the statements has been executed in the loop . * Coding Using Do/While Loop : main() int loop_count, number ; loop_count = 0 ; number =0 ; do loop_count+; number-; while (number 0) ; printf ( Loop_count is %d n , loop_count) ; printf ( Number is %d n , number) ; return 0
25、; Loop_count is 1 Number is -1 * Coding Using While Loop : main() int loop_count, number ; loop_count = 0 ; number =0; while (number 0) loop_count+ ; number-; printf ( Loop_count is %d n , loop_count) ; printf ( Number is %d n , number) ; return 0 ; Loop_count is 0 Number is 0 FOR Structure * Proble
26、m : Print 1 to 10 1 * Coding (1): main() 2 int counter ; 3 4 for (counter=1; counter=10; counter+) 5 6 printf( %d , counter) ; : printf(n) ; : return 0 ; * Coding(2) : main() int counter=1 ; for ( ; counter 11 ; counter+) printf( %d , counter) ; printf(“n”); return 0 ; * 1 2 3 4 5 6 . * for (counter
27、=1 ; counter=10 ; counter+) Initialization, repetition condition, increment are all put in the header. - Initialization occurs only once, - Increment occurs after the body statement is performed. 1 2 4 for (counter=1; counter=10; counter+) printf( %d , counter) ; 3 printf(n) ; 1 2 3 4 * The conditio
28、n (counter = 10 ) or (counter 11 ) is OK But (counter 10 ) will run only 9 time and caused off-by-one error. * For Loop While Loop main() main() int count ; int count ; for (count=1;count =10;count+) count =1 ; printf( %d , count) ; while (count = 10 ) printf(“n”); return 0 ; printf( %d , count) ; count+; printf(“n”); return 0 ; * Initialization, condition, and
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1