Introduction to Loops
Loops are essential elements of any programming language we pick. In C programming also, loops play a vital role. In this tutorial, I will introduce loops in C programming and by the end you will know why loops are so important in our programming journey. So, without any further ado, let’s get started.
Why are loops essential in programming?
Loops are essential in programming because they allow us to repeat a block of code a certain number of times. Let’s say we want to print “Hello World” 10 times. The following code will do the job:
#include <stdio.h>
int main() {
printf("Hello world\n");
printf("Hello world\n");
printf("Hello world\n");
printf("Hello world\n");
printf("Hello world\n");
printf("Hello world\n");
printf("Hello world\n");
printf("Hello world\n");
printf("Hello world\n");
printf("Hello world");
return 0;
}
Output:
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
The above program will work as expected, but I would never recommend you to write the code the way I have written above. Following are the reasons:
- The code is repetitive. Now, imagine if you would have to print “Hello World” 100 times. Don’t you think it is troublesome to copy and paste the same line of code a hundred times?
- The code is unnecessarily lengthy and requires a lot of manual work of copying and pasting.
Instead, the best approach is to use loops. The following program uses one type of loop called the while loop in C programming to repeat the same printf()
function a total of 10 times:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 10) {
printf("Hello world\n");
i++;
}
return 0;
}
Output:
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Don’t worry about the technical details or the nitty-gritty of the program. Just enjoy the beauty of the loops that save us from the tedious task of copying and pasting a bunch of stuff.
The above program works the same as before, but this time, instead of 10 lines of repetitive code to print “Hello World”, we now have only 5 lines of code. And the best part is that the same code can be used to print “Hello World” 100 times (if we want) by making a small tweak inside the while statement as follows:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 100) { // 10 is replaced by 100 and viola!
printf("Hello world\n");
i++;
}
return 0;
}
The above program displays “Hello World” 100 times, but the lines of code remain unchanged. That’s powerful!
I hope I have convinced you to use loops from now on to repeat a bunch of stuff. I will teach you to use the different tools available in our arsenal. Apart from the while loop, we will study the other two types:
- The for loop
- The do-while loop
These loops have their own advantages and use cases which will be understood properly in the upcoming tutorials. And guess what? We’ll also cover a couple of other things that’ll make our lives as programmers a whole lot easier.
Alright, peeps! We’re about to dive into the exciting world of loops and uncover all the hidden secrets.
Leave a comment