### Looping in C Programming

## Introduction of loop

Looping is a way to repeat a block of statement for the finite number of times. Here finite means that a loop should be terminated automatically or whenever the user wants.We also learn that a program is a collection of instruction. In C there are various control instructions that control the way to execute the program.

**Sequence control instruction:**In which the program statements are executes in the order in which they are written.**Decision making control instruction:**In which we make a choice by using the if-else construct.**Switch/case control instruction:**Alternate choice of if-else construct.**Loop control instruction:**Used to perform the task repeatedly.

We have already learned first 3 control instruction, and now we are going to discuss the last one loop control instruction. Let's start the discussion with an example.

Suppose you want to print "Hello", then you write **print("Hello");** once. But if I tell you, print "Hello" for 100 times, then this is not a good way. If there is a way to repeat **printf("Hello");** for 100 times automatically then that can be optimize solution. The way is called Looping/Iteration.

The way to achieve this repetition is that we repeat a task still the specified condition, If the condition becomes false then the repetition will automatically stop. This is shown in the following program snippet.

```
i=1;
while(i<=100)
{
printf("Hello");
i++;
}
```

In the above program snippet you can see that i=1 statement initialize "i" with 1, then condition "i<=100" imposed with while and i++ increment i by one each time. Don't worry about the while keyword. we will cover it in a few minutes.

In C, there are two types of loop as given below:

### Entry Control loop/Top tested loop

In this type of loop, the first condition is evaluated, if the condition is true then statement within the loop are executed otherwise control immediately jumps out of the loop.

**Example: while loop, for loop**

### Exit Control loop/Bottom tested loop

In this type of loop once the statements of the loop are executed and then the condition is evaluated, if the condition becomes true, then the statement within the loop is again executed otherwise control immediately jumps out of the loop.
**Example:do-while loop**

## Syntax of while loop

```
initialization;
while(condition)
{
statement-1;
statement-2;
...........;
...........;
}
```

### Syntax explained

The block associated with while loop is called **body of loop**.In the above loop syntax, first initialization is performed, the condition is checked, if the condition is true the control reaches inside the loop and body of the loop is executed, When control reaches the end of the loop, automatically move to the condition and loop is again executed, if the condition evaluates true. This process is repeated until the condition remains true. As soon as the condition becomes false control jumps outside the loop and rest of the code will be executed.

## Program: Print the series 1,2,3,4,.....N

```
#include<stdio.h>
int main()
{
int i,n;
printf("Enter how many terms: ");
scanf("%d",&n);
i=1;
while(i<=n) {
printf("%-4d",i);
i++;
}
return 0;
}
```

### Output

```
Enter how many terms: 10
1 2 3 4 5 6 7 8 9 10
```

### Program Description

At first Line-7 and 8 input the value of n from the user. suppose user input 5 for n.For the step by step execution see the following table,Work with it and find out the logic

Round | i | i<=n | Condition ? | Output | i++ |
---|---|---|---|---|---|

1 | 1 | 1<=5 | T | 1 | 2 |

2 | 2 | 2<=5 | T | 2 | 3 |

3 | 3 | 3<=5 | T | 3 | 4 |

4 | 4 | 4<=5 | T | 4 | 5 |

5 | 5 | 5<=5 | T | 5 | 6 |

6 | 6 | 6<=5 | F | Loop Terminated |

Note: within the %-4d is used make the 4 spaces to print the number and number will be left aligned in the available spaces remaining spaces will be printed on the right side.

## Program:Print 1,3,5,7................N terms

```
#include<stdio.h>
int main()
{
int n,cnt,term;
printf("Enter how many terms: ") ;
scanf("%d",&n);
cnt=1;term=1;
while(cnt<=n)
{
printf("%-4d",term);
cnt++;
term+=2;
}
return 0;
}
```

### Output

```
Enter how many terms: 10
1 3 5 7 9 11 13 15 17 19
```

### Program Description

Suppose the value of n is 5, input by the users. Trace the program with the help of given table and find out the logic.

Round | cnt | term | cnt<=n | Condition? | Output | cnt++ | term+=2 |
---|---|---|---|---|---|---|---|

