While loop in java with example

There are several looping statements available in java. One of them is while loop in java.
While loop is used to execute some statements repeatedly until condition returns false. If number of iterations are not known beforehand, while loop is recommended.

The syntax for while loop in java

Let’s take a very simple example:
Print number from 1 to 10 using while loop

Output:

1 2 3 4 5 6 7 8 9 10

If you are still confused with above concept, let’s understand it with the help of flow diagram.

While loop in java

Let’s try to print only even numbers now.

Output:

2 4 6 8 10

Exercise

If you are given an array of integer, you need to find an element in that array.

Input:

{32,45,53,65,43,23}

You need to write a program to search an element in the array.If element is found in the array, return "PRESENT" else return "NOT PRESENT"

I would recommend you to try it yourself and then look at below code.

Program:

Output:

53 is present in the array
PRESENT

Infinite while loop

You need to be careful with condition you provide in while loop otherwise, you may end up creating infinite while loop.
For example:
Let’s say you want to print number from 10 to 1 and you use below code:

Output:

10 9 8 7 6 5 4 3 2 1

Now in above code, instead of i–, you have put i++. In this code,loop will go into infinite loop.

Another example of infinite while loop in java is below code:

That’s all about while loop in java.

Related Posts

  • 27 April

    Switch case in java

    Switch case in java is alternative to if else if ladder. It is used to execute statements based on some conditions. Syntax of Switch case in java [crayon-6001ca96e89a7394403162/] Let’s understand it with the help of simple example. We are going to print weekday based on integer. 0 represents Sunday, 1 represents Monday and so on.. […]

  • 27 April

    if else statement in java

    If else statements in java are used to test some conditions. It evaluates a condition to be true or false. There are three types of if statements. If statement If else statement If else if ladder statement Simple If statement [crayon-6001ca96e8bc0798447385/] For example: [crayon-6001ca96e8bc5360365629/] Output: Price is greater than 10 You can write single line followed […]

  • 27 April

    Do While loop in java with example

    There are several looping statements available in java. One of them is do while loop in java. While loop is used to execute some statements repeatedly until the condition returns false. If the number of iterations is not known beforehand, while the loop is recommended. In Do while loop, loop body is executed at least […]

  • 13 April

    Java break statement example

    Break statement is one of the different control statements which we use very often. If break statement is found in loop, it will exit the loop and execute the statement following the loop. Let’s take very simple example: Java break statement example You want to search for the element in an array and if element […]

  • 12 April

    Java continue statement example

    continue statement is one of the different control statements which we use very often.If continue statement is found in loop, it will continue the current iteration and will not execute statements following continue statements. It is generally used to skip current iteration on the basis of some condition. Let’s take very simple example: Java continue […]

  • 05 April

    For loop in java with example

    There are several looping statements available in java. One of them is for loop in java. For loop in java is used to execute some statements repeatedly until the condition returns false. There are three parts to for loop. Initialization Condition Increment or decrement Syntax for for loop in java [crayon-6001ca96e8eac579340713/] Initialization: Initialization statement executes […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.