We’ll look at a simple program using while loop, and explain in detail the working of the program. (Related to course – Programming in C (CS 3251) of Anna University.)

Print Hello on the Screen (One Time)

Let us first write a simple program to print Hello on the screen one time.

#include <stdio.h>
int main()
{
printf("Hello\n");
return 0;
}
Output
Hello

Print Hello on the Screen 5 Times

How to do this?

Well, printf("Hello\n"); printed Hello once.

So, we can simply write 5 such printf statements.

#include <stdio.h>
int main()
{
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
return 0;
}

Output

Hello
Hello
Hello
Hello
Hello

Introducing Loops

The above method (writing the same printf statement many times) is not very good. There should be a simpler, cleaner, more compact method.

Yes, there is. We use a loop.

A loop is used to repeat a block of code multiple times.

Here, we need to repeat the printf statement multiple times. So, we’ll put it inside a loop, and tell the computer to run the loop 5 times.

(Each time the loop is run, printf statement is executed once.)

Hence, the printf statement gets executed 5 times, which is what we want.


Program Using while Loop
#include <stdio.h>
int main()
{
int i = 1; // Initialisation
while (i <= 5) // Checking condition
{
printf("Hello\n");
i++; // Increment
}
printf("Out of the loop\n");
return 0;
}

Output

Hello
Hello
Hello
Hello
Hello
Out of the loop

What is the Program Doing?
1. (Line 4) (Initialisation)

Start with i = 1.

2. (Line 5)

Check the condition: i <= 5

If TRUE → Execute lines 6 to 9 (print “Hello” and increase i by 1), and then go back to Line 5 and check the condition (using new value of i).

If FALSE → Exit the loop. Execute from Line 10 onwards.


Working of while Loop (Step by Step)
Panel 1 – Initialization

(Line 4)

i = 1;

i is now 1

Output so far:

(No output)

Panel 2 – Check Condition

(Line 5)

Check whether:

i <= 5

Current value of i is 1.

Output so far:

(No output)

Panel 3 – Condition is TRUE

i is 1, which is <= 5.

Hence condition is true.

So now we go inside the loop.

We execute the statements within the loop.

We start executing from Line 6 onwards.

{
printf("Hello\n");
i++;
}

Panel 4 – Hello is Printed Once

Line 7 is executed.

printf("Hello\n");

Output so far:

Hello

Panel 5 – i is Incremented by 1

Line 8 is executed.

i++;

i was 1 before increment.

After incrementing, i becomes 2.

Output so far:

Hello

Panel 6 – reached end of loop

There are no more statements to execute in the loop (after Line 9).

We now go back to the condition in Line 5 (i <= 5) and check that condition using the new value of i.

while (i <= 5)
{
printf("Hello\n");
i++;
}

Output so far:

Hello

Panel 7 – Condition is Checked

The condition in Line 5 is whether i is <= 5.

i is 2.

2 <= 5.

Therefore, i <= 5.

Therefore, condition is true.

Therefore, we enter the loop again.

That is, we execute the statements in lines 6 to 9.

Output so far:

Hello

Panel 8 – We Execute Line 7 (that is, print Hello)

Line 7:

printf("Hello\n");

Hello was already printed once earlier.

Now, Hello is being printed for the 2nd time.

Output so far:

Hello
Hello

Panel 9 – We Increment Value of i by 1

Line 8:

i++;

i was 2.

After increment, it becomes 3.

Output so far:

Hello
Hello

Panel 10 – We’ve Reached the end of the loop

Line 9:

}

There are no more statements to execute in the loop.

Hence, we go back to Line 5, to check the condition (i <= 5).

Current value of i:

3

Output so far:

Hello
Hello

Panel 11: i is <= 5. Condition is True.

i is 3.

3 <= 5.

Therefore, i <= 5.

Therefore, condition is true.

Since the condition is true, we go inside the loop.

We execute the statements within the loop (lines 6 to 9).

Output so far:

Hello
Hello

Panel 12 – Print Hello One More Time

Line 7:

printf("Hello\n");

Already Hello was printed 2 times earlier.

Now, Hello is printed one more time.

Output so far:

Hello
Hello
Hello

Panel 13 – i is incremented by 1

i was 3.

After incrementing it becomes 4.

Line 8:

i++;

Output so far:

Hello
Hello
Hello

Panel 14 – We’ve Reached the End of the Loop

Line 9:

}

There are no more statements to execute in the loop.

Hence, we go back to Line 5, to check the condition (i <= 5).

Current value of i:

4

Output so far:

Hello
Hello
Hello

Panel 15 – Check the Condition Again

Line 5:

Check if i <= 5.

i is 4.

4 <= 5.

Therefore, i <= 5.

Therefore, condition is true.

Since the condition is true, we go inside the loop.

We execute the statements within the loop (lines 6 to 9).

Output so far:

Hello
Hello
Hello

Panel 16 – Print Hello One More Time

Line 7:

printf("Hello\n");

Already Hello was printed 3 times earlier.

Now, Hello is printed one more time.

Output so far:

Hello
Hello
Hello
Hello

Panel 17 – Increment i by 1

i was 4.

After incrementing it becomes 5.

Line 8:

i++;

Output so far:

Hello
Hello
Hello
Hello

Panel 18 – We’ve Reached the End of the Loop

Line 9:

}

There are no more statements to execute in the loop.

Hence, we go back to Line 5, to check the condition (i <= 5).

Current value of i:

5

Output so far:

Hello
Hello
Hello
Hello

Panel 19 – Check the Condition Again

Line 5:

Check if i <= 5.

i is 5.

5 <= 5.

Therefore, i <= 5.

Therefore, condition is true.

Since the condition is true, we go inside the loop.

We execute the statements within the loop (lines 6 to 9).

Output so far:

Hello
Hello
Hello
Hello

Panel 20 – Print Hello One More Time

Line 7:

printf("Hello\n");

Already Hello was printed 4 times earlier.

Now, Hello is printed one more time.

Output so far:

Hello
Hello
Hello
Hello
Hello

Panel 21 – Increment i by 1

i was 5.

After incrementing it becomes 6.

Line 8:

i++;

Output so far:

Hello
Hello
Hello
Hello
Hello

Panel 22 – We’ve Reached the End of the Loop

Line 9:

}

There are no more statements to execute in the loop.

Hence, we go back to Line 5, to check the condition (i <= 5).

Current value of i:

6

Output so far:

Hello
Hello
Hello
Hello
Hello

Panel 23 – Check the Condition Again

Line 5:

Check if i <= 5.

i is 6.

6 is greater than 5.

Therefore, i <= 5 is false.

Condition is now FALSE.

We do not go inside the loop.

We exit the loop.

We go directly to Line 10.

Current value of i:

6

Output so far:

Hello
Hello
Hello
Hello
Hello

Panel 24 – We’ve Reached Line 10
printf("Out of the loop\n");

We print Out of the loop.

Output

Hello
Hello
Hello
Hello
Hello
Out of the loop

We’ve reached the end of the program.

Program explanation completed.


Summary

We saw a simple program to print Hello 5 times using while loop.

Initialization is done once. (i was set to 1.)

Condition is checked each time we want to enter the loop.

As long as the condition is true, we keep entering the loop.

When the condition becomes false, we exit the loop. The loop is terminated. We go to the statements that come after the loop.

Posted in

One response to “while loop (Programming in C). Introducing loops in C.”

  1. […] while loop (Programming in C). Introducing loops in C. […]

    Like

Leave a comment

Is this your new site? Log in to activate admin features and dismiss this message
Log In