loops Flashcards

1
Q

Counter

A

A counter is a variable that is incremented or decremented each time a loop repeats

can be used to control execution of the loop (also known as the loop control variable

Must be initialized before entering loop

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

The do -while loop

A

do-while: a posttest loop - execute the loop, then test the expression

General Formal:
do
statement; // or block in { }
while (expression) ;

Note that a semicolon is required after
(expression)

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

notes on do-while loops

A

Loop always executes at least one

Execution continues as long as expression is true, stops repetition when expression becomes false

Useful in menu-driven programs to bring user back to menu to make another choice

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

The for Loop

A

A loop commonly must iterate a specific number of times, such as 10 times. Though achievable with a while loop, that situation is so common that a special kind of loop exists. A for loop is a loop with three parts at the top: a loop variable initialization, a loop expression, and a loop variable update. A for loop describes iterating a specific number of times more naturally than a while loop.

Useful for counter-controlled loop

General Format:

for (initialization; test; update)
statement; // or block in { }

No semicolon after the update expression or after the )

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

for Loop - mechanics
for(initialization; test; update)
statement; // or block in { }

A

1) Perform initialization
2) Evaluate test expression
-If true, execute statement
If false, terminate look execution
3) Execute update, then re-evaluate test expression

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

When to use the for Loop

A

In any situation that clearly requires
an initialization
a false condition to stop the loop
an update to occur at the end of each iteration

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

The for Loop is a Pretest Loop

A

The for loop tests its test expression before each iteration, so it is a pretest loop

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

Each time through a loop’s statements is called an

A

iteration.

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

A while loop is a program construct

A

that repeatedly executes a list of sub-statements (known as the loop body) while the loop’s expression evaluates to true. Each execution of the loop body is called an iteration. Once entering the loop body, execution continues to the body’s end, even if the expression would become false midway through.

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

What is the increment operator?

A

The statement i = i + 1 is so common that the language supports the shorthand ++i, with ++ known as the increment operator.

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

What is the decrement operator?

A

– is the decrement operator, –i means i = i - 1).

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

Actually two increment operators exist: ++i (pre-increment) and i++ (post-increment).

A

++i increments before evaluating to a value, while i++ increments after. Ex: If i is 5, outputting ++i outputs 6, while outputting i++ outputs 5 (and then i becomes 6). This material primarily uses ++i for simplicity and safety, although many programmers use i++, especially in for loops.

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

Choosing among for and while loops

A

Generally, a programmer uses a for loop when the number of iterations is known (like loop 5 times, or loop numItems times), and a while loop otherwise.

for Number of iterations is computable before the loop, like iterating N times.
while Number of iterations is not (easily) computable before the loop, like iterating until the input is ‘q’.

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