Loops in C – for while and do while

Loops in C – for while and do while

Looping concepts are very common in programming languages. However, there are several people out there who fear implementing loops. I have heard many complaining that they just do not understand where and when they must implement looping. Well, in this article, I will demonstrate everything – what are loops in C, where to use them, why we use them and of course; how we use them perfectly.

For the most general form of learning, I have chosen to demonstrate loops in C language. Let us study the whole concept in a procedural manner. First, what are loops?

What are loops in C language?

 As the word literally means, a loop is something that goes on and on until it is stopped by some external stoppage. Well, here it is the same. We repeat a certain code block or some several lines of codes multiple times. This number of times is defined and determined by a condition that is mentioned along with the loop.

Why do we need loops in our codes?

Now, let’s take an example. Just consider the following line of code –

printf(“Hello World”);

The above line prints “Hello World” on the output window. But, what if we want to print the same thing 5 times? Well, we can write the same line 5 times one after the other. What if we want to print it 1000 times?! Can we keep on writing that same line 1000 times? Is it feasible or possible? Well, it might be possible for some out there. But thankfully, this is bad coding technique. This reduces LOC (lines of code) and increases code complexity and document weight. So, how do we accomplish that? Here’s an example –

for(int i=1;i<=1000;i++){
printf(“Hello World”);
}

Here, we use a loop. So, loops in C or any other language for that matter has this functionality; to repeat a statement or group of statements for a desired number of times.

Where do we use loops in C?

This is most common question among most novice programmers. They start learning coding in high school or senior middle school and they just can get hold of this concept.

Even when I was a beginner in my 8th standard, I could not understand the possibility of loops in C. I knew the syntax; I knew what they are and why they are used. But, when it came to implementing; I just didn’t know where the right point was to strike.

Well, after around 12 years (or more I guess now) of programming in various languages; I can say one thing – the instinct of using loops is the main thing. You would know where and when to use loops in C only if you practice a variety of programs in C.

Normally we use loops when we need to build number patterns or displaying a string multiple times. This is because we need to move around the same data multiple times. Always remember, if you figure out that your program wants to display or even manipulate a single data more than once; it must be operated with the help of loops.

From an experienced point of view, let’s take an example. Suppose you want to access all the data that is available in a particular database table. Is it possible for you to access it without a loop? Not possible. There might be thousands or even ten thousands of records. You can only do it with the help of a loop. Set the counter to the table starting point and move through all the records until the end is reached.

How to use loops in C – their structures

There are 3 types if looping statements in C, C++, Java or any other language for that matter. They are-

  1. The for loop – This loop is generally used when we have a task that must be repeated a certain number of times. An initial value is given; that may be big or small. Then the condition is set to check whether the variable’ value has reached a certain mark or not. Until that time, all statements within the loop keep on executing repeatedly. Here’s a demo code for this –
/* prints * 5 times */
for(int i=1;i<=5;i++){
printf(“%c”,’*’);
}
  1. The while loop – This loop is used when we need looping until a certain condition is met. This condition might be a numerical value reaching process (just like for loop), or might be something else like string matching, reaching end point in a database query, etc. We can, however, use a while loop just like a for loop. Here’s an example to differentiate between while and for loop –
/* php code to run loop until end of database reached */
while($row = mysql_fetch_row){
echo $row[‘name’];
}
/* just like for loop */
int i=1;
while(i<=5){
printf(“%c”,’*’);
i++;
}
  1. The do-while loop – This loop is just like an alias of the while loop. It has the same functionality and structure.

The only striking difference between this and while loop is that; a while-loop checks condition and starts running. However, a do-while loop runs the including statements at least once and then checks the condition. So, the statements are run at least once even if the condition is false.

/* Structure of a do-while loop */
do{
// some statements here.
}
while(condition);

So, that’s that. That’s all about loops in C. If you have anything to say, any queries or suggestions – the comments section is all yours.

Comments are closed.