Keyboard Boolean to Continue While Loop Java
Introduction to while loop in Java
Loops in Java are used. when we need to repeatedly execute a block of statements. The two most important types of loops are the while loop and the for loop. while loop in Java is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.
In this tutorial, we will learn about while loop java in detail. We will cover different examples and various kinds of while loop java including an empty and infinite while loop. At the same time, we will also discuss the use of break and continue statements in the while loop and their role in control flow. Moreover, we will also the nested while loops and how they actually work by taking different examples.
Towards the end, we will also give a touch to the java do-while loop and will solve examples. All in all, this is going to be a full tutorial about while loop java, and we are sure that by the end of this tutorial you will have a deep understanding of while loop.
ALSO READ: How to Compare Characters in Java [Practical Examples]
Getting started with while loop in Java
while loop in Java may contain a single, compound, or empty statement. The loop repeats while the test expression or condition evaluates to true. When the expression becomes false, the program control passes to the line just after the end of the loop-body code. Here is a simple diagram that explains the while loop java pictorially.
In the following section, we will learn the basic syntax of while loop java along with some examples.
Syntax of while loop in Java
The syntax of while loop java is very simple. We need to write the condition inside the brackets as shown below:
while(condition){ // statements }
Now let us take a simple example and print all the numbers from 0 up to 5. See the example below.
// class public class Main { public static void main(String[] args){ // variable defining int num = 0; // while loop java while(num < 5){ // printing the numeber System.out.println("number...."+ num); // updating the condition num++; } } }
Output:
number....0 number....1 number....2 number....3 number....4
Notice that once the condition inside the while loop becomes false, the program stops executing the while loop.
Syntax and example of empty while loop in Java
An empty while loop java does not contain any statement in its body. It just contains a null statement which is denoted by a semicolon after the while statement: The simple syntax looks like this;
while(condition){ ; }
Notice that there is nothing inside the body of the while loop except a semi-colon. Now let us take an example of the empty while loop. See the example below:
// class public class Main { public static void main(String[] args){ // variable defining int num = 0; // while loop java while(++num < 5){ ; } } }
Notice that we increment the num inside the condition which is fine. The above empty loop will be executed until the condition becomes false. Because it is an empty while loop java, it will not return anything when we run the code. Such a loop is a time delay loop. The time delay loop is useful for pausing the program for some time. For instance, if an important message flashes on the screen and before we can read it, it goes off. So, In such cases, we can use a time delay loop to get sufficient time to read the message.
ALSO READ: NumberFormat Class in Java Explained [Practical Examples]
Syntax and example of Java infinite loop
A while loop can be an infinite loop if we skip writing the update statement inside its body or the condition is always true. The following is the simple syntax of the java infinite loop;
while (true condition){ // while loop statements }
It is important that to have always a true condition for an infinite loop, otherwise, the loop will stop, once the condition becomes false. Now let us take an example of java infinite while loop. See the example below;
// class public class Main { public static void main(String[] args){ // variable defining int num = 0; // while loop java while(num < 5){ System.out.println("counting....."+num); } } }
Notice that the above condition is always true as we are not updating or incrementing the number inside the loop. If we run the above code, it will iterate or execute the while loop infinitely time. We can stop an infinite loop by pressing ctrl+ C
button on the keyboard.
Iterating an array using while loop in Java
Now we know the basics and important concepts about the while loop. Let us now iterate over an array using while loop java and print out all the elements. See the example below:
// class public class Main { public static void main(String[] args){ // array of int type int arr[]={1,2,3,4, 5}; //INDEX starts with 0 as array index starts with 0 too int INDEX=0; // while loop java: while(INDEX<5){ // printing the element System.out.println(arr[INDEX]); // updating the index INDEX++; } } }
Output:
1 2 3 4 5
ALSO READ: Java break & continue statements Explained [Easy Examples]
The break statement in the Java loop
The break keyword is a command that works inside Java while (and for) loops. When the break command is met, the Java Virtual Machine breaks out of the while loop, even if the loop condition is still true. No more iterations of the while loop are executed once a break command is met. This is useful when we want to stop the loop when a certain condition meets. See the example below, which stops the loop even the initial condition is true all the time.
// class public class Main { public static void main(String[] args){ // variable int num = 0; // while loop java: while(true){ // if condition if(num==7){ break; } System.out.println("counting..."+num); num++; } } }
Output:
counting...0 counting...1 counting...2 counting...3 counting...4 counting...5 counting...6
Notice that even the initial condition was true all the time but still we managed to come out of the while loop using a break statement.
While loop in Java with continue statement
Java continue command is also used inside Java while (and for ) loops. The continue command is placed inside the body of the while loop. When the continue command is met, the Java Virtual Machine jumps to the next iteration of the loop without executing more of the while loop body. The next iteration of the while loop body will proceed like any other.
ALSO READ: Java WatchService Examples [Monitor Directory and Files]
If that iteration also meets the continue
command, that iteration too will skip to the next iteration, and so forth. Now let us take an example and say that we want to print the numbers from 0 to 9 without printing the number 5. In such cases, we can use the continue statement. See the example below;
// class public class Main { public static void main(String[] args){ // variable int num = 0; // while loop java: while(num<10){ // if condition if(num==5){ num++; // continue statement continue; } // printing the num System.out.println("counting..."+num); num++; } } }
Output:
counting...0 counting...1 counting...2 counting...3 counting...4 counting...6 counting...7 counting...8 counting...9
Notice that we were able to print the numbers without printing number 5. We used the continue statement which skips the number 5 and prints others.
Nested while loop in Java
When a while loop exists inside the body of another while loop, it is known as nested while loop in java. Initially, the outer loop executes once and the afterward inner loop begins to execute. Execution of the inner loop continues until the condition of the inner loop is satisfied(until the test expression is false). In this section, we will have a look at the java nested while loops and will solve examples as well.
Syntax and example of Java nested while loop
As we already discussed nested while loop is when one while loop is inside another while loop. The simple syntax of nested while loop looks like this:
// Outer while loop while(condition1){ // inner while loop while(condition2){ // statements } }
We can put as many while loops inside each other as we wanted. Now, let us take an example of java nested while loop. See the example below:
// class public class Main { public static void main(String[] args){ // variable int num = 1; // while loop in Java: while(num<5){ // printing the table System.out.println("Table of "+num); int num2 = 1; // inner while loop while(num2< 6){ // printing the table System.out.print(num*num2+" "); num2++; } // printing new line System.out.println(""); // updating the num num++; } } }
Output:
Table of 1 1 2 3 4 5 Table of 2 2 4 6 8 10 Table of 3 3 6 9 12 15 Table of 4 4 8 12 16 20
Notice that we were successfully able to print the table of numbers from 1 to 5 using nested while loop. There can be many other cases as well where nested loops help us to solve the problem easily.
ALSO READ: List vs LinkedList in Java Explained [Practical Examples]
do-while loop Java
Unlike the for and while loops, the do-while loop is an exit-controlled loop which means a do-while loop evaluates its test-expression or test-condition at the bottom of the loop after executing the statements in the loop-body. Even if the condition is false, the loop is going to be executed at least once. In this section, we will have a look at the syntax of the java do-while loop and will solve examples using the do-while loop.
Syntax of do-while loop in Java
In the for and while loops, the condition is evaluated before executing the loop-body. The loop body never executes if the test expression evaluates to false for the first time itself. But in some situations, we want the loop-body to execute at least once, no matter what is the initial state of the test expression. In such cases, the do-while loop is the best option which is executed at least once even the condition is always false. The simple syntax looks like this:
do { // statements } while(condition);
First, the statements inside the do will be executed and then the program will check the condition in the while loop. If the condition is false, the loop will not execute for the second time but if the condition is true the do statements will again execute.
ALSO READ: Java Type Casting Explained [Easy Examples]
Examples of do-while loop in Java
Now we already know the syntax of the Java do-while loop. Let us now take an example and see how it actually works. See the example below:
// class public class Main { public static void main(String[] args){ // do while loop do{ System.out.println("Do while loop"); } while(false); } }
Output:
Do while loop
Notice that even the condition was false, the do-while loop was executed once. Now let us take another example and print the numbers from 1 to 5 using do-while loop;
// class public class Main { public static void main(String[] args){ // variable int num =0; // do while loop do{ num++; System.out.println("counting..."+num); }while(num<5); } }
Output:
counting...1 counting...2 counting...3 counting...4 counting...5
Summary
A while loop executes the body of the loop as long as (or while) a Boolean condition is true. When the condition is false, the program exits the loop and continues with the statements that are after the body of the while loop. If the condition is false the first check, the body of the loop will not execute. In this tutorial, we learned about while loop in Java. We covered how we can write the syntax of while loop in java along with various examples. We also learned how to create empty and infinite while loop in Java.
ALSO READ: How to set up Java with Eclipse IDE [Step-by-Step]
Moreover, we also discussed the nested while loops and solve various examples. Towards the last part of this tutorial, we also discussed the do-while loop in java and learned how it is different from other while loop. All in all, this tutorial, covers, and everything that you need to know about while loop in Java.
Further Reading
while loop in Java
break and continue statements
Java for loop
Source: https://www.golinuxcloud.com/while-loop-java-examples/
0 Response to "Keyboard Boolean to Continue While Loop Java"
Post a Comment