Javatpoint Logo
Javatpoint Logo

Loops

Loops in programming are the way of repeating actions. Let?s look at three different kinds of the loop, a For loop, a While loop, and a Do While loop.

While Loop

The ?while loop? is used to perform an action, while a condition is met.

To make a while loop, start with the keyword ?while? followed by brackets. Within the brackets, you must write a condition. Whenever the condition is true, the code within the loop block will be executed:

Syntax:

Example:

Let?s see an example. In this example, we have a variable called PlayerLives. We have a total four life of a player. While the number of life of a player is more than zero, the player can play the game. Means, this loop will continue four times because the player has four lives. And after that, the Player will lose the game.

Output:

While Loop

Do While Loop

The do-while loop is almost identical to the While loop, with one major difference. While loop tests the condition before the loop body, however, do-while loop tests the condition at the end of the body. This difference means that the body of the do-while loop is guaranteed to run at least once. Here is the syntax of the do-while loop:

Syntax:

Example:

In this example, we can see that we have a Boolean variable called shouldContinue. This variable is set to false. Next, we have the Do while loop. We start with the keyword do follow by open and closed braces, whatever the code is between these braces makes up the body of the loop. After the body ?while keyword? is there followed by the condition. In this case, the loop would only continue while the variable shouldContinue is equal to true.

Output:

Do While Loop

For Loop

?For Loop? is probably the most common and flexible loop. The ?for loop? works by creating a loop with a controllable number of iterations. Functionally it begins by checking conditions in the loop. After each loop, known as an iteration, it can optionally increment a value.

The syntax for this has three arguments. The first one is iterator; this is used to count through the iterations of the loop. The second argument is the condition that must be true for the loop to continue. Finally, the third argument defines what happens to the iterator in each loop.

Syntax:

Example:

Output:

For Loop

For Each Loop

The foreach loop is very simple and easy to use. It has the simplest syntax. The foreach keyword followed by brackets is used in this loop. You must specify the type of data you want to iterate inside the brackets.

Pick a single element variable name, give a name to this variable whatever you want. This name is used to access this variable inside the main loop block. After the name, write in a keyword, followed by our List variable name.

Syntax:

Example:

Output:

For Each Loop





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA