Conditionals, Loops, and Other Kinds of Statements Assumptions: Graduate level Operating Systems Making Choices about operation systems Why a micro-century? …just about enough time for one concept CIS 1057 Computer Programming in C Fall 2013 (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements
Conditionals, Loops, and Other Statements Review Functions CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 2
Conditionals, Loops, and Other Statements If-else Statements if (expr) statement1 /*do this if expr is non-zero*/ else statement2 /*do this if expr is zero*/ The else clause is optional! CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 3
Conditionals, Loops, and Other Statements If-else Examples if (j > limit) return j; /* note semicolon*/ else j += stepValue; /* note semicolon*/ ... if (++k >= 4) k = 0; /* increment k mod 4*/ CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 4
If-else Examples (continued) if (n < maxInput) { scanf("string", &arg1, &arg2, …); n++; printf("string2", arg3, arg4, …); } else { /* note NO semicolon*/ printf("Summary\n", arg5, …); return n; } CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 5
Concatenated If-else Statements if (expr1) statement1 /*do this if expr1 is non-zero*/ else if (expr2) statement2 /*i.e., expr1 == 0, expr2 != 0*/ else if (expr3) statement3 /expr1 and expr2 are zero, expr3 != 0*/ else if (expr4) statement4 /expr1, expr2 , expr3 all zero, expr4 != 0*/ else statement5 /*i.e., all expr are zero*/ CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 6
Concatenated If-else Statements Last else is always attached to last if If it should be attached to any other if, use {} to control the flow of execution. CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 7
Conditionals, Loops, and Other Statements Switch Statement Somewhat like a concatenated if-else Occasionally easier to read Each arm is called a case Evaluate switch expression, select the appropriate case (if any) default case is optional Difference from concatenated if-else Need break statement to exit switch after a case Otherwise, control falls through to next case CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 8
Switch Statement Example int month, daysInMonth, leapYear; … switch (month) { case 0: printf("January\n"); daysInMonth = 31; break; case 1: printf("February\n"); daysInMonth = leapYear ? 29 : 28; break; case 2: printf("March\n"); daysInMonth = 31; break; case 3: printf("April\n"); daysInMonth = 30; break; … } // switch CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements
Switch Statement Example case values must be constants int month, daysInMonth, leapYear; … switch (month) { case 0: printf("January\n"); daysInMonth = 31; break; case 1: printf("February\n"); daysInMonth = leapYear ? 29 : 28; break; case 2: printf("March\n"); daysInMonth = 31; break; case 3: printf("April\n"); daysInMonth = 30; break; … } // switch break statement needed to jump over subsequent cases default case is optional (not shown) CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements
Conditionals, Loops, and Other Statements Questions? CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 11
Conditionals, Loops, and Other Statements Iterative Statement while loop for loop do-while loop CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements
Conditionals, Loops, and Other Statements while loops while (expression) statement Evaluate expression If true, execute statement and then repeat Repeat until expression becomes false statement may be executed zero or more times! Often a compound statement CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 13
Conditionals, Loops, and Other Statements while loop example Note initialization of sum and count int sum = 0; int count = 0; int input; while (scanf("%d", &input) != EOF){ sum += input; count++; printf("Input value of %f recorded.\n", input); ... } if (count > 0) printf("Average is %f\n", (double)sum/count); else printf("No inputs recorded\n"); What is this? CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 14
while loop examples (continued) A program to count digits, white space characters, and other characters. Includes a while loop with a switch statement inside CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 15
Conditionals, Loops, and Other Statements do-while loop Note: semicolon is required here do statement while (expression); Similar to while loop, but guaranteed to execute statement at least once CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 16
Conditionals, Loops, and Other Statements Breaking out of a Loop When it becomes necessary to terminate a loop prematurely break; /*exits smallest containing switch or loop*/ When it becomes necessary to terminate the current iteration and start the next one continue; /*terminates this iteration only*/ CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 17
Conditionals, Loops, and Other Statements Questions? CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 18
Conditionals, Loops, and Other Statements for loop Remember: zero is false, non-zero is true A counting loop for (expr1; expr2; expr3) statement Evaluate expr1 to initialize Evaluate expr2 to test If true, execute statement If not true, exit for loop Evaluate expr3 to prepare for next iteration Repeat expr2 to test CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 19
for loops and while loops The for loop for (expr1; expr2; expr3) statement is exactly equivalent to the following expr1; while (expr2) { statement expr3; } CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 20
The most common kind of for-loop int i; for (i = 0; i < limit; i++) { ...; /* do something with ith entity */ } Loop “body” is typically a compound statement CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 21
The most common kind of for-loop int i; for (i = 0; i < limit; i++) { ...; /* do something with ith entity */ } It is traditional in C that for-loops start counting at zero and test that the counter is less than the upper limit CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 22
The most common kind of for-loop int i; for (i = 0; i < limit; i++) { ...; /* do something with ith entity */ } This loop iterates limit times. Iterations are numbered i = 0, 1, 2, …, limit-1 Reason:– arrays are indexed this way! CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 23
Conditionals, Loops, and Other Statements Nested for-loops int i, j; for (i = 0; i < limit; i++) { ...; /*prepare subgroup i*/ for (j=0; j < limit2; j++) { ...; /* do something with item j of subgroup i*/ } CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 24
An Extension in Modern C Compilers The following construct is legal in C99:– for (int i = 0; i < limit; i++){ expression involving i; ...; printf(“Iteration %d completed.\n”, i); ...; } The loop counter i is declared in for loop Not visible outside of loop! Common practice Good programming style CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 25
Conditionals, Loops, and Other Statements Notes on Loop Style for (int i = 0; i < limit; i++) { ...; /*prepare for subgroup i*/ for (int j=0; j < limit2; j++) { ...; /* do something with item j of subgroup i*/ } /* end of loop j */ } /* end of loop i */ Declare loop variables in for-statements – C99 CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 26
Conditionals, Loops, and Other Statements Notes on Loop Style for (int i = 0; i < limit; i++) { ...; /*prepare for subgroup i*/ for (int j=0; j < limit2; j++) { ...; /* do something with item j of subgroup i*/ } /* end of loop j */ } /* end of loop i */ Include a comment at end of each loop to show which loop it is CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 27
Conditionals, Loops, and Other Statements Questions? CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 28
Conditionals, Loops, and Other Statements An Example int startingDay; /* init from user input*/ for (int month = 0; month < 12; month++) { } // for month CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 29
An An Example (continued) This variable is “global” to the loop. I.e., it is remembered from one iteration to the next. int startingDay; /* init from user input*/ for (int month = 0; month < 12; month++) { } // for month CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 30
An Example (continued) int startingDay; /* init from user input*/ for (int month = 0; month < 12; month++) { } // for month At beginning of each iteration, startingDay indicates the day of the week on which that particular month starts. It is the responsibility of the loop to update startingDay for the next month. CIS 1057 Fall 2013 Conditionals, Loops, and Other Statements 31