JAVA LOOPS
Java for loop
for loop is useful when you know the number of repetitions in advance or want to update a counter consistently. All loop control information appears on one line.
for loop syntax
for (inicializavimas; sąlyga; pakeitimas) {
// Repeated code block
}It simply shows the three places for the actual counter, condition and update.
| Part | When does it run? | Purpose |
|---|---|---|
| Initialization | Once before the loop | Creates or sets the counter |
| Condition | Before each iteration | Decides whether to run the block |
| Update | After each iteration | Changes the counter's value |
- Initialization prepares the counter. For example,
int diena = 1. - Condition asks before each repetition whether the loop can continue.
- If the condition is
true, Java runs the code inside the braces. - Update updates the counter, then the condition is checked again.
Counting four days
Imagine we want to number four days:
for (int diena = 1; diena <= 4; diena++) {
System.out.println("Diena: " + diena);
}Diena: 1
Diena: 2
Diena: 3
Diena: 4
- Before the loop starts, a counter is created:
diena, whose initial value is1. - Java checks
diena <= 4. The first check is1 <= 4, so the condition is true. - The current day value is displayed in the console.
diena++adds one, so the counter's next value is2.- This repeats until day
4. When the counter becomes5, the condition5 <= 4is false and the loop ends.
The counter can change by more than one
This loop lists the even house numbers on one side of a street:
for (int namoNumeris = 2; namoNumeris <= 10; namoNumeris += 2) {
System.out.println("Namo numeris: " + namoNumeris);
}Namo numeris: 2
Namo numeris: 4
Namo numeris: 6
Namo numeris: 8
Namo numeris: 10
- The counter
namoNumerisstarts at the first even number,2. - After each number is displayed, the statement
namoNumeris += 2adds two. - The counter therefore moves as follows:
2, 4, 6, 8, 10and skips the odd numbers. - After
10the counter becomes12. The condition12 <= 10is false, so the loop stops.
Accumulating a result in a loop
Ona reads 10 pages of a book each day for five days. A loop can calculate the total:
int perskaitytaPuslapiu = 0;
for (int diena = 1; diena <= 5; diena++) {
perskaitytaPuslapiu += 10;
}
System.out.println("Perskaityta puslapių: " + perskaitytaPuslapiu);Perskaityta puslapių: 50
- Initially,
perskaitytaPuslapiuis0, because Ona has not started reading yet. - The loop runs five times, once for each day.
- On each iteration,
+= 10adds another 10 pages to the previous result. - The value grows as follows:
10, 20, 30, 40, 50. - When the loop ends,
println()displays the final accumulated value, 50.
A decreasing counter
Jonas needs to carry four shopping bags from the car. The counter can decrease using --:
for (int maiseliai = 4; maiseliai >= 1; maiseliai--) {
System.out.println("Automobilyje liko maišelių: " + maiseliai);
}
System.out.println("Visi pirkiniai parnešti");Automobilyje liko maišelių: 4
Automobilyje liko maišelių: 3
Automobilyje liko maišelių: 2
Automobilyje liko maišelių: 1
Visi pirkiniai parnešti
- The counter's initial value is
4, because there are four bags in the car. - After each iteration,
maiseliai--reduces the counter by one. - The console displays these values in order:
4, 3, 2, 1. - When the counter becomes
0, the conditionmaiseliai >= 1becomes false and the loop ends. - Java continues after the loop and displays the text Visi pirkiniai parnešti.
When the condition is false from the start
The program should water the flowers only on days one through five, but the counter incorrectly starts at eight:
for (int diena = 8; diena <= 5; diena++) {
System.out.println("Laistome gėles");
}Nieko neišvedama
- The counter
dienaimmediately receives the value8. - Before the first iteration, Java checks the condition
8 <= 5. - The answer is
false, so the loop'sprintln()never runs. - The console therefore remains empty. This is not an error: the loop simply had no valid iterations.
for or while?
Choose forwhen repetitions are naturally controlled by a counter or their number is known. while is usually clearer when the number of repetitions is unknown and the loop depends on a changing program state.