HomeContact

How to use While Loop in Java

Published in Java
May 10, 2023
1 min read

Among the repetitive statements used in Java, while statements and do-while statements are introduced.

while (condition) {
Code to execute if the condition is true
}

🌱Sample code

public static void main(String[] args) {
int i = 0;
while (i <= 3) {
System.out.println(i); // 0, 1, 2, 3
i++;
}
}


do {
Code to execute once in advance before condition determination and continue to execute once condition is true.
}
while(condition);

🌱do-while statements

public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i++;
}
while (i <= 3);
}

Tags

#Java#java.lang

Share


Previous Article
Using Array in Java

Topics

Java
Other
Server

Related Posts

How to use BigInteger in Java - 2
May 31, 2023
1 min