7/31/2023 0 Comments While loop java![]() Because the while loop checks the condition/expression before the block is executed, this control structure is often referred to as a pre-test loop. This repeats until the condition/expression becomes false. First, the condition/expression is evaluated if the condition/expression is true, the code within the block is executed. The while construct consists of two parts: a condition/expression and a block of code. Read: How to Use Java Foreach Loops What is the While Loop in Java? They are also extremely well supported, appearing with virtually the exact same syntax in programming languages like C, C#, Objective-C, C++, JavaScript/TypeScript, and, of course, Java. This programming tutorial will focus on Java’s w hile loop and it’s cousin, the do while loop, which are two of the most basic and oldest looping constructs. While all of them provide similar basic functionality, they differ in their syntax and condition evaluation. Java provides no less than three ways of executing loops. As a result, program control is never coming out of the while loop.Looping is a feature of programming languages that facilitates the execution of a set of instructions repeatedly while some condition is true. And we have not updated the control variable i. When the while starts execution, and i is decrementing, and when i reaches 5, we have a continue statement. The condition is that i should be positive. In the following example, we have initialized i to 10, and in the while loop we are decrementing i by one during each iteration. This also is a typical scenario where we use a continue statement in the while loop body, but forget to modify the control variable. Hello Example – Java Infinite While Loop while working with Continue Statement This makes the loop an infinite while loop. ![]() But, if we forget the decrement statement in the while body, i is never updated. Typically, in the following example, one would decrement i to print hello 10 times. In the following example, we have initialized variable i to 10. ![]() These type of infinite while loops may result when you forget to update the variables participating in the condition. Hello Example 3 – Java Infinite While Loop with No Update to Control Variables No matter how many times the loop runs, the condition is always true and the while loop will run forever. For example, the condition 1 = 1 or 0 = 0 is always true. Instead of giving true boolean value for the condition, you can also give a condition that always evaluates to true. Example 2 – Java Infinite While Loop with Condition that is Always True If you are running the program from an IDE, click on the stop button provided by the IDE. If you are running from command prompt or terminal, to terminate the execution of the program, enter Ctrl+C from keyboard. Note: You will see the string hello print to the console infinitely. So, considering these two statements, we can provide the boolean value true, in place of condition, and the result is a infinite while loop. Secondly, we also know that the condition evaluates to a boolean value. Example 1 – Java Infinite While Loop with True for Conditionįirstly, we know that the condition in while statement has to always evaluate to True for it to become infinite Loop.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |