dr. yang, qingxiong (with slides borrowed from dr. yuen, joe) lt4: control flow - loop cs2311...

Click here to load reader

Upload: vincent-simmons

Post on 17-Dec-2015

219 views

Category:

Documents


2 download

TRANSCRIPT

  • Slide 1
  • Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming
  • Slide 2
  • Outlines 2 While and Do While loop For loop
  • Slide 3
  • Outcomes 3 Able to express repeating task with a loop statement in C++ Clearly define the loop condition and loop body
  • Slide 4
  • Syntax Summary 4 Keywords while, do, for Punctuators,
  • Slide 5
  • Loop 5 Beside sequential execution and branch execution, looping is another common control flow in programming. When the execution enters a loop, it will execute a block of code repeatedly until the exit loop condition is matched
  • Slide 6
  • Loop 6 In general, a program loop consists of: Initialization statements Body Exit condition Post loop statements (stepping forward to exit loop condition)
  • Slide 7
  • Loop 7 Set x=0; cout 10) then exit the loop Add 1 to X Loop back
  • Slide 8 > n; while (n > x; /* read 1st number */ max = x; /* pick the largest number in while-loop */ while (++cnt < n) { cin >> x; /* read another number */ if (max < x) max = x; } cout
  • An example program of while (contd) 9 cout > x; /* read 1st number */ max = x; /* pick the largest number in while-loop */ while (++cnt < n) { cin >> x; /* read another number */ if (max < x) max = x; } cout
  • Example of do statement 11 int error; /* no Boolean type support */ int n; do { cout > n; if (error = (n 1 and m1 and m1 and m> n">
  • break statement 19 the break statement causes an exit from the innermost enclosing loop or switch statement (discussed already) while (1) { cin >> n; if (n < 0) break; /* exit loop if x is negative */ cout
  • Example of continue statement 21 /* read in and sum 10 not-too-small numbers. */ cnt = 0; while (cnt < 10) { cin >> x; if (x > -0.01 && x < 0.01) continue; /* discard small values */ ++cnt; sum += x; /* continue transfer control here */ }
  • Slide 22
  • Continue, break 22
  • Slide 23
  • Comma operator (,) 23 q It has the lowest precedence of all operators in C++ and is evaluated from left to right q General form is expr1, expr2 q The comma expression as a whole has the value and type of its right operand Sometimes used in for statements to allow multiple initializations and multiple processing of indices q The comma operator is rarely used q Not all commas in a C++ program are comma operators
  • Slide 24
  • Examples of comma operator 24 sum=0; for (j=1; j> true; /* print true */ else cout >> false; Use (2 < k && k < 7) for the correct answer
  • Slide 27
  • Further remarks 27 q Use a relational expression, if possible, rather than an equality expression to control a loop or a selection statement, e.g., Don t use while (j!=4) {... } Use while (j
  • Programming style 29 Follow the normal rules of English (e.g. putting a space after a comma) Put one space on each side of a binary operator (e.g. = > < ) for readability Indent code in a consistent fashion to indicate the flow of control (use the tab key) Note the multiple levels of indentation
  • Slide 30
  • Indentation 30 void main(){ int i; for (i = 0; i < 100; i++) { if (i>3) cout