What is Loop

In programming language loop is sequence of statement that used for repeated of statement until a specific condition not reached.Computers not only do calculations, but also will do them over and over and over.

To get a computer to repeat its work you need a loop statement. Looping statements have many uses. For example, loops are used to count the number of words in a document or to count the number of accounts that have past due balances.By the example we can understand in easy way without using loop

#include <iostream>
using namespace std;
int main(){
clrscr();
cout<<“Codeverb.com 1”endl;
cout<<“Codeverb.com 2n”;
cout<<“Codeverb.com 3n”;
cout<<“Codeverb.com 4n”;
cout<<“Codeverb.com 5n”;
return 0;
}

Output
Codeverb.com 1
Codeverb.com 2
Codeverb.com 3
Codeverb.com 4
Codeverb.com 5

Suppose that you want to print 10,20,100,1000 times then just think it’s possible but very difficult to type or copy paste. By using loop we can print many types only two or three line of code.There are three types of loop used in c++.

  1. or
  2. while
  3. do while

For Loop In C++

A for loop executes statements in the loop body, while the loop condition is true. Before the loop initialization statement is executed exactly once. After each cycle, the iteration execution part is executed.A for loop is defined as.

for (/initialization statement/; /condition/; /iteration execution/)
{
// body of the loop
}

Or loop used for executes for a group of statement repetation until condition not reached.First initilize then check print statement and after increment/decrement then check condition and again print statement.Syntax of for loop in c++

for(initilization;condition;incr/decr)
{
statement/expression;
}

Simple example of for loop in c++ Print Codeverb.com message for 5 times by using for loop

#include <iostream>
using namespace std;
int main(){
int i;
clrscr();
for(i=1;i<=5;i++)
{
cout<<“Codeverb.com %dn”,i;
}
return 0;
}

Output
Codeverb.com 1
Codeverb.com 2
Codeverb.com 3
Codeverb.com 4
Codeverb.com 5

Just look here we do not repeat statement Codeverb.com but only two or three lines of code we are printing here for 5 time Codeverb.com messages.Explanation of the placeholder statements:

initialization statement: This statement gets executed only once, at the beginning of the for loop.You can enter a declaration of multiple variables of one type, such as int i = 0, a = 2, b = 3. These variables are only valid in the scope of the loop.

Variables defined before the loop with the same name are hidden during execution of the loop.condition: This statement gets evaluated ahead of each loop body execution, and aborts the loop if it evaluates to false.

iteration execution: This statement gets executed after the loop body, ahead of the next condition evaluation, unless the for loop is aborted in the body (by break, goto, return or an exception being thrown).You can enter multiple statements in the iteration execution part, such as a++, b+=10, c=b+a.

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Loop In Cpp

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password