310 likes | 337 Views
Conditionals, Loops, and Other Kinds of Statements. CIS 1057 Computer Programming in C Fall 2013 (Slides include materials from The C Programming Language , 2 nd edition, by Kernighan and Ritchie and from C: How to Program , 5 th and 6 th editions, by Deitel and Deitel). Review.
E N D
Conditionals, Loops, and Other Kinds of Statements 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) Conditionals, Loops, and Other Statements
Review Functions 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! 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*/ Conditionals, Loops, and Other Statements
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; } Conditionals, Loops, and Other Statements
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*/ Conditionals, Loops, and Other Statements
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. 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 Conditionals, Loops, and Other Statements
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 Conditionals, Loops, and Other Statements
break statement needed to jump over subsequent cases default case is optional (not shown) case values must be constants 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 Conditionals, Loops, and Other Statements
Questions? Conditionals, Loops, and Other Statements
Iterative Statement • while loop • for loop • do-while loop 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 Conditionals, Loops, and Other Statements
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"); while loop example Note initialization of sum and count What is this? Conditionals, Loops, and Other Statements
while loop examples (continued) A program to count digits, white space characters, and other characters. Includes a while loop with a switch statement inside Conditionals, Loops, and Other Statements
do-while loop do statementwhile(expression); Similar to while loop, but guaranteed to execute statement at least once Note: semicolon is required here 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*/ Conditionals, Loops, and Other Statements
Questions? Conditionals, Loops, and Other Statements
for loop 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 Remember: zero is false, non-zero is true Conditionals, Loops, and Other Statements
for loops and while loops The for loop for (expr1; expr2; expr3) statement is exactly equivalent to the following expr1; while (expr2) { statement expr3;} Conditionals, Loops, and Other Statements
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 Conditionals, Loops, and Other Statements
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 Conditionals, Loops, and Other Statements
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! 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*/ ...; } ...; } Conditionals, Loops, and Other Statements
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 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 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 Conditionals, Loops, and Other Statements
Questions? Conditionals, Loops, and Other Statements
An Example int startingDay; /* init from user input*/ for (int month = 0; month < 12; month++) { } // for month Conditionals, Loops, and Other Statements
An An Example (continued) int startingDay; /* init from user input*/ for (int month = 0; month < 12; month++) { } // for month This variable is “global” to the loop. I.e., it is remembered from one iteration to the next. Conditionals, Loops, and Other Statements
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. Conditionals, Loops, and Other Statements