For-each loop in Java

For-each loop in Java

Java while loop is another loop control statement that executes a set of statements based on a given condition. In this tutorial, we will discuss in detail the while java loop. Compared to for the loop, while the loop has no fixed number of iterations. unlike loop, the scope of the variable used in java while loop is not limited in the loop since we declare the variable outside the loop.for

Java while loop syntax

while(test_expression){ codeupdate_counter;//update the variable value used in the test_expression}

expression_test – This is the condition or expression on the basis of which the while loop executes. If the condition is true, it executes the code in the while loop. If it is false, it exits the while loop.

update_counter – This is to update the value of the variable used in the condition of the java while loop. If we don’t specify it, it can result in an infinite loop.

How the while loop works

The flowchart below shows you how java the loop works.while

Java while boucle
  • When the run-time control points to the while statement, it first evaluates the test condition or expression. The condition can be any type of operator.
  • If the condition returns a true value, it executes the code inside the while loop.
  • Then updating the value of the variable increments or decrements the variable. It is important to include this code in the java while loop, otherwise it could result in an infinite javawhile loop. We will discuss the infinite loop towards the end of the tutorial.
  • Again, the control points to the while statement and repeats the above steps.
  • When the condition returns a false value, it exits the java while loop and continues executing statements outside the while loop

Example of a simple java loop

Below is a simple code that demonstrates a java while loop.public class simpleWhileLoopDemo{ public static void main(String[] args){ int i=1; while(i<=5){System. out. println(“Value of i is: ” + i); i++; }}}Value of i is: 1Value of i is: 2Value of i is: 3Value of i is: 4Value of i is: 5

We first declare an int variable i and initialize with the value 1. In the while condition, we have the expression like i <= 5, which means until i value is less than or equal to 5, it executes the loop.

Therefore in the 1st iteration, when in = 1,the condition is true and prints the statement in the java while loop. It then increments i value by 1 which now means in = 2.

It then checks again if i < = 5. Since it is true, it re-executes the code inside the loop and increments the value.

It repeats the above steps up to in =5. At this point, after you run the code inside the while loop, i increments value and in = 6. Now the condition returns false and therefore exits the while java loop.

While Loop in Array

Similar to loop, we can also use a java while loop to retrieve the elements of the array. In the example below, we retrieve the elements of the array and find the sum of all the numbers using the while loop.forpublic class whileLoopArray{ public static void main(String[] args){ int[] numbers = {20,10,40,50,30}; int i=0; int sum=0; while(i<numbers. length){sum = sum+numbers[i];i=i+1; }System. out. println(“Sum of array elements: ” + sum);System. out. println(“Length of array: ” + i); }}Sum of array elements: 150Length of array: 5

explanation:

First, we initialize an array of integer numbers and declare the loop counter while java variable i. Since it is an array, we need to iterate through all the elements of an array to the last element. For this, we use the length method inside the while java loop condition. This means that the while loop runs until i The value reaches the length of the array.

Iteration 1 when i = 0: condition: true, sum = 20, i = 1

Iteration 2 when i = 1: condition: true, sum = 30, i = 2

Iteration 3 when i = 2: condition: true, sum = 70, i = 3

Iteration 4 when i = 3: condition: true, sum = 120, i = 4

Iteration 5 when i = 4: condition: true, sum = 150, i = 5

Iteration 6 when i = 5: condition: false -> exit loopwhile

We invite you to refer to our Tables in java tutorial to learn more about tables.

Infinite while loop

As discussed at the beginning of the tutorial, when we do not update the counter variable correctly or do not mention the condition correctly, it will result in a loop. Let’s see this with an example below.whilepublic class infiniteWhileLoop{ public static void main(String[] args){ int i = 0; while(i>=0){System. out. println(i); i++; }}}

Here we initialized the variable with the value 0. In the java while condition of the loop, we check whether i value is greater than or equal to 0. Since we increment i value inside the while loop, the condition i> = 0 while always returns a true value and will run indefinitely.

We can also have an infinite while loop in java in another way, as you can see in the example below. Here the value of the bDrapeau variable is always true since we do not update the value of the variable.public class infiniteWhileLoop{ public static void main(String[] args){ Boolean bFlag = true; while(bFlag){System. out. println(“Infinite loop”); }}}

Therefore, the java while infinite loop occurs under the conditions below 2. It is always important to remember these 2 points when using a while loop.

  • when we do not update the value of the variable
  • when we do not use the condition correctly in the while loop

Nested while loop

We can also have a nested while loop in java similar to loop. When there are multiple while loops, we call it as a nested while loop.forpublic class Nestedwhileloop{ public static void main(String[] args){ int i=1,j=10; while(i<=5){System. out. println(“i: ” + i); i++; while(j>=5){System. out. println(“j: ” + j); j–; }}}}i: 1j: 10j: 9j: 8j: 7j: 6j: 5i: 2i: 3i: 4i: 5

In this example, we have 2 while loops. The outer while loop repeats until i < = 5 and the inner while loop repeats until j> = 5.

When in = 1,the condition is true and prints i value and then increments i value by 1. Then it executes the internal while loop with a value of j = 10. Since the condition j> = 5 is true, it prints the value j. Now it continues executing the internal while loop completely until the condition j> = 5 returns false. Once it is false, it continues with the execution of the outer while loop until i <= 5 returns false.

That’s why in the output you can see after printing in =1,it executes all j values starting with j=10 up to j=5 and then printing i values up to in=5. When in = 2,it does not execute the loop since the condition is false.while

Java while looping with multiple conditions

We can have multiple conditions with multiple variables in the java while loop. In the example below, we have 2 variables a and i initialized with the values 0. Here we will print the even numbers between 0 and 20. For this, inside the while java loop, we have the condition a <= 10, which is just a counter variable and another condition ((i% 2) == 0) to check if it is an even number. Inside the java while loop, we increment the counter variable a by 1 and i value by 2.public class Whileloopconditions{ public static void main(String[] args){ int a = 0; int i = 0;System. out. println(“Even numbers between 0 to 20:”); while((a<=10) && ((i%2)==0)){System. out. println(i); a++; i=i+2; }}}Even numbers between 0 to 20:02468101214161820

Tech Crazee

Kumar Swamy founder of Tech Crazee and I have more than 8 years of experience in digital marketing. I have gained knowledge of different strategies in digital marketing to boost website traffic and reputation. I am also interested in blogging and started this website to write articles on technology. I am interested to show something new to my readers. And I always help people who are seeking knowledge of digital marketing. Contact me for any information about digital marketing.