java while loop and java do while loop with programming examples

While  and  do-while loops:

Java while loop:- Java knows two variants of the while loop, in one case the loop condition is checked before the loop body, in the other case after that.

Java While loop:

With the java while loop, the loop body is executed as long as the condition is met:

Flowchart of java while loop:

java while loop



Syntax of the java while loop

Here the expression is evaluated first (result type boolean) and as long as this returns the value true – the instruction or the instruction block executed and the expression checks again.

It should be noted here that it makes sense to include those that belong to the loop Always parenthesize instructions as a block, even if only one instruction exists to prevent possible confusion.

The second example anticipates  Br is an object of the BufferedReader class. Its method readLine reads a line from the corresponding text file. If When the end of the file is reached, readLine returns zero. The following Loop processes the text file line by line:

Java Do-while loops

In java do-while loops, the condition is at the end of the loop. The Loop body is executed as long as the condition is met. Note That you have to put a semicolon after the while (condition)! In contrast to all other Java loop types, the do-while-Loop that the loop body is definitely executed at least once becomes. With all other loops, it can happen that the condition is false the first time and the inside of the loop thus never achieved.


Flowchart of java do while loop:

java while loop

Syntax of java do while loop:

So here the instruction or the instruction block is first executed And then the expression is evaluated. As long as this has the value true delivers, the whole thing is repeated. The difference to the java while loop is thus the fact that in the repellent loop the logical expression is still ahead the first execution of a statement from the loop body is checked, while with the non-repellent do loop the expression only after the first Execution of the instruction (s) is evaluated. It can therefore happen that with the rejecting loop no instruction of the loop body is executed Is clocked, while in the case of the non-repellent loop in any case at least something is performed once. The above example for the while statement now with the do statement:

Here again, the note that it makes sense to use the statements belonging to the loop always to be bracketed as a block, even if there is only one instruction, to avoid possible mix-ups.


Endless or Infinite loops:

When programming loops, it is (intentionally or unintentionally) possible to formulate so-called infinite loops (also called infinite loops). The naming is justified by the fact that the instructions of the loop body can be executed infinitely often. Examples for consciously formulated endless loops would be something like

In order to avoid unintentional endless loops, you should be careful with the Formulation of the logical expressions that cause the loop to break off, the operands of the logical expression after finitely many steps like this change so that the expression returns the value false and thus the loop comes to an end. Unfortunately, the two examples below have programming errors crept in so that the above requirement is not met. In the java do while loop

we forgot to increase the variable i with every run. In the for-loop

unfortunately neutralizes the decrement of i in the last statement of The body of the loop is the incrementation of i in the update list.



marked instructions:

Finally, we want to familiarize ourselves with the class of the so-called jump commands, for example, to jump out of loops and use them to do so. prematurely. For those who already have experience in programming languages like Basic or C++ have collected, a small warning in advance: In the language Java, unlike other programming languages, does not goto-statement – and that’s good! In general, you should use the use commands, especially break and continue, only with care, because nothing is clearer (and therefore more error-prone) than programs, in which are jumped back and forth wildly.

The break statement has already been used in conjunction with the switch statement. It is used in general for the purpose of ¨ the innermost block or the block currently under construction to interrupt the innermost loop and with the instruction immediately after the block or loop follows. In Java, however, it is also possible with nested blocks and loops, these purposefully abort prematurely. For this purpose, a so-called mark (consisting of from an identifier followed by a colon), and Select a block or a loop. If, for example, a java while loop in the form

so you can add more loops and blocks to their instructions and from these inner loops or blocks with the command

break mark;

Equivalence of java while loop and a java do-while loop :

The java do-while loop is used less frequently than the java while loop. Nevertheless, both can be converted into one another. First the first Case: We replace a java while loop with a java do-while loop:

Let’s look again at what’s happening here. Depending on the expression, the hull is executed. First, a test, if the java do-while loop were already a block execution continues. For example, in the first step, we query with an if statement. whether the condition is true or not. If so, then we will leave the Process program code in a java do-while loop.

The equivalent do-while loop looks like this:

Now the second case: We replace the java do-while loop with a java while loop:

Since the statements are executed first and then the for the while variant, we simply write the expressions before the Test. This ensures that these are processed at least once will be:


When use for loop and when use java while loop?

Since the while and for loops are very similar, the question is authorizes the use of one and the other. Unfortunately, the compact for loop very quickly leads to an overload. Some programmers like to put everything in the loop head. and the fuselage consists of only one empty statement. This is the rooms were very small.

for loops should be used whenever a variable is increased by a constant size. If there is no loop variable that is incremented or decremented should be a while loop. A do-while loop should are used when the abort condition is only at the end of a loop pass. The for loop where all three expressions in the Refer loop head to the same variable. should be avoided unrelated expressions in the loop head. The writer Access to the loop variable in the hull is a bad idea if it’s also being modified in the head at the same time — that’s hard to do. and can easily lead to endless loops.

The for loop is not set to a specific type, even if for loops for incrementing the implicit type int. The initialization part can pre-allocate all possible options, whether int, double, or a reference variable. The condition can be anything Try it out, only the result has to be a boolean.

Java While loop and java do while loop Programming examples:

Example: how to display from 1 to 10 using java while loop:

output:

java while loop


Example: how to display the sum of an only positive number using java while loop:

When you put the negative number it will display the sum.

output:

java while loop

Example: how to display from 1 to 10 using java do while loop:

output:

java while loop



Example: how to use infinite do while loop in java:

output:

java while loop

The program will never stop until you press the ctrl + c

java while loop


Related Articles:

Java for Loop Statements with Programming Examples

Java Switch Statement with Programming Examples

Java if Statement: if else, correct indentation, ternary operator, nested if

 

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: