Learn While Loop In Java

Sharing is healthy !!

In this post we will learn while loop in java.

Here we will see how to use while loop which is the most basic kind of loop in java.

Learn While Loop In Java: what is a loop?

A loop is a piece of code or statement which executes some block of code again and again until some condition is met.

Syntax:

while(condition)
{
statement 1;
statement 2;
}

So, for example you want to execute some block of code again and again in java without rewriting it. Loop will help you to do exactly the same.

Now i will show you one example,

public class While_Loop_Example 
{
public static void main(String[] args) 
{
int x = 0;
while(x < 10)
{
System.out.println(x);
}
}

}

For example i want to check whether the value of ‘x’ is less than 10 or not. And right now i have initialized it by 0 so obviously it is less than 10. Then it will be executed forever if i don’t change the value of ‘x’.  So above example can be an infinite loop. Infinite loops are also useful if you know how to use them.

But right now we will see how to use condition more effectively.

Let’s see an example,

public class While_Loop_Example 
{
public static void main(String[] args) 
{
int x = 0;
while(x < 10)
{
System.out.println(x);
x++;
}
}

}

Output:

Learn While Loop In Java

I print the value of ‘x’ and then i increase it by one. So then after running the program it did the same execution of code again and again unless test condition has met.

So what this code has done initially is; the value of ‘x’ was zero so it has printed the value of ‘x’ as 0 because 0 is less than 10. Then it increased the value of ‘x’ by 1 and now the value of ‘x’ became 1. And then it goes to the condition again and 1 is less than 10 then it prints the value of ‘x’ once again and it prints 1. And it executes the same statement again and again and it increases the value of ‘x’ again and again until the value of ‘x’ becomes 10.

Also Read – Do While Loop Java

So it printed the value of ‘x’ until 9. But when the value of ‘x’ becomes 10 in the condition, 10 is not less than 10 then the condition is false and it will go out of the loop and program is terminated.

In the same way if you do something like below,

public class While_Loop_Example 
{
public static void main(String[] args) 
{
int x = 0;
while(x <= 10)
{
System.out.println(x);
x++;
}
}

}

Output:

0
1
2
3
4
5
6
7
8
9
10

In the above example the condition is x less than or equal to 10. Then the above example will print the value of ‘x’ until 10. And we have done using only a single line of code.

System.out.println(x);

We have printed ten or eleven values using a single line of code using loop. We are just incrementing the value of ‘x’ on the basis of

x++;

We are changing the value of ‘x’ and then we are checking these conditions

while(x <= 10)

This will work in opposite direction also. For example,

public class While_Loop_Example 
{
public static void main(String[] args) 
{
int x = 10;
while(x >= 1)
{
System.out.println(x);
x--;
}
}

}

Here in the above condition when you change it to x >= 1 then the output will be,

Output:

10
9
8
7
6
5
43
2
1

It has printed the value from 10 to 1in decreasing order. So you can do or execute your code like above.

conclusion:

So what basically this while loop is doing is it’s executing the code or these two lines

System.out.println(x);
x--;

again and again until and unless the condition is true. So if condition is true the statements will be executed. As soon as the condition becomes false the while loop will break and it will go out of the loop.

So this is the basic concept of loop. I hope you have understood the basic concept

Sharing is healthy !!

Leave a Reply

Your email address will not be published.