08 May Loops in Java
Loops in Java, includes the following,
- while loop
- do-while loop
- for loop
Let us learn about the loops one by one,
while loop
If the condition is true, a statement or a group of statements execute in while loop. As long as the condition/ expression is true, the statement in the body executes.
If the condition is false, the body of the loop will not execute.
Syntax
Here, expression is a condition or a Boolean Expression,
1 2 3 4 5 |
while(expression) { // statements execute if the expression is true } |
Example
Let us see an example, wherein we learn about the usage of while loop,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class StudyopediaDemo { public static void main(String args[]) { System.out.println("'While Loop' in Java - Studyopedia"); int i = 50; System.out.println("Displaying Values!"); // while loop condition while( i < 55 ) { System.out.println(i); i++; } } } |
Output
The following is the output,
do-while loop
The do-while loop executes at least once, even if the condition/ expression is false, unlike while loop.
If the condition is true, a statement or a group of statements execute in do-while loop
If the condition is false, the body of the loop executes at least once.
Syntax
Here, expression is a boolean expression or condition,
1 2 3 4 5 |
do { // statement to execute } while(expression); |
Example
Let us see an example, wherein we learn about the usage of do-while loop,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class StudyopediaDemo { public static void main(String args[]) { System.out.println("'do-while loop' in Java - Studyopedia"); int i = 50; System.out.println("Displaying Values!"); // do-while loop condition do { System.out.println(i); i++; }while( i < 55 ); } } |
Output
The following is the output,
Difference between while and do-while loop
If the condition is false in a while loop, the body of the loop never executes.
If the condition is false in a do-while loop, the body of the loop executes at least once.
The conditional expression is at the top of the while loop.
The conditional expression is at the bottom of the do-while loop.
Now let us learn about for loop in Java.
for-loop
The for loop is a loop that executes a specified number of times. Under this, initialize, test, and increment loop counter in a single line, unlike while and do-while loop.
Syntax
1 2 3 4 5 |
for(initialization; expression; inc/ dec) { // statements execute } |
Here,
- initialization: declare and initialize any loop control variable
- expression: a Boolean expression if true, the statement is executed
- inc/ dec: increment/ decrement loop
Example
Let us see an example, wherein we learn about the usage of for-loop,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class StudyopediaDemo { public static void main(String args[]) { System.out.println("'for loop' in Java - Studyopedia"); int i; for (i = 0; i < 5; i++) { System.out.println(i); } } } |
Output
The following is the output,
In this lesson, we learned about loops in Java, while, do-while and for loop. In the next lesson we will learn about break and continue statements in Java.
No Comments