1 / 31

Conditionals, Loops, and Other Kinds of Statements

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.

dneese
Download Presentation

Conditionals, Loops, and Other Kinds of Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Review Functions Conditionals, Loops, and Other Statements

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. Questions? Conditionals, Loops, and Other Statements

  12. Iterative Statement • while loop • for loop • do-while loop Conditionals, Loops, and Other Statements

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. Questions? Conditionals, Loops, and Other Statements

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. Questions? Conditionals, Loops, and Other Statements

  29. An Example int startingDay; /* init from user input*/ for (int month = 0; month < 12; month++) { } // for month Conditionals, Loops, and Other Statements

  30. 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

  31. 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

More Related