Articles → Java → Loops In Java
Loops In Java
What Are Loops?
- A structure, series, or process, the end of which is connected to the beginning.
- A shape produced by a curve that bends round and crosses itself.
For Loop
Syntax
for (initialize counter; test counter; increment counter) {
code to be executed;
}
Example
class MainClass {
public static void main(String args[]) {
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}
Output
Click to Enlarge
While Loop
Syntax
while (condition) {
// code block
}
Example
package com.TestProject;
public class TestClass {
public static void main(String[] args) {
int i = 0;
while (i != 5) {
System.out.println(i);
i++;
}
}
}
Output
Click to Enlarge
Do While Loop
Syntax
do {
// Code block that will be executed atleast once
} while (condition);
Example
public class TestClass {
public static void main(String[] args) {
int i = 0;
do {
// Code block that will be executed atleast once
System.out.println(i);
} while (i != 0);
}
}
Output
Click to Enlarge