Posted by
Zhullyblog
on
- Get link
- X
- Other Apps
In this tutorial, you would learn the use of while and do while loop in Java
WHILE LOOP
The java while loop is used to run a code repeatedly until a certain condition is met. This is used basically to test the code within the parenthesis and once the condition is met, it gives result.
SYNTAX
while ( condition ) {
//Code;
//Increment / decrement;
}
Explanation
• The parenthesis will contain the condition of the code
• Once the condition is met, the body of the loop will be executed.
Let's take an example in Java
//by Zhullyblog
public class sample{
public static void main(String[] args) {
int x = 1;
while ( x <20 ){
System.out.println("The value of x is : " + x );
x++;
}
}
}
Screenshot
The code above is just a simple test that shows how Java while loop works.
DO....WHILE LOOP
The do...while loop is quite similar to while loop but in this case, the body of the loop is executed before the condition is tested.
SYNTAX
do {
//Statement ;
//increment /decrement;
} while ( condition );
Example
public class sample{
public static void main(String args[]){
int x =1;
do{
System.out.println(x);
x++;
}while(x<10);
}
}
Screenshot
Let's move
Introduction to Java programming
How to declare a variable in Java
If, if..else statement in Java
Java Object Oriented Programming
Constructors in Java programming
Please share
Comments
Post a Comment