Repeating Logic in Programming

Shafi Sahal
Geek Culture
Published in
5 min readJun 15, 2021

Suppose there arises a need to display a text a hundred times. One way that is very painful, is to write a hundred lines of code to display it a hundred times. Thanks to loops, there is no need to repeatedly write code to do such repetitive tasks.

while loop

The ‘while loop’ loops through the statements until the condition becomes false.

Syntax:

while(condition) {
statement(s);
}

Example:

Output

I am looping 1 times
I am looping 2 times
I am looping 3 times
I am looping 4 times
I am looping 5 times

The condition for the loop is written inside the parenthesis after the ‘while’ keyword

while(i <= 5)

The while loop first checks the condition. If the condition is true, it then executes the statements until the condition becomes false. As the value of ‘i’ is incremented on each loop, it finally becomes greater than 5, and when that happens, the condition becomes false and the loop will stop execution.

printf("I am looping %d times\n", i);

The ‘\n’ is used for the newline. The backslash tells the computer ’n’ is not to be displayed but used as a representation for the newline. If there was no newline, the output will have displayed continuously in one line.

for loop

In the while loop code, the value of ‘i’ is initialized first and incremented inside the loop block (code between the curly braces).

For loop is more convenient to use in such cases. It provides a way to initialize, give conditions, and increment the variables more conveniently.

Syntax:

for ( init; condition; increment ) {
statement(s);
}

Example:

Output

I am looping 1 times
I am looping 2 times
I am looping 3 times
I am looping 4 times
I am looping 5 times

The initialization, conditions, and increment, all are given in the parenthesis after the ‘for’ keyword.

for (i = 1; i <= 5; i++)

This tells the computer, initialize ‘i’ with the value of one, repeat the statements if ‘i ≤ 5’, and increment the value of ‘i’ by one on each loop.

The for loop also checks the condition first and then executes the statements.

do…while loop

It’s the same as the while loop except for one difference. The former loops check the condition first and then execute the statements. ‘do…while’ loops execute the statements and then check the condition.

Syntax:

do {
statement(s);
} while( condition );

Example:

Output:

I am looping 1 times
I am looping 2 times
I am looping 3 times
I am looping 4 times
I am looping 5 times

The program displays the same output for the while loop and the do…while loop. So, what’s the difference actually? What’s the whole checking condition at first versus checking condition at the last thing about?

Difference between while loop and do…while loop

The difference is that the while loop will never execute the statements if the condition is false but do…while loop will execute the statement at least once even if the condition is false as it executes first and then checks the condition.

The above program outputs nothing because the ‘printf’ statement is not executed as the condition is false.

Output

I am looping 1 times

See, even if the condition is false, the statements are executed for once as the condition is checked after the execution. That’s the main difference between the while loop and the do…while loop.

Multiplication Table Generator program

A simple and interesting thing to make using loops is the multiplication table generator. Give it a try.

Output

Enter a number to print it's multiplication table: 4
Enter the limit: 11
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
4 x 11 = 44

Multiplication Generator Pro

This is an improved multiplication generator. Here, the user can say the range. For example, the user can say he wanted to print the tables from 1 to 5 each having multiplication up to 11.

Output

Enter starting number and ending number(space seperated)4 7
Enter the limit: 10
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Think of ways to improve this. It would be fun.

scanf("%d%d", &startNum, &endNum);

Multiple inputs can be taken consecutively like this. The spacebar can also be used to enter values.

for(i = startNum; i <= endNum; i++) {
for (j = 1; j <= limit; j++) {
printf("%d x %d = %d\n", i, j, i * j);
}
printf("\n");
}

Here we had a for loop inside another for loop. These are known as nested loops.

That’s about loops. If you have been following till here through the earlier tutorials, you might have felt that programming is fun.

Yeah, it is.

Previous => Decision Making with Switch Statement

Next => Arrays — store multiple items using the same variable.

--

--

Shafi Sahal
Geek Culture

Developer, adventure lover and a truth seeker. Like to write about topics from a unique perspective. Twitter: https://twitter.com/_shafisahal.