Module 3 (Prelim) Flashcards
3.1 Overview 3.2 Loop Structure 3.3 while Loop 3.4 Increment and Decrement Operators 3.5 for Loop 3.6 do...while Loop 3.7 Nested Loops 3.8 break and continue Statements
A structure that allows repeated execution of a block of statements
Loop
A block of statements that are executed repeatedly
Loop body
One execution of any loop
Iteration
Three types of loops
while
for
do…while
The loop-controlling Boolean expression is the first statement
while
A concise format in which to execute loops
for
The loop-controlling Boolean expression is the last statement
do…while
Executes a body of statements continually. As long as the Boolean expression that controls entry into the loop continues to be true
while loop
while loop syntax:
while (expression)
….statement
Performs a task a predetermined number of times
Definite loop or counter-controlled loop
Definite loop or counter-controlled loop is also called _________
counted loop
Sentinel variable is tested in the condition. Loop ends when sentinel is encountered
Indefinite or Sentinel-controlled while loops
A loop that never ends. Can result from a mistake in the while loop
Infinite loop
Suspect an infinite loop when:
-The same output is displayed repeatedly
-The screen remains idle for an extended period of time
To exit an infinite loop:
- press and hold Ctrl + C
- Break
is an operator used for one value
unary operators
- The result is calculated and stored
- Then the variable is used
Prefix ++
- The variable is used
- Then the result is calculated and stored
Postfix ++
Prefix and postfix increment operators
++someValue and someValue++
Prefix and postfix decrement operators
–someValue and someValue–
The expression (x++) simply means______
A) add 1 to x and return the old value (Postfix increment)
B) add 1 to x and return the new value (Prefix increment)
C) take 1 from x and return the old value (Postfix decrement)
D) take 1 from x and return the new value (Prefix decrement)
A) add 1 to x and return the old value
The expression (++x) simply means______
A) add 1 to x and return the old value (Postfix increment)
B) add 1 to x and return the new value (Prefix increment)
C) take 1 from x and return the old value (Postfix decrement)
D) take 1 from x and return the new value (Prefix decrement)
B) add 1 to x and return the new value
The expression (–x) simply means______
A) add 1 to x and return the old value (Postfix increment)
B) add 1 to x and return the new value (Prefix increment)
C) take 1 from x and return the old value (Postfix decrement)
D) take 1 from x and return the new value (Prefix decrement)
D) take 1 from x and return the new value
The expression (x–) simply means______
A) add 1 to x and return the old value (Postfix increment)
B) add 1 to x and return the new value (Prefix increment)
C) take 1 from x and return the old value (Postfix decrement)
D) take 1 from x and return the new value (Prefix decrement)
C) take 1 from x and return the old value
Used when a definite number of loop iterations is required
for Loop
for Loop syntax:
for (initial statement; loop condition; update statement)
statement
The initial statement, loop condition, and update statement are called ________
for loop control statements
Other uses for the three sections of a for loop:
- Initialization of more than one variable
- Performance of more than one test using AND or OR operators
- Decrementing or performance of some other task
- Altering more than one value
You can leave one or more portions of a for loop empty
(True or False)
T
Two semicolons are still required as placeholders
For loop:
- To pause a program: (2)
- Use do-nothing loop (ex. for(x = 0; x < 100000; ++x);)
- use the built-in sleep() method
is a posttest loop
do…while loop
The statement executes first, and then the expression is evaluated. As long as expression is true, loop continues. Loop always iterates at least once
do…while Loop
do…while Loop:
To avoid an infinite loop, body must contain a statement that makes the expression ________
false
do…while Loop syntax:
do
{
statement
}
while (expression)
An inner loop must be entirely contained in an outer loop
(True or False)
T
______and ______ alter the flow of control
break
continue
break statement is used for two purposes:
- To exit early from a loop
- To skip the remainder of a switch structure
break and continue Statements:
After ______ executes, the program continues with the first statement after the structure
break
continue is used in while, for, and do…while structures
(True or False)
T
When __________ is executed in a loop, It skips remaining statements and proceeds with the next iteration of the loop
break and continue