DEV Community

Saravanan
Saravanan

Posted on

2

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 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;
                }
                */


Enter fullscreen mode Exit fullscreen mode

task 2:

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

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.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay