Control flow statement: while loop.

loops:

-Loops can execute a block of code as long as a specified condition is reached.
-Loops are handy because they save time, reduce errors, and they make code more readable.

while loop:

A while loop in Java is a control flow s…


This content originally appeared on DEV Community and was authored by Saravanan

loops:

-Loops can execute a block of code as long as a specified condition is reached.
-Loops are handy because they save time, reduce errors, and they make code more readable.

while loop:

A while loop in Java is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The loop continues to execute as long as the condition evaluates to true.

syntax:

while (condition) {
// code block to be executed
}

task 1:

/*
                int count = 0;
                while(count<=5)
                {                                       //Output:1 1 1 1 1
                    System.out.println(1);
                    count = count + 1;
                }
                */


task 2:

 /*
                int count = 5;
                while(count>=1)
                {                                         //Output:5 4 3 2 1 
                    System.out.println(count);
                    count = count - 1;
                }
                */

key point:

-The loop runs zero or more times, depending on the condition.
-If the condition is false at the start, the loop does not execute.
-If the condition never becomes false, an infinite loop occurs.


This content originally appeared on DEV Community and was authored by Saravanan


Print Share Comment Cite Upload Translate Updates
APA

Saravanan | Sciencx (2025-01-29T13:26:26+00:00) Control flow statement: while loop.. Retrieved from https://www.scien.cx/2025/01/29/control-flow-statement-while-loop/

MLA
" » Control flow statement: while loop.." Saravanan | Sciencx - Wednesday January 29, 2025, https://www.scien.cx/2025/01/29/control-flow-statement-while-loop/
HARVARD
Saravanan | Sciencx Wednesday January 29, 2025 » Control flow statement: while loop.., viewed ,<https://www.scien.cx/2025/01/29/control-flow-statement-while-loop/>
VANCOUVER
Saravanan | Sciencx - » Control flow statement: while loop.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/29/control-flow-statement-while-loop/
CHICAGO
" » Control flow statement: while loop.." Saravanan | Sciencx - Accessed . https://www.scien.cx/2025/01/29/control-flow-statement-while-loop/
IEEE
" » Control flow statement: while loop.." Saravanan | Sciencx [Online]. Available: https://www.scien.cx/2025/01/29/control-flow-statement-while-loop/. [Accessed: ]
rf:citation
» Control flow statement: while loop. | Saravanan | Sciencx | https://www.scien.cx/2025/01/29/control-flow-statement-while-loop/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.