Looping & Random Numbers Flashcards
A structure that allows repeated execution of a block of statements.
Loop
What must be evaluated before the loop body executes?
A Boolean Expression
In order for the loop body to execute, a Boolean expression must be evaluated to be what?
The Boolean expression must be true.
A block of statements. It can be a single statement, or a block of statements between curly braces.
Loop Body
What are the two categories of loops?
- Conditional Loop
- Count-controlled Loop
This category of loops executes as long as a particular condition exists.
Conditional Loop
This category of loops repeats a specific number of times.
Count-controlled Loop
What are the three elements of a count-controlled loop?
- Initialization - Initialize a control variable to a starting value.
- Increment/Decrement - Update the control variable during each iteration.
- Condition - Test the control variable by comparing it to a maximum value. When the control variable reaches its maximum value, the loop terminates.
What is the ++ operator called?
Increment
What is the – called?
Decrement
What do the increment and decrement operators do?
They add and subtract one from their operands.
What does it mean to increment a value?
To increase the value by one.
What does it mean to decrement a value?
To decrease the value by one.
x = 6;
System.out.print(x++);
What would the output be? What would the new value of x be?
Output: 6
New Value of x: 7
x = 6;
System.out.print(++x);
What would the output be? What would the new value of x be?
Output: 7
New value of x: 7
Used to iterate a part of the program repeatedly until the specified Boolean condition is true. As soon as the Boolean condition becomes false, the loop automatically stops.
While Loop
This loop is also considered as a repeating if statement.
While Loop
What are the two important parts of a while loop?
- A boolean expression that is tested for a true or false value
- A statement or block of statements that is repeated as long as the expression is true.
This loop is known as a pretest loop, which means it tests its expression before each iteration.
While Loop
What would the output be?
int number = 6;
while (number <= 5) {
System.out.println(“Hello”);
number++;
}
This program would have no output since the condition is false.