for vs while loop
Unraveling the Mystery of Multiple Loops in Programming: Exploring the Similarity of While, For, and Do…While Loops
Since a long I have wondered… why are there two kinds of loops in programming… and this article is about that. If you are wondering, hold on — there are three — do justice to do…while as well, then wait till I get to it as well.
Whenever you try to seek the answer that what on earth is the need for two (or more) types of loops…
The most typical response you might receive is →
When we know the number of iterations, we use the for loop; when we don’t, we use the while loop.
But lets dig deeper!
The while Loop
The syntax of while loop we have learnt thus far is
while(condition){
//code
}
If we have to print numbers 1 to 5, we would do something like:
In the right side we can see that the code gives the output 1 to 5, separated by the EOL character.
Using while loop we can also create an infinity loop as follows:
The for Loop
The syntax of for loop we have learnt thus far is:
for(initialization; condition; increment){
//code
}
Lets take an example of a for loop to print numbers from 1 to 5
We can see on the right that this produces output that has numbers 1 to 5.
Let us initialize the variable before the loop block and let us increment it at the end of the loop.
This for loop above still produces similar output, but now looks a lot more like a while loop.
We can also see the for loop like
for(statement1; condition; statement2){
//code
}
Here, statement 1 is any statement that runs once before the first iteration, and the statement 2 is any statement that runs once after every iteration.
To prove this, let us print something in place of both of these statements.
In the output we can see that I has been printed once and E has been printed at the end of every iteration.
Now that we know this we can even use for loops to create infinity loops.
The do…while loop
I said I would talk about do…while loops as well, so lets begin the discussion.
Do…while loops are said to check their conditions at the bottom of the loop, as opposed to for and while loops, which test their conditions at the top of the loop. It does sound like something different but do…while loops are just the while loops that run at least once, no matter the condition gets satisfied or not.
The syntax of a do…while loop is
do {
//code
}while(condition);
Any such do…while loop can be implemented as:
bool firstIteration = true;
while(condition || firstIteration){
firstIteration = false;
//code
}
The differences
Thus far we have inferred that anything we can do using a while loop can be implemented using a for loop. One question arises in mind that if there ever was the need to create more than one kind of loop.
When you look up on the internet and try to find out an answer, here is what you get →
“A for loop is typically used when you know exactly how many times you want to cycle through a certain section of code. The range of integers you enter in the header’s starting value, ending value, and step value (if necessary) fields is automatically iterated through by the for loop.
On the other hand, a while loop is typically used when you’re not sure how many times you want to loop through a certain section of code. As long as the condition you provide in the while loop’s header is true, the loop will continue to run.”
Although we have already seen how we can use for loops to loop through a certain section of code where the number of items in unknown, let us see how we can use a while loop to iterate through an array.
To solve a problem like “Given a 2D array of integers, find the sum of all elements in the array.” we would use for loops as below:
Let us now use while loops and see if we still get similar output.
Here, I changed the for loops
//code
int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum += arr[i][j];
}
}
//code
to the while loops like this:
//code
int sum = 0, i = 0;
while (i < 3) {
int j = 0;
while (j < 3) {
sum += arr[i][j]; j++;
} i++;
}
//code
which resulted in similar output. Now we are further sure that there are no differences and hence… for and while loops can be replaced whenever, wherever. They are meant to be together. While’ll be there and for’ll be near, and that's the deal my dear!
If I get a good response on this post, I will also talk in-depth about the while and for loops in python!
Feel free to connect with me on my LinkedIn.