Loops

description of image


Loops in Programming


In programming, loops are used to repeat a block of code.

Suppose, we want to print a sentence 100 times. We can solve this problem in a few lines by using a loop.

This is just a simple example; we can create many interesting programs after learning loops.

There are three types of loops in Java programming.

For loop

While loop

Do…while loop



For Loop:


● The syntax of the for loop is:

For (initialization; testCondition; update) {

// body of the loop

}

Here’s how this loop works:

The initialization statement is executed only once at the beginning.

Then, the test condition is evaluated.

If the test condition evaluates to true, the body of the loop is executed, and the update statement is updated.



Example: for Loop


● This program prints a sentence 3 times.


● it is because we provided input as 3.



Quiz


Question Text