Looping can make my work this much easier? I never knew.

You’re a young person, and you don’t know what this is. What’s a LOOP? If you’re on the internet, chances are you’ve heard the term, but have no clue what it means. Then let me help you with this. Check out what are loops and its working.

What do you mean by LOOPS?

In Simple term Loops are a programming element that repeat a portion of code a set number of times until the desired process is complete. Repetitive tasks are common in programming, and loops are essential to save time and minimize errors.

Suppose you need to print a thing again and again!

Suppose you want to print "Virtual Developers !" 5 times.
This can be done in two ways.

First One is by printing it again and again for 5 times:

CODE:

#include <iostream>
using namespace std;

int main( )
{
  cout<<"Virtual Developers !"<<endl;
  cout<<"Virtual Developers !"<<endl;
  cout<<"Virtual Developers !"<<endl;
  cout<<"Virtual Developers !"<<endl;
  cout<<"Virtual Developers !"<<endl;
  return 0;
}

Output :

Virtual Developers !
Virtual Developers !
Virtual Developers !
Virtual Developers !
Virtual Developers !

Second way is by using loops :

These are the Types of Loops commonly taught and used:
1. Entry Controlled Loop
 (i) while Loop
 (i) for Loop
2. Exit Controlled Loop
 (i) do-while Loop

Let's see how to write these code under different loop ways:

1. While Loop :

This is the simplest types of loop. It execuute a block of statement as long as a specified condition is true.

CODE:
#include <iostream>
using namespace std;

int main( )
{
  int n = 10;
  while(n>=0){
   cout<<n<<"\t";
   n--;
  }
}

Output :

10 9 8 7 6 5 4 3 2 1 0

1. For Loop :

When you know exactly how many times you want to loop through a block of code, use the for loop instead of while loop.

CODE:
#include <iostream>
using namespace std;

int main( )
{
  for(int n = 10; n >= 0; n--){
   cout<<n<<"\t";
  }
}

Output :

10 9 8 7 6 5 4 3 2 1 0

1. Do-While Loop :

A do-while loop is similar to a while loop, except that a do-while loop is guaranteed to execute at least one time. The Conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.

CODE:
#include <iostream>
using namespace std;

int main( )
{
  int n = 10;
  do{
   cout<<n<<"\t";
   n--;
  }while(n >= 0);
}

Output :

10 9 8 7 6 5 4 3 2 1 0