Our code right now is growing in terms of lines of code ~100 lines of code and 30-40 % of that code is repetition, so we need a mechanism that allows us to “repeat” several statements.
Java provides two types of loop
There are two types of loops:
Pre-test: you check for a true statement BEFORE performing an action
Post-test: you check for a true statement AFTER performing an action
Regardless of the type of loops, we need to understand that there are four elements that every loop shall have.
Elements of a loop
All loops contain the following four components:
1. Initialization of the control variable. This variable will keep track of the times/state of the mechanism to keep the loop running
2. A Boolean condition, a.k.a., test/check. This condition will “check” if the Boolean statements remain true; if that is true, then the loop will continue; otherwise (i.e., false), the loop will stop. Remember that we can use relational operators to extract a Boolean expression.
3. The body of the loop. This is the set of commands that are redundant in a code and we wish to repeat in the loop.
4. The update of the control variable. The update can be by incrementing/decrementing the control variable in order to accomplish the Boolean condition of the check. The control variable works as an accumulator that will keep track of the current value before reaching a Boolean condition.
It is common to have different ways to update your control variable. For example,
counter = counter + 1;
This means that for the old counter (to the right side of the equals) you add 1 to it, and then assign the new counter. There is also a shortcut to do this:
counter += 1;
This way counter will be updated by the old counter plus 1.
There is another shortcut for this particular update using the ++ operators. Notice that the ++ operator will only modify the control variable by one. We can use counter++; to have the same result.
Shortcut:
counter = counter + 1;
counter++;
counter += 1;
Ask someone to say the word “hello”:
3 times
5 times
7 times
13 times
What did you think when you needed to count up to 13?
Repeating “hello” 3 times was probably not a big deal. Maybe 5 not too much. But 7 or maybe 13 will require some kind of counting either with your fingers or with something else.
Your fingers become sort of a counter, and now you could potentially think:
If the counter == 13, then I will stop repeating the word “hello”
As long as the counter <= 13, I will repeat the word “hello.”
As long the counter != 13, I will repeat the word “hello,” then I will stop
Once I arrive to counter == 14, I will not say “hello” and stop
Types of Loops
Java supports two types of loops: pre-test and post-test. The pre-test loops are:
The while loop
The for loop
The pre-test checks if the test of the loop is true in order to execute the body of a loop. Otherwise, the loop will not be executed.
The post-order loop is:
do-while
The post-test performs an action as part of the body, and then it checks if the condition is true.
The while loop is a pre-test loop that will allow us to repeat certain statements in the following format:
// (1)
while( (2) ){
... (3)
... (4)
}
Example 1: Write a program that prints the string “hello” five times.
1. int counter = 0; //(1)
2. while(counter < 0) //(2)
3. {
4. System.out.println("hello"); //(3)
5. counter = counter + 2; //(4) counter++;
6. }
The for loop is equivalent to the while loop; both of them are pre-test loops. The difference is that the for-loop uses a compact way to store information and has a different execution order.
The for loop is a more compressed way to use a loop. It allows us to perform exactly the same as the while loop. The format of the for-loop is as follows:
for((1) ; (2); (4)){
(3)
}
The order of execution of the for-loop is the initialization, then the test, followed by the body, and finally the update. After that, we repeat (2), and in case the test is true, then (3) and (4) are run. In case step (2) is false, that indicates to get out of the loop execution.
You can get away with not incorporating the {} IF there is only one statement in the body of the loop.
Example 1: Write a program that prints the string “hello” five times.
// (1) (2) (4)
for(int counter = 0; counter < 0; counter++){
System.out.println("hello"); // (3)
}
The do-while loop is a post-fix loop that allows us to perform some repetitive action and then check if a condition is held. The format of the do-while loop is as follows:
// (1)
do{
// (3)
// (4)
}while((2));
Example 1: Write a program that prints the string “hello” five times.
int counter = 0; // (1)
do{
System.out.println("hello"); // (3)
counter++; // (4)
}while(counter < 0);
AP CS A
[while Loop]
CON-2.H Compute statement execution counts and informal run-time comparison of iterative statements.
CON-2.H.1 A statement execution count indicates the number of times a statement is executed by the program.
[do-while Loop]
CON-2.C.1 Iteration statements change the flow of control by repeating a set of statements zero or more times until a condition is met.
[Random Numbers]
CON-1.D.4 The values returned from Math.random can be manipulated to produce a random int or double in a defined range
Explain the importance of algorithms in the problem-solving process.
Demonstrate how a problem may be solved by multiple algorithms, each with different properties.
Read a given program, and explain what it does
Trace the flow of control during the execution of a program (both correct and incorrect).
Use appropriate terminology to identity elements of a program (e.g., identifier, operator, operand)
Reading and explaining code
Basic concepts such as variables, primitive data types, expression evaluation, assignment, etc.