Xem mẫu

Chapter 6 ­ Repetition while Loop Simplest loop Two parts: test expression and loop body Pre­tested loop – Execute loop body if test true – Bypass loop body if test false Lesson 6.1 General Structure while (expression) Logical expression (variable or arithmetic expression) Boolean results (True or False) statement2 … Lesson 6.1 } Steps In Using a while Loop 1. Initialize variable acting as test expression 2. Evaluate the boolean test 3. If true execute statements within loop 4. Update the variable 5. Go back to step 2 Note: if no change to variable then infinite loop (never ends) is created Lesson 4.1 Example while Loop int i= 0, number = 1; Initialize variables while (number) Variable as expression { value other than zero tests true cout << “Please type a number. ” << “Type 0 (zero) to stop execution.\n”; cin >> number; i++; Loop body if (i > 50) break; } Statements that provide exit from loop Lesson 6.2 ... - tailieumienphi.vn
nguon tai.lieu . vn