Java Tutorial

What is Java


Control Statements


Java Object Class


Java Inheritance


Java Polymorphism


Java Abstraction


Java Encapsulation


Java Array

Java OOPs Misc


Java While Loop

The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.
The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:

Syntax:


while (expression)
{
statement(s)
}


Example:


package com.ashu;
class Java64bit {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}


Output: Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

Java Infinitive While Loop

If you pass true in the while loop, it will be infinitive while loop.
You can implement an infinite loop using the while statement as follows:

Syntax:


while (true){
// your code goes here
}


Example:


package com.ashu;
class Java64bit {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}


Output: execute infinite time
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
............

Advertisement

Copyright 2017 Design& Development