Lesson 5. Loops

Overview

  • Exploring multiple ways to perform loops in C

Loops

  • Loops are used to repeat the sets of code repeatedly

Different Types of Loops

There are multiple types to perform loop operation in C. There are numerous ways to perform this, but we will be discussing the three major types.

  1. for loop
  2. while loop
  3. do while loop

for loop

This is the most common method of performing the loop. You can use this type of loop to perform un-terminated loop (meaning the loop will not end) or loop that will repeat for certain amount of times. The basic form of the for loop look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for([initialize];[loop condition];[command that will be performed each repeat]){
  Code that will be repeated each time;
}

The example code of this would be:

```c
#include <stdio.h>
int main(){
  for(int i=0;i<10;i++){
    printf("%d",i);
  }
  return 0;
}

This for loop will have i as a indicating variable. i will be initialized to 0 at the start of the loop. Next, the for loop will continuously check if the loop condirtion is true.If not, the for loop will be terminated. Lastly, for each repeat, the value of i will be increased by 1 every repeat.

To perform un-terminated loop with for loop, you can do:

1
2
3
for(;;;){
  Code that will be repeated each time;
}

So that there are no condition to meet to terminate the loop. However, for this specific case, using while loop is highly recommended.

while loop

While you can do everything you can imagine with for loop, while loop is still a very effective loop method. It is much faster that for loop and also easier to use. while loop only has one parameter, loopcondition other than for loop. An while loop looks like:

1
2
3
while([loop condition]){
  Code that will be reoeated each time;
}

As you can see, you can easily create a loop depending on the certain variable’s condition, other than than needing to add unnecessary initializing process and other things needed for for loop.

An un-terminated while loop looks like this:

1
2
3
while(1){
  Code that will be repeated each time;
}

An sample while loop that is not un-terminated is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>
int chk=0;
void f(int a){
  if(a%2==0){
    chk=1;
  }
  else{
    chk=0;
  }
}
int main(){
  while(chk==0){
    int tmp;
    scanf("%d",&tmp);
    f(tmp);
  }
}

This program above will perform a even-odd check for inputted integers. The program will continue to recieve the integers untill the even number is entred. As you can see, using while loop when possible can reduce the complexity of the code you write and easier to debug (find the error in logic or the code itself).

do while loop

do while loop works almost identical to while loop, except for one thing. When while loop’s condition is false before while loop is executed, the program will skip the while loop. This may seem okay, and it is. However, there are some cases where you should execude the code at least onece to keep the code working. do while loop is designed to suite the needs in those cases. An do while loop looks loke this:

1
2
3
do{
  Code that will be repeated each time;
} while([loop condition]);

How to terminate the loop

As we have seen from above, it is possible to create both terminatable and un-terminated loops. However in real-world situations, there are desires for terminating the loop at specific location of code, not start or the end of the loop. The best example for this is game. There can be many conditions that can lead to game over. It is still possible to create the game with traditional loops. However, that will cause a disaster in planning and designing the logic and structure of the program. To perform this, you can simply type break; inside the loop to exit the loop immediately. An example of this is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
int main(){
  int sum=0;
  while(1){
    int a;
    scanf("%d",&a);
    sum+=a;
    if(sum>30){
      break;
    }
    sum/=2;
    printf("%d",sum);
  }
  return 0;
}

Codes that make use of loops

The most common type of program using loops is sorting. There are numerous types of sorting algorithms, but the most basic sorting method is selection sort. Selection sort uses loops to go through all possible cases, and sorts them in either increasing or decreasing order.

Next lesson

There are limited things you can do only with loops. I personally believes that knowing about arrays will dramatically increase the number of things you can do with loops. Therefore, our next lesson will be about arrays.