In the last tutorial, we discussed for loop. In this tutorial we will discuss while loop. As discussed in previous tutorial, loops are used to execute a set of statements repeatedly until a particular condition is satisfied.
Syntax of while loop
while(condition) { statement(s); }
How while Loop works?
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute. When condition returns false, the control comes out of loop and jumps to the next statement after while loop.
Note: The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely.
Simple while loop example
class WhileLoopExample { public static void main(String args[]){ int i=10; while(i>1){ System.out.println(i); i--; } } }
Output:
10 9 8 7 6 5 4 3 2
Infinite while loop
class WhileLoopExample2 { public static void main(String args[]){ int i=10; while(i>1) { System.out.println(i); i++; } } }
This loop would never end, its an infinite while loop. This is because condition is i>1 which would always be true as we are incrementing the value of i inside while loop.
Here is another example of infinite while loop:
while (true){ statement(s); }
Example: Iterating an array using while loop
Here we are iterating and displaying array elements using while loop.
class WhileLoopExample3 { public static void main(String args[]){ int arr[]={2,11,45,9}; //i starts with 0 as array index starts with 0 too int i=0; while(i<4){ System.out.println(arr[i]); i++; } } }
Output:
2 11 45 9
Check out these related programs:
Hi, is it possible to these tutorials in pdf format?
Hey,
the notes were really helpful but i couldn’t understand the last example .Can anyone help me please?
please mention the point which you can’t understand
First of all…..
The initialization done with i=0
Then goto while loop and check the condition i<4(i=0)
It is true goto the loop body execute the looping statement i.e., args[0]
Then increment the i value by 1
After incrementing again check the while loop condition …….
……
Until the condition is false.
It prints given o/p
….thats all
i would like to print all the tables with while loop
for example i want the output as :
1*1=1
1*2=2
………….
2*1=2
2*2=4
…………..
3*1=3
3*2=6
…..
up untill 10th table
Can someone help me to write the code for this.
Thank you
public class Tables2 {
public static void main(String[] args) {
int num=3;
int i=1;
while(i<=10){
System.out.println("Tables 2: " +num*i);
i++;
}
}
}
hi , I have small doudt when use for loop and when use while loop
and what is the different between for loop and while loop
In for loop if the condition is true, block of statement executes first
——————
means change reflects after the completion of first iteration
In while loop if the condition is true and if it finds the increment/decrement statement in first line inside the block then it process the increment/decrement operation first and prints the output accordingly
——————
means changes reflects in first iteration itself else if the increment/decrement statement is not in first line then it is same as ‘for’ loop.
Please find the example below,
Example for loop:
class Forlooparrayexample {
public static void main(String args[]){
int a[]={1,2,3,4};
for(int i=0;i<4;++i)
{
System.out.println(a[i]);
}
}
}
output:
1
2
3
4
Example while loop:
class Whilelooparray{
public static void main(String[] args)
{
int a=0;
int []i=new int []{1,2,3,4};
while(a<3)
{
++a;
System.out.println(i[a]);
}
}
}
output:
2
3
4
Write a method with a while loop to prints 1 through
n in square brackets. For example, if n = 6 print
! ![1] [2] [3] [4] [5] [6]!