Loops
The concept of loops arises in need of executing a specific block of code iteratively.
Suppose I want to print Hello, World!
5 times. Instead of using print method multiple times we can use a loop to reduce the lines of codes.
In java there are multiple types of loops.
- while loop
- do...while loop
- for loop
- for...each loop
while loop
while (condition) {...}
After using while
statement, it takes a boolean
value as condition. As long as the condition is true
the java compiler will repeat the lines of codes inside the code block. For example,
// Do not execute this code
public class Loops {
public static void main(String[] args) {
//while loop
while (true) {
System.out.println("Hello, World!");
}
}
}
Have you identified the issue that might arise in this code?
Because we are using true
inside the (condition)
block, the java compiler will repeat the code inside the block infinite amount of times and eventually the code will crash. Can you think of any solution that we can use to solve this issue?
We can use a variable to store the the boolean
value and once we are done with our repetition, we can simply assign the variable to false
value. Also, to check whether we are done with our repetition, we can use condition statements.
Now, to print Hello, World!
5 times we can write the following code.
public class Loops {
public static void main(String[] args) {
int iteration = 1;
boolean flag = true;
//while loop
while (flag) {
System.out.println("Hello, World!");
iteration++;
if (iteration > 5) {
flag = false;
}
}
System.out.println("End");
}
}
Here, multiple things are happening. First we assigned an int
value to keep track how many times we want to repeat the code. And we stored the boolean
value true
initially.
Now, in every iteration first we are printing Hello, World!
. Second, we are increasing the iteration value to keep track of the times we want to print Hello, World!
. Third, we are checking whether we are done with our repetition. Once we are done, we are assigning the boolean
value to false
. When while loop condition is false
it will skip the block of code and go to the next line.
Output
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
End
This is how the code is working. We can make this code much more simpler in the following way,
public class Loops {
public static void main(String[] args) {
int iteration = 1;
while (iteration <= 5) {
System.out.println("Hello, World!");
iteration++;
}
System.out.println("End");
}
}
Here we don't need to store a boolean
value manually. We know that relational operators give us boolean
value. So here we are checking whether the condition is true
or false
. When the condition is false
, the loop will terminate. This is how the iteration is working,
Iteration | variable | (iteration <= 5) | Action |
---|---|---|---|
1st | iteration = 1 | true | prints Hello, World! iteration += 1 |
2nd | iteration = 2 | true | prints Hello, World! iteration += 1 |
3rd | iteration = 3 | true | prints Hello, World! iteration += 1 |
4th | iteration = 4 | true | prints Hello, World! iteration += 1 |
5th | iteration = 5 | true | prints Hello, World! iteration += 1 |
6th | iteration = 6 | false | goes to the next line prints End
|
do...while loop
do {...} while (condition);
This works same as while loop. However, here first the code in the do
block executes, then checks the condition and repeat accordingly. If we want to print numbers 1 to 5 we can write the following code,
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
Output
1
2
3
4
5
Find out the different use cases of while
loop and do...while
loop.
Happy Coding
Top comments (0)