Ticker

20/recent/ticker-posts

Basic of Loops in c++ - Algomentor

Loops in C++ 

Loops in c++ for, while, do while

Hi, welcome to the series data structures and algorithms made easy of Loops are the basic concept of any programming language they help us to remove iterative process to write it again suppose you are writing a program that needs to the repetition up to 10 times rather than writes same things, again and again, we can apply a loop to it the loop will automatically iterate up to as many times as we want for example you write a program in we you ask a user to print the table of the given number so rather than use cout statement, again and again, you can put loop that can traverse from 1 to 10 and do than work for you without doing the mistake, loops are highly recommended when you write a program for automation because human are eligible for doing mistake computer don't. so apply then loop whenever you need to do iterative process we take one another example that user has a set of 10 value and for each value, you need to give user result that it's even or odd you can simply do it by applying a loop and conditional statement in them. we discuss both the programs in detail and we also give code for the same 😎. So don't worry learn with us the concept of the loop before starting programs we will discuss some basic questions related to the loop.

What are the Loops?

Loops are the iterative statement that helps us do repeat the same task may be with different values or the same value and help our program to automate itself.

How many types of Loop supported by C++?

There are three types of loops supported by the C++
  1. For Loop
  2. While Loop
  3. Do-While Loop
Note:- Now we will discuss three programs. All the program will give the same output we print the table of number that was input by the user

This program is done by for loop

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a Number: ";
cin>>n;
for(int i=1;i<=10;i++)
{
cout<<n*i<<endl;
}
}

This program is done by while loop

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a Number: ";
cin>>n;
int i=1;
while(i<=10)
{
cout<<n*i<<endl;
i++;
}
}

This program is done by do-while loop

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a Number: ";
cin>>n;
int i=1;
do
{
cout<<n*i<<endl;
i++;
}
while(i<=10);
}

The work done by all the program is the same then all print the table of a particular number that was given by the user.

Here is the output of all the code (They share the same input, output)

Loops in c++ for, while, do while


All loop can do the same work but they all are better at their write use case like when you need to check statement is true or false you can use while loop. When you need exit control you can use a do-while loop and when you need loop iteration step by step you can use for loop

Now finally we write another program when the user has a set of number and at each number, we need to print that it's even or odd 😉.

Here is the program

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a Number: ";
cin>>n;
for(int i=1;i<=n;i++)
{
int num;
cin>>num;
if(num%2==0)
{
cout<<"even"<<endl;
}
else
{
cout<<"odd"<<endl;
}
}
}

Here is the output for the code

Loops in c++ for, while, do while


In this program at each step, we ask the user to give us input, and just after that, we tell them that it's even or odd. Suppose if we don't want to use the loop we write program again and again to tell the output.

If you have dought regarding the post you can ask us in the comment or you can email us at algomentor@gmail.com We read all your email so feel free to ask about it.

Thank you very much for reading this post and give you valuable time to our post 😍. Please share this article with your coder friends if possible and loving our series of data structures and algorithms made easy.

Hi if you want to write a post on our website then please check this article on How to do a guest post.

Post a Comment

0 Comments