1 / 27

CSCI 125 & 161 Lecture 7

CSCI 125 & 161 Lecture 7. Martin van Bommel. Control Statements. Statements that affect the sequence of execution of other statements Normal is sequential May want to repeat statements - iteration May or may not want particular statement executed at all - conditional.

Download Presentation

CSCI 125 & 161 Lecture 7

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. CSCI 125 & 161 Lecture 7 Martin van Bommel

  2. Control Statements • Statements that affect the sequence of execution of other statements • Normal is sequential • May want to repeat statements - iteration • May or may not want particular statement executed at all - conditional

  3. Control Statement Structure • Control line - first line (e.g. while) • Body - statements enclosed in { } that are repeated • Body typically indented to show range of control statement

  4. Conditional Execution • May want to execute a statement only if some condition applies • May wish to choose between two alternative courses of actions depending on some test • Simplest form - if statement

  5. if Statement if (conditional-test) { …statements executed if test true... } } else { …statements executed if test false... }

  6. Relational & Logical Operators == Equal != Not equal > Greater < Less >= Greater or equal <= Less or equal • Logical operate on results of comparisons ! Not (true if operand false – one operand) && And (true if both true) || Or (true if either true)

  7. Example 1 • “x is not equal to either 2 or 3” if (x != 2 || x != 3) • No, it should be if (!(x == 2 || x == 3)) • or if (x != 2 && x != 3)

  8. Example 2 • “x is in the range from 0 to 10 exclusive” if (0 < x < 10) • No, it should be if (0 < x && x < 10)

  9. Example • Leap year every fourth year, except centuries, then just every fourth century • year is divisible by 4 but not by 100, or • year is divisible by 400 • Try ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)

  10. Bad Example if (x < 5) cout << ”A”; if (x > 5) cout << ”B”; else cout << ”C”;

  11. If Blocking Rule • For any if statement that requires (1) more than a single line, or (2) an else clause, always use braces to enclose in a separate block the statements under the control of the if statement .

  12. Single-line if statement if (condition) statement where: condition is the Boolean value to test statement is a single statement to be executed if condition is true

  13. Multiline if statement if (condition) { statements; } where condition is the Boolean value to test statements is a block of statements to be executed if condition is true

  14. if-else statements if (condition) { statementsT ; } else { statementsF ; }

  15. Cascading if statement if (condition1) { statements1 ; } else if (condition2) { statements2; . . . } else { statementsnone }

  16. Iteration and Loops • Iteration - process of repeating an operation • Essential for large amounts of data - repeating statements shortens programs • Loop - any portion of program repeated via control statement • e.g. while loop

  17. The while statement while (condition) { statements } where condition tested before first and every iteration statements executed each time condition true

  18. while operation • Condition test performed before every cycle, including first. • If condition is initially false, body of loop not executed at all. • Condition test performed only at beginning of each loop cycle. • Even if condition becomes false during cycle, cycle is completed.

  19. while example • Sum up digits of a number sum = 0; while (n > 0) { sum = sum + n % 10; n = n / 10; }

  20. Infinite loops • A loop cycle that never stops – infinite loop • Condition of while statement never false • e.g. while (n >= 0) { sum += n % 10; n /= 10; }

  21. Repeat-N-Times Idiom • for (i = 0; i < N; i++) {statements to be repeated } • May want to add up 10 input numbers for (i = 0; i < 10; i++) { cin >> value; sum = sum + value; }

  22. Index Variable in Counting Loop for (i = 0; i < N; i++) • Variable i is called the index variable • Any variable may be used - traditionally i • Must be declared at beginning of function • Initial value of i in loop is 0 - first cycle • Next cycles value is 1, 2, etc. • On last cycle, value is N – 1 • After loop, value of i is N

  23. Non-Zero Counting Loop • It is possible to modify for loop to begin at another number for (i = first; i <= last; i++) • Note the <= operator instead of < • Value of i ranges from first to last • Used if value of i required in body of loop

  24. for statement for (init; test; step) { statements } where init is an expression evaluated at beginning test is a condition for continuing the loop step is an expression to prepare next cycle

  25. for example • Countdown from 10 to 0 for (t=10; t >= 0; t--) { cout << t; } cout << ”Liftoff!\n”;

  26. for more • Note that expressions init, test, and step are optional, but the semicolons must appear for (;;) - infinite loop for (;t<10;) same as while (t<10)

  27. Nested for loops • Create a 10 x 10 multiplication table for (row=1; row<=10; row++) { for (col=1; col<=10; col++) { cout << (row * col); } cout << endl; }

More Related