repetition Flashcards

1
Q

Four Steps Needed to Create a Loop

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

1) Choose the Loop Control Variable(s) (LCV)

- This is the variable(s) that will

A

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’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

2) Choose the loop category

- Loops fall into to general categories

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

3) Design the Loop

- The loop design has

A

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 &laquo_space;“The value of x is: “ &laquo_space;x &laquo_space;endl;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

For event-controlled loops

A

the Init and Update are usually

the same.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

To update a variable (increment or decrement) you have

the following options:

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  • FOR Loop Syntax (Used mostly
A
for count-controlled loops)
			for(Initialization; Logical Expression; Update)
			{
				//Work - Statement(s)		
			}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

WHILE Loop Syntax (Used mostly

A
for event-controlled loops)
			//Initialization
			while(Logical Expression)
			{
				//Work - Statement(s)
				//Update
			}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
/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.
A
//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;
	 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
//Example #2: Event-controlled loop
	 //Create a loop that will display the user's entry
	 //until the user enters 0.
A
//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);
	 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly