While programming, or say working on a solution, you might face a situation where you may have to repeat a statement or a set of statements over and over again. This is where we need a “loop”. Let’s learn different types of loops in programming and how to use them properly.
A loop in programming is a block of code that executes over and over again. Any loop will, and must have these three loop properties:
When a block of code enters into a loop it is called Loop Initiation. Here we need to define a variable with some value, to start the loop.
Next, when the loop is active, we need to set up one or multiple conditions to identify whether the loop is valid or not. This we call a Loop Condition. So, if the loop condition is valid, the block of code will execute again. If not, the execution will jump to the statement, next to the end of the loop.
At last, it is obvious that if the value of the loop variable set at initiation doesn’t change within the loop block of code, the loop condition will also not change. Hence, we need to make some changes to the variable which was set at loop initiation, so that the loop condition also changes. And so there would be some value or say, a condition when the loop is satisfied and no more valid. Then only the execution will come out of the loop. Otherwise, it will become an infinite loop condition and your program will be stuck in between that block of code forever.
All loops must satisfy this theory, or else the loop will go in an infinite looping, or it won’t execute at all.
Please note that while describing the types of loops I will not be using pseudocodes, rather I will be using code snippets using the language C. Even if you do not have experience in the language C, do not worry, as I have explained the code in detail.
The most common type of loops in programming, is a for-loop. Different programming languages will have their respective syntax to write a for-loop, but under the hood, its purpose and usage remain the same.
In a for-loop, initiation, loop-condition, and loop variable change are written in one single statement, like this:
for(int x=1; x<=10; x++){ //BLOCK OF LOOPING CODE }
Here, the keyword “for” is used to define a loop block. Any code written inside the curly braces ({…}) will be looped. Further, following the keyword “for“, the brackets (parentheses) define the loop properties.
Example:
for(int x=1; x<=10; x++){ print(x); }
Result:
12345678910
Another very common type of loops in programming that is frequently used in almost all programming languages is the while loop. In the while-loop, we initiate the loop variable before getting into a loop, check the condition, execute the looping code block, and within the looping code block we make changes in the loop variable.
int x=1; while(x<=10){ //BLOCK OF LOOPING CODE x++; }
Example:
int x=1; while(x<=10){ print(x); x++; }
Result:
12345678910
Similar to the while loop in programming, we also have a do-while loop. The only difference between a while loop and a do-while loop is that in a while loop the condition is checked prior to entering the loop, whereas in a do-while loop, the condition is checked once the loop body is executed.
int x=1; do { //BLOCK OF LOOPING CODE x++; }while(x<=10);
Example:
int x=1; do { print(x); x++; }while(x<=10);
Result:
12345678910
The difference between a while loop and a do-while loop is that, in a while loop, if the condition is not met, the loop body may not execute even once. But using the same logic, in a do-while loop, even with a non-satisfying condition, the loop body of a do-while loop will execute at least once.
Do-While Loop
int x=11; do{ print(x); x++; }while(x<=10);
Result:
11
While Loop
int x=11; while(x<=10){ print(x); x++; }
Result:
As you can see in the do-while loop, the loop body is executed once and then the condition is checked, so we see the value of x, 11 is printed. But in the case of the while loop, the condition is checked prior to entering into the loop and hence loop body wasn’t executed at all because the value of x is 11 and is not less than or equal to 10.
Here is another example of using loops.
Problem Statement: Display a Multiplication Table of 9, from 1 to 10.
int x=9; for(int i=1;i<=10;i++){ print x + ' times ' + i + ' = ' + (x*i) + '\n'; }
int x=9; int i=1; while(i<=10){ print x + ' times ' + i + ' = ' + (x*i) + '\n'; i++; }
int x=9; int i=1; do{ print x + ' times ' + i + ' = ' + (x*i) + '\n'; i++; }while(i<=10);
The special character used in this program is ‘\n’ which resembles a “new line”, and all these three programs will have similar output as follows:
9 times 1 = 9 9 times 2 = 18 9 times 3 = 27 9 times 4 = 36 9 times 5 = 45 9 times 6 = 54 9 times 7 = 63 9 times 8 = 72 9 times 9 = 81 9 times 10 = 90
Hope you have understood the basic concept of loops. If you have any queries, do write to me. I will try my best to answer them. Happy coding!