• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Structures » Iteration Statements or Loops in C++
Next →
← Prev

Iteration Statements or Loops in C++

By Dinesh Thakur

The statements that cause a set of statements to be executed repeatedly either for a specific number of times or until some condition is satisfied are known as iteration statements. That is, as long as the condition evaluates to True, the set of statement(s) is executed. The various iteration statements used in C++ are for loop, while loop and do while loop.

The for Loop

The for loop is one of the most widely used loops in C++. The for loop is a deterministic loop in nature, that is, the number of times the body of the loop is executed is known in advance.

The syntax of the for loop is

for(initialize; condition; update) {
  //body of the for loop
}

Note that initialize, condition and update are optional expressions and are always specified in parentheses. All the three expressions are separated by semicolons. The semicolons are mandatory and hence cannot be excluded even if all the three expressions are omitted.

To understand the concept of the for statement, consider this example.

Example: A program to display a countdown using for loop

#include<iostream>
using namespace std;
int main() {
  int n;
  for(n=l; n<=10; n++)
    cout<<n<<" "; // body of the loop
    cout<<"\n This is an example of for loop!! !";
    //next statement in sequence
return 0;
}

The output of this program is

1 2 3 4 5 6 7 8 9 10

This is an example of for loop!! !

for loop using comma operator: for loop allows multiple variables to control the loop using comma operator. That is, two or more variables can be used in the initialize and the update parts of the loop. For example, consider this statement.

for (i=1,j=50 ;i<10;i++, j- -)

This statement initializes two variables, namely i and j and updates them. Note that for loop can have only one condition.

The while Loop

The while loop is used to perform looping operations in situations where the number of iterations is not known in advance. That is, unlike the for loop, the while loop is non deterministic in nature.

The syntax of the while loop is

while(condition) {
  // body of while loop
}

These points should be noted about the while loop.

• Unlike for loops where explicit initialize and update expressions are specified, while loops do not specify any explicit initialize and update expressions. This implies that the control variable must be declared and initialized before the while loop and needs to be updated within the body of the while loop .

• The while loop executes as long as condition evaluates to True. If condition evaluates to False in the first iteration, then the body of while loop never executes.

• while loop can have more than one expression in its condition. However, such multiple expressions must be separated by commas and are executed in the order of their appearance.

To understand the concept of the while loop, consider this example.

Example : A program to determine the sum of first n consecutive positive integers

#include<iostream>
using namespace std;
int main() {
  int n,i,sum; // i is the control variable
  cout<<" Enter the number of consecutive positive"<< 
  "\n integers(starting from 1): ";
  cin>>n;
  sum=0;
  i=l; // initialize expression
  while (i<=n) {
   sum+=i;
   ++i; //update expression
}
cout<<"\nThe sum is "<<sum;
return 0;
}

The output of the program is

Enter the number of consecutive positive integers(starting from 1): 9

The sum is : 45

The do-while loop: in a while loop, the condition is evaluated at the beginning of the loop and if the condition evaluates to False, the body of the loop is not executed even once. However, if the body of the loop is to be executed at least once, no matter whether the initial state of the condition is True or False, the do-while loop is used. This loop places the condition to be evaluated at the end of the loop.

The syntax of the do-while loops is given here.

do {
  //body of do while loop
}while(condition) ;

To understand the concept of do-while loop, consider this example.

Example : A program to calculate the sum of an Arithmetic Progression (AP)

#include<iostream>
using namespace std;
int main () {
  int a,d,n,sum,term=0; /*a is the first term , d is
  the common difference, n is the number of terms to be summed */
  cout<<"Enter the first term, common difference,"
  <<"and the number of terms to be summed"
  <<"respectively:\n";
  cin>>a>>d>>n;
  sum=0;
  int i=1;
  cout<<"\nThe terms are ";
  do //do-while loop {
   term= a+ (i-1)*d;
   sum+=term; //Adding each term to 'sum'
   cout<<term<<" “;
   ++i;
}
while (i<=n) ;
cout<<"\nThe sum of A.P. is "<<sum;
return 0;
}

The output of the program is

Enter the first term, common difference, and the number of terms to be summed respectively:

3

6

5

The terms are 3 9 15 21 27

The sum of A.P. is 75

Note that, all the three loops (for, while and do-while) can be nested within the body of another loop

You’ll also like:

  1. Define iteration Statements
  2. Conditional Statements in C++ or if Statements
  3. What is the difference between ‘for’ and ‘while’ loops
  4. Nested Loops in C
  5. Nested FOR Loops in C
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW