if
statement (not a loop)
int n = 1;
if(n <= 5)
{
System.out.println(n);
n++;
}
System.out.println("n outside: " + n);
The code segment prints:
1
n outside: 2
The condition for an if
statement is checked when the statement is reached. If the condition evaluates to true
, the code inside the body of the statement runs once.
while
statement (a loop)
int n = 1;
while(n <= 5)
{
System.out.println(n);
n++;
}
System.out.println("n outside: " + n);
The code segment prints:
1
2
3
4
5
n outside: 6
The only change from the earlier code segment is that if
has been replaced with while
.
The condition for a while
loop is checked before the first execution, the same as an if
statement. If the condition evaluates to true
, the body of the loop executes (also the same as an if
statement`).
When the body finishes executing, the condition is checked again. If the condition evaluates to true
again, the body executes again. The process repeats until:
- The condition is checked and evaluates to
false
or - something else causes the loop to end, such as execution of a
return
statement.
while
loop termination
A while
loop terminates when the condition is checked and evalutes to false
, not as soon as the condition would evaluate to false
. The example below shows the difference.
int n = 1;
while(n <= 5)
{
n++;
System.out.println(n);
}
System.out.println("n outside: " + n);
The code segment prints:
2
3
4
5
6
n outside: 6
The value of n
becomes 6
during the last execution. This does not immediately cause the loop to terminate. The value 6
is printed before the condition is checked again.
for
loops
Structure & execution order
for(initialization; condition; update)
{
body
}
The initialization statement runs once.
The condition is checked before each run of the body, including before the first run. If the condition evaluates to true
, the body is executed.
The update statement is executed after each execution of the body (before the condition is checked again).
Example:
initialization
condition (true)
body
update
condition (true)
body
update
condition (false)
The initialization statement typically declares and initializes a variable that is used in the condition and update statements (and often in the body).
The condition must be a boolean expression. The condition often checks the variable declared in the initialization statement against a fixed value.
The update statement typically changes the value of the variable declared in the initialization statement.
Java does not check that the initialization and update statements follow the structure above; however, loops that do not follow the structure are typically better written as while
loops.
for
loop example
for(int n = 1; n <= 5; n++)
{
System.out.println(n);
}
// n does not exist here.
The code segment prints:
1
2
3
4
5
The condition (n <= 5
) is checked before the first execution. The update statement (n++
) is executed after each execution of the body. The first value printed is 1
. The last value printed is 5
.
The scope of n
is the loop itself. Variables declared in the initialization statement do not exist outside the loop. (It is possible to declare the variable used in a for
loop outside the loop itself; however, this generally indicates that the loop should be a while
loop.)
Enhanced for
loops
Enhanced for loops can be used to loop through arrays, lists, and some other data structures.
Choosing a for
loop or a while
loop
Known number of executions
A for
loop is often easier to write and to read (than a while
loop) when the number of executions is known. Examples include:
- Looping a fixed number of times (ex: 10)
- Looping through a fixed range of values (ex: multiples of 3 from 3 up to and including 300)
- Looping through a String
- Traversing an array or list
Execution based on a condition
A while
loop is typically used whenever a for
loop is not appropriate. Examples include:
- Looping while input remains
- Looping while a user wishes to perform a task again
- Input validation
- Adding to or removing from an ArrayList within a loop
- Varying the update of the control variable based on conditions within the loop body
Help & comments
Get help from AP CS Tutor Brandon Horn