Repetition

循环

​Parts of a Loop

  • Initialization

    • Usually of a loop control variable

  • Continuation condition

    • Boolean expression specifying when to continue executing the loop

  • Update

    • Usually of a loop control variable

  • Loop Body

    • Sequence of the statement to execute

Creating a Loop

  1. What do things have to look like before entering the loop?

    Initialization (control variable)

    Initialization (loop body)

  2. How do you exit/end the loop?

    Control variable

    Continuation condition

    Interrupt: break, return, exception

  3. What do you do to ensure you eventually exit/end the loop?

    Update

  4. What does the loop do?

    Loop Body

While

//initialize 
While (continuation condition)
{
    //loop body
    //update
}
While 循环

For

//A. initialize
//B. continuation condition
//C. update

for (A; B; C) {
 //loop body
 }
for 循环(其实都差不多)

Types of control

  • Counting

    • Control variable increments by set amount each iteration

  • Sentinel

    • Control variable is set to a value obtained during the loop each iteration

  • Flag

    • Control variable is Boolean varaible tha represents a condition each iteration

    • Special case of sentinel

Do While

//initialize
do {
    //loop body
    //update (can be initialization)
} while (continuation conidtion);
do while 循环

while 和 do while 的区别

  1. while是先判断条件,条件为true之后才会执行内部代码

  2. do while是先执行内部代码,再判断条件,条件为true继续执行,为false自动推退出循环

  3. 所以正常情况下do while会比while多执行一次内部代码,假设两个判断条件为false的情况

Loop

三种循环的区别

Loop conversion

  • Most loops can be converted to any other type of loop

    • What can't? (Well you could but it would be bad practice)

  • good exercise that can help you build good loop structurea

  • Remember the guidance for building a loop:

    • Initialize control variable(s)

    • Specify continuation condition

    • Specify loop body

    • Define update for control varaible

The Increment and Decrement Operators

  • ++ is the increment operator

    it adds one to a variable

    • val++, is the same as val = val + 1;

  • ++ can be used before (prefix) or after (postfix) a varaible

    • ++val:

    • val++;

  • -- is the decrement operator

    it subtracts one from a variable

    • val--, is the same as val = val - 1;

  • -- can be used before (prefix) or after (postfix) a varaible

    • --val:

    • val--;

Prefix vs. Postfix

  1. 当他们处在表达式中一定要注意,val++是先运算表达式,表达式处理结束后自己再加一,++val是先加一,再运算表达式。 a = (val++) + 1; a = (++val) + 1;

  2. ++val 相当于 val = val + 1 val++ 相当于 int tem;tem = val + 1; val = tem;

  3. 效率差别,不言而喻。

  • ++ and -- operators can be used in complex atatements and expressions

  • In prefix mode (++ val, -- val) the operator increments or decrements, then rturns the value of the variable

  • In post fix mode (val++, val--) the operator returns the value of the variable, then increments or decrements

Example:

int num, val = 12;
cout << val++; //display 12, val is now 13;
cout << ++val; //sets val to 14, then display it(14);
num = --val; //sets val to 13, stores 13 in num
num = val--; //store 13 in num, sets val to 12

Deciding Which Loop to Use

  • The while loop is a conditional pretest loop

    • Iterates as long as a certain condition exits

    • Validating input

    • Reading lists of data terminated by a sentinel

  • The do-while loop is a conditional posttest loop

    • Always iterates at least once

    • Repeating a menu

  • The for loop is a pretest loop

    • Built-in expressions for initializing, testing, and updating

    • Situations where the exact number of iterations is known

Last updated

Was this helpful?