Loops

Okay do you like doing repetitive tasks? No? Well you’re not alone then. Humans in general avoid mundane repetitive tasks. But computers? Well no. Loops are a way of telling the computer to do a task several times. Without loops we would be writing the same portion of the code multiple times which is not fun isn’t it?

For Loop

The most common loop we use is known as the for loop. In code it looks something like this..

for(int i=0;i<5;i++)
{
 std::cout<<"Hello\n;
}

The above code will print Hello to the console five times. Now why is that?

Well let’s look at the code in detail. The first term inside the bracket is what is known as the initializer where we declare an int variable and set it to zero. The next term is the condition and it tells the computer to run whatever is inside the parenthesis after the for line if that condition is true. In our case there’s nothing there so nothing is going to happen. This code will only delay the execution of our program and sort of works like a timer. The next term is where i is being incremented by one so that there comes a time when i<5 is no longer true and the for loop will stop executing.

It’s not important to set the initializer inside the for loop, and we can omit the first term if we want. For example this code will work fine and print Hello five times.

int i=0;
for(;i<5;i++)
{
 std::cout<<"Hello\n;
}

Infinite Loop

Now let’s see what happens if we omit the second term…

int i=0;
for(;;i++)
{
 std::cout<<"Hello\n;
}

This will go on printing Hello to the console for infinity as we have not provided any condition and the for loop will run for ever.

Now we can also omit the third term and that too will result in an infinite loop as we are not changing the value of i anywhere and the condition to stop the loop is never met.

We can even do something like this and this will run the loop five times…

int i = 0;
for (; i<5; )
{
 std::cout << "Hello World!\n";
 i++;
}

While Loop

Next up is the while loop. Lets see what it looks like…

int i = 0;
while(i<5)
{
 std::cout << "Hello World!\n";
 i++;
}

A while loop runs as long as the condition inside the brackets is true. Here the value of i is being incremented inside the while loop.

Now lets look at some code. Can you tell what will happen?

while (0 )
{
 std::cout << "Hello World!\n";
}

This loop will not run even once as the value inside the bracket is 0, which in computer term stands for false and so the loop will never run.

We can change this to an infinite loop by doing something like this…

while (1)
{
 std::cout << "Hello World!\n";
}

By providing 1 inside the brackets which stands for true this makes the condition always true and so Hello World will be printed for infinity.

Do While Loop

Next up is the do while loop. A feature of do while loop is that it gets executed at least once. In code it looks like this..

int i = 0;
do
{
 std::cout << "Hello World!\n";
 i++;
} while (i<5);

As you can see as the condition is provided at the end, the part inside the parenthesis gets executed at least once.

Conclusion

That’s all about loops for now. Cheers!

Further Readings

If you liked this article, then please subscribe to our YouTube Channel. You can also find us on InstagramFacebook and Twitter.

READ – CONNECT – BOOST – CREATE

Related :

Follow :