While statement use for iteration until provided expression is true.
While loop contains expression , check for expression is true or false, if expression is true then Code executed inside while loop until expression will not false.
while (booleanExpression) {
// You Code Here....
}
Below example showing Very basic example code for while loop.
public class While_For_Loop_Statement {
public static void main(String[] args) {
int a = 1;
// each iteration while check (a < 3)
// if find out (a < 3) is true execute inner code
while (a < 3) {
System.out.println("a = "+a);
a++; // In each iteration increase a variable value to 1
}
} // Main close
}
a = 1 a = 2
Below example is an infinite loop
public class While_For_Loop_Statement {
public static void main(String[] args) {
while (true) {
System.out.println(" infinite loop ");
}
} // Main close
}
We can use break statement to break while loop iteration, After break statement execution will out from while loop
public class While_For_Loop_Statement {
public static void main(String[] args) {
int a = 1;
while (a < 10) {
System.out.println("a = "+a);
if(a==6)
break;
a++; // In each iteration increase a variable value to 1
}
} // Main close
}
a = 1 a = 2 a = 3 a = 4 a = 5 a = 6
Below example is calculating sum of 10 numbers
public class While_For_Loop_Statement {
public static void main(String[] args) {
int a = 1;
int NumberSum = 0;
while (a <= 10) {
// Add current a variable value to NumberSum
NumberSum = NumberSum + a;
a++; // In each iteration increase a variable value to 1
}
System.out.println("Sum of numbers = "+NumberSum);
} // Main close
}
Sum of numbers = 55
Do While statement also use for iteration until provided expression is true.
Do While loop contains expression , Do While loop firstly runs code once then
check for expression is true or false, if expression is true
then Code executed again until expression will not false.
do{
// You Code Here....
}while (booleanExpression);
Below code show do-while execute at least once.
public class While_For_Loop_Statement {
public static void main(String[] args) {
int x = 1;
do {
System.out.println("x = "+x);
x++;
} while (x > 2); // expression false
} // Main close
}
x = 1
Below example is calculating sum of 10 numbers using do-while loop
public class While_For_Loop_Statement {
public static void main(String[] args) {
int a = 1;
int NumberSum = 0;
do {
// Add current a variable value to NumberSum
NumberSum = NumberSum + a;
a++; // In each iteration increase a variable value to 1
}while (a <= 10);
System.out.println("Sum of numbers = "+NumberSum);
} // Main close
}
Sum of numbers = 55
For loop statement use for iteration until provided expression is true.
For loop contains expression , check for expression is true or false, if expression is true
then Code executed inside for loop until expression will not false.
for ( initialize variable ; booleanExpression ; update initialize variable) {
// You Code Here....
*
}
initialize variable : initilize one or many variables. Ex. int i=0
booleanExpression : i < 5
update initialize variable : update one or many variables. Ex. i++
See Below for loop examples
public class While_For_Loop_Statement {
public static void main(String[] args) {
for (int a = 0; a < 5; a++) {
System.out.println( "a = " + a);
}
} // Main close
}
a = 0
a = 1
a = 2
a = 3
a = 4
Taking example for use of || operator.
public class While_For_Loop_Statement {
public static void main(String[] args) {
int a = 0;
for ( ; a < 5; a++) {
System.out.println( "a = " + a);
}
} // Main close
}
a = 0
a = 1
a = 2
a = 3
a = 4
public class While_For_Loop_Statement {
public static void main(String[] args) {
int a = 0;
for ( ; a < 5 ; ) {
System.out.println( "a = " + a);
a++;
}
} // Main close
}
a = 0
a = 1
a = 2
a = 3
a = 4
public class While_For_Loop_Statement {
public static void main(String[] args) {
int a = 0;
for ( ; ; ) {
System.out.println( "a = " + a);
if(a > 4)
break;
a++;
}
} // Main close
}
a = 0
a = 1
a = 2
a = 3
a = 4
Below example is calculating sum of 10 numbers
public class While_For_Loop_Statement {
public static void main(String[] args) {
int NumberSum = 0;
for ( int a=1 ; a <= 10 ; a++) {
// Add current a variable value to NumberSum
NumberSum = NumberSum + a;
}
System.out.println("Sum of numbers = "+NumberSum);
} // Main close
}
Sum of numbers = 55
Below example is infinite loop.
public class While_For_Loop_Statement {
public static void main(String[] args) {
for ( ; ; ) {
System.out.println("Infinite Loop");
}
} // Main close
}
Below example is showing how to take multiple initialization and updation in for loop
public class While_For_Loop_Statement {
public static void main(String[] args) {
for (int x = 0, y = 4; x < 5; x++, y--){
System.out.println("i = " + x + " , j= " + y);
}
} // Main close
}
i = 0 , j= 4 i = 1 , j= 3 i = 2 , j= 2 i = 3 , j= 1 i = 4 , j= 0