# Repeating Logic in Programming

Published in

--

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 timesI am looping 2 timesI am looping 3 timesI am looping 4 timesI 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 timesI am looping 2 timesI am looping 3 timesI am looping 4 timesI 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 timesI am looping 2 timesI am looping 3 timesI am looping 4 timesI 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: 4Enter the limit: 114 x 1 = 44 x 2 = 84 x 3 = 124 x 4 = 164 x 5 = 204 x 6 = 244 x 7 = 284 x 8 = 324 x 9 = 364 x 10 = 404 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 7Enter the limit: 104 x 1 = 44 x 2 = 84 x 3 = 124 x 4 = 164 x 5 = 204 x 6 = 244 x 7 = 284 x 8 = 324 x 9 = 364 x 10 = 405 x 1 = 55 x 2 = 105 x 3 = 155 x 4 = 205 x 5 = 255 x 6 = 305 x 7 = 355 x 8 = 405 x 9 = 455 x 10 = 506 x 1 = 66 x 2 = 126 x 3 = 186 x 4 = 246 x 5 = 306 x 6 = 366 x 7 = 426 x 8 = 486 x 9 = 546 x 10 = 607 x 1 = 77 x 2 = 147 x 3 = 217 x 4 = 287 x 5 = 357 x 6 = 427 x 7 = 497 x 8 = 567 x 9 = 637 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.