09 — Core Java for complete beginners.
One of the cool things about programming is that you can ask the computer to do things over and over again without having to write the same code multiple times. This is where loops come in handy.
Imagine you want to print your name four times. You could just write System.out.println("YourName")
four times, like this.
public class Main {
public static void main(String[] args) {
System.out.println("Dinesh");
System.out.println("Dinesh");
System.out.println("Dinesh");
System.out.println("Dinesh");
}
}
And the output would be.
Dinesh
Dinesh
Dinesh
Dinesh
But what if you needed to print your name 100 times? Writing System.out.println("Dinesh")
100 times would be a nightmare. This is where loops come to the rescue.
Types of Loops in Java.
In Java, there are different types of loops: while
, do-while
, and for
loops. Each has its own use cases, but let's start with the while
loop.
The while
Loop.
A while
loop execute the block of code repeatedly as long as a specified condition is true. Here's the basic structure.
while (condition) {
// code to be executed
}
Condition: A boolean expression that is checked before each iteration.
Statements: The code that runs as long as the condition is true.
Let’s say you want to print your name followed by a number from 1 to 4. Here’s how you can do it with a while
loop.
public class Main {
public static void main(String[] args) {
int i = 1; // Initialization
while (i <= 4) { // Condition
System.out.println("Dinesh " + i); // Code to be executed
i++; // Update (increment)
}
}
}
Explanation:
- Initialization: The variable
i
starts at 1. - Condition: The loop runs as long as
i
is less than or equal to 4. - Execution:
System.out.println("Dinesh " + i)
prints "Dinesh" followed by the current value ofi
. - Update:
i
is incremented by 1 after each iteration.
The loop continues until i
becomes 5, at i=5
the condition i <= 4
is false, and the loop stops.
Example: check below code carefully.
public class Main {
public static void main(String[] args) {
int i = 1;
while (i <= 4) {
System.out.println("Dinesh " + i);
i++;
}
System.out.println("Dinesh " + i);
}
}
Dinesh 1
Dinesh 2
Dinesh 3
Dinesh 4
Dinesh 5
Notice how Dinesh 5
is printed after the loop ends because the System.out.println("Dinesh " + i);
line runs outside the loop.
Nested while
Loop.
What if you need to print “Hi” after each “Dinesh”? You can use a nested while
loop.
public class Main {
public static void main(String[] args) {
int i = 1;
while (i <= 2) {
System.out.println("Dinesh " + i);
int j = 1;
while (j <= 2) {
System.out.println("Hi");
j++;
}
i++;
}
}
}
Output:
Dinesh 1
Hi
Hi
Dinesh 2
Hi
Hi
The do-while
Loop.
A do-while
loop is similar to a while
loop, but it guarantees the code block runs at least once because the condition is checked after the code executes. Here's the basic structure.
do {
// code to be executed
} while (condition);
Let’s look at an example.
public class Main {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Dinesh " + i);
i++;
} while (i <= 3);
}
}
Output.
Dinesh 1
Dinesh 2
Dinesh 3
If i
is initialized to 5.
public class Main {
public static void main(String[] args) {
int i = 5;
do {
System.out.println("Dinesh " + i);
i++;
} while (i <= 3);
}
}
Output.
Dinesh 5
The loop runs once even though the condition i <= 3
is false because the check happens after the first execution. That is the reson to have Dinesh 5 as the output.
The for
Loop.
The for
loop is like a compact version of the while
loop, bundling initialization, condition, and update in one line. Here’s the structure.
for (initialization; condition; update) {
// code to be executed
}
Here’s a simple example to print numbers from 0 to 4.
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}
Output.
0
1
2
3
4
You can also count downwards.
public class Main {
public static void main(String[] args) {
for (int i = 4; i > 0; i--) {
System.out.println(i);
}
}
}
Output.
4
3
2
1
Choosing the Right Loop
Now that we’ve covered while
, do-while
, and for
loops, you might wonder which one to use. It depends on the situation:
- Use a
for
loop when you know the number of iterations in advance. - Use a
while
loop when the number of iterations is not known and depends on a condition. - Use a
do-while
loop when you need the code to run at least once, regardless of the condition.
But in the practical world, most of the time you need to use for-loop and rarely you will need to use while-loop. In my experience, it is very very rare to use do-while loop.
Happy coding! If you have any questions, feel free to ask in the comments. 😊