Examples on Loops

For Complete YouTube Video: Click Here

In this class, we will try to understand Examples on Loops.

We have already discussed for, while, do-while loops and examples on loops.

The image below is the format that we discussed in for loop.

We have discussed that the program’s initialization, condition, and increment part is for initialization, condition check, and increments only.

No, this is not the case.

We can also use any expression, function calls, etc., in the initialization, condition check and increment part.

Examples on loops

Example 1

The image below is an example.

Examples on Loops 1
Examples on Loops 1

In the above example, the initialization part uses expression where the increment and decrementing of ‘i’ and j is done.

Whereas the initialization of ‘i’ and j is done before the for ‘loop.’

A very important point we have to understand is the expression will get executed only once as it s used in the initialization part of the ‘for’ loop.

Example 2

The image below is an example.

Examples on Loops 2
Examples on Loops 2

In the above example, the increment part uses the printf function call.

Whereas the increment of ‘i’ and ‘j’ is done in the body of the for ‘loop.’