repetition Flashcards
Four Steps Needed to Create a Loop
1) Choose the Loop Control Variable(s) (LCV)
2) Choose the loop category
3) Design the Loop
4) Choose the loop type (FOR loop or WHILE loop)
1) Choose the Loop Control Variable(s) (LCV)
- This is the variable(s) that will
the behavior
of the loop.
- The LCV must be part of the initialization, the logical
expression, and the update
- Ex. The loop control variable is ‘x’
2) Choose the loop category
- Loops fall into to general categories
a) Count-controlled loops are definite and will repeate
a definite number of times (Meaning you can determine
the number of iterations by looking at the code).
Each time you run a count-controlled loop, the number
of iterations will be the same.
b) Event-controlled loops are indefinite and the number of
repetitions is indefinite (Meaning you can’t determine
the number of iterations by looking at the code).
Each time you run an event-controlled loop, the number
of iterations can change.
3) Design the Loop
- The loop design has
four parts
a) Initialization - Where the loop starts (starting number or event)
Ex. int x = 0;
b) Logical Expression - How long the loop continues
Ex. (x <= 10)
c) Update - How to get from the start to the end of the loop
Ex. x = x + 1
d) Work (Statements) - What the loop does each iteration
Ex. cout «_space;“The value of x is: “ «_space;x «_space;endl;
For event-controlled loops
the Init and Update are usually
the same.
To update a variable (increment or decrement) you have
the following options:
a) ++ (post-increment) - This only increments by one
b) – (post-decrement) - This only decrements by one
c) x = x + ? (This is the normal way of incrementing a
variable by any value you choose by replacing
the ? with a number)
d) x = x - ? (This is the normal way of decrementing a
variable by any value you chooseby replacing
the ? with a number)
e) x+=? (This is the shorthand way of incrementing a
variable by any value you choose by replacing
the ? with a number)
f) x-=? (This is the shorthand way of decrementing a
variable by any value you chooseby replacing
the ? with a number)
- FOR Loop Syntax (Used mostly
for count-controlled loops) for(Initialization; Logical Expression; Update) { //Work - Statement(s) }
WHILE Loop Syntax (Used mostly
for event-controlled loops) //Initialization while(Logical Expression) { //Work - Statement(s)
//Update }
/Example #1: Count-controlled loop //Create a loop that will count from 50 to 150 by 5s and //display the value of the LCV each iteration.
//Step 1 - LCV int value = 0; //Declare our LCV
//Step 2 - Choose loop category //This a count-controlled loop
//Step 3 - Design the loop //Init => value = 50 //Log Exp => (value<= 150) //Updt => value = value + 5 OR value+=5 //Work => cout << "The LCV value is" << value << endl;
//Step 4 - Choose the loop type //Use a for loop for(value=50; (value<=150); value = value + 5) { cout << "The LCV value is: " << value << endl; }
//Example #2: Event-controlled loop //Create a loop that will display the user's entry //until the user enters 0.
//Step 1 - LCV string userEntry = ""; //Declare our LCV
//Step 2 - Choose loop category //This a count-controlled loop
//Step 3 - Design the loop //Init => Get the user's entry // cout << "Enter some text (Enter 0 to quit): "; // getline(cin, userEntry); //Log Exp => (userEntry != "0") //Updt => Get the user's entry (Same as the Init) // cout << "Enter some text (Enter 0 to quit): "; // getline(cin, userEntry); //Work => cout << "You entered: " << userEntry << endl;
//Step 4 - Choose the loop type //Use a while loop //Initialization cout << "Enter some text (Enter 0 to quit): "; getline(cin, userEntry);
while(userEntry != "0") //Logical Expression { //Work cout << "You entered: " << userEntry << endl;
//Update cout << "Enter some text (Enter 0 to quit): "; getline(cin, userEntry); }