1 | 1 | 1 | 1<=5 | T | 1 | 2 | 3 |

2 | 2 | 3 | 2<=5 | T | 3 | 3 | 5 |

3 | 3 | 5 | 3<=5 | T | 5 | 4 | 7 |

4 | 4 | 7 | 4<=5 | T | 7 | 5 | 9 |

5 | 5 | 9 | 5<=5 | T | 9 | 6 | 11 |

6 | 6 | 11 | 6<=5 | F | Loop Terminated |

In the above given table it is clear that if n is a natural number than 2n-1 generate the odd number series.Natural number means 1,2,3,4,......N.The following snippet implement this logic.In the following snippet cnt is a natural number.

```
cnt=1;
while(cnt<=n) {
printf("%-4d",2*cnt-1);
cnt++;
}
```

## Program:Print 1,0,1,0................N terms

```
#include<stdio.h>
int main()
{
int i;
printf("Enter how many terms: ") ;
scanf("%d",&n);
i = 1;
while(i <= n)
{
printf("%d ",i % 2);
i++;
}
return 0;
}
```

### Output

```
Enter how many terms: 10
1 0 1 0 1 0 1 0 1 0
```

### Program Description

i | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|

i % 2 | 1 % 2 | 2 % 2 | 3 % 2 | 4 % 2 | 5 % 2 |

Output | 1 | 2 | 3 | 4 | 5 |

- >In the above table you observe that if the mod is performed with any number the possible output is 0 or 1. When a number is completely divisible by 2, then the remainder is 0 otherwise 1. so we rotate the loop from 1 to n and print the output of number%2.
- The mod operator show the same behavior with other numbers also. Suppose you mod a number by 3 then the possible output is 0,1,2 because in every 3rd step number is divisible by 3 and output will be 0, then for the next successive number output is 1 and then for next successive number output is 2 and then 0.
- So the conclusion is that a%n returns the output 0 to n-1, where a can be any integer number.

## Program: To print 1,-1,1,-1................N terms

```
#include<stdio.h>
int main() {
int n,cnt,term;
printf("Enter how many terms: ");
scanf("%d",&n);
cnt=1;term=1;
while(cnt<=n) {
printf("%-4d",term);
term=term*-1;
cnt++;
}
return 0;
}
```

### Output

```
Enter how many terms: 10
1 -1 1 -1 1 -1 1 -1 1 -1
```

### Program Description

Suppose that the value of n is 5, input by the user.

Round | cnt | term | cnt <= n | Condition? | printf("%d ",term) | term = term * -1 |
---|---|---|---|---|---|---|

1 | 1 | 1 | 1 <= 5 | T | 1 | -1 |

2 | 2 | -1 | 2 <= 5 | T | -1 | 1 |

3 | 3 | 1 | 3 <= 5 | T | -1 | 1 |

4 | 4 | 1 | 4 <= 5 | T | 1 | -1 |

5 | 5 | -1 | 5 <= 5 | T | -1 | 1 |

6 | 6 | 1 | 6 <= 5 | F | Loop Terminates |

One more possible way to print the above series is possible. The logic is if you observe the above series, then you see that on each even term -1 gets printed and on each odd term 1 gets printed. The logic is implemented in the following program snippet.

```
cnt=1;
while(cnt<=n) {
if(cnt%2==0)
printf("%-4d",-1)
else
printf("%4d",1);
}
```

## Program: To print 1,2,3,0,1,2,3,0,.....N terms

```
#include<stdio.h>
int main() {
int n,cnt;
printf("Enter how many terms: ");
scanf("%d",&n);
cnt=1;
while(cnt<=n) {
printf("%-4d",cnt%4);
cnt++;
}
return 0;
}
```

### Output

```
Enter how many terms: 10
1 2 3 0 1 2 3 0 1 2
```

### Program Description

The logic in the above program is not new. Observe the given series,you see that after print 1,2,3 again 0 gets printed.That can be achieved by printing the output of mod(%) by 4 for an increasing sequences running from 1 to n.

## Comments