Sharp Tutorial
loop in c++

Python

Java

C

C++

HTML/CSS

Java Script

PHP

SQL

C Programs

introduction to c++

  • C++ Home
  • Introduction to C++
  • History features
  • Sample Programs
  • cout , cin , endl
  • Data type and Operators
  • if else in c++
  • loop in c++

Oops in C++

  • Object / Class
  • Constructor
  • Function & Constructor Overloading
  • Inheritance
  • Overriding

Advanced C++

  • Strings in C++
  • Exception Handling
  • Special Functions
  • C++ Programs

loops in c++

Suppose, block of statements have to be executed repeatedly,
then instead of writing those statements multiple times, loops are used.

The looping statements are used to execute a single statement or block of statements repeatedly until the given condition is FALSE. So if i have to print myname 100 times, then instead of writing printf hundred times, we can use loop with condition that executes 100 times.

Types of the Loops in C++ :
  while statement
  do-while statement
  for statement

1. While loop :

the syntax for while loop is,
   while(condition here)
       {
           /*statement to be executed for true condition*/
       }

1. C++ program to print table of the given no using while loop :

#include<iostream>
using namespace std;
int main()
{
int n,i=1;
cout<<"enter the number";
cin>>n; while(i<=10) { cout<<n <<"*"<<i<<"="<<(n*i)<<endl;
i++;
} return 0;
}

Output of the above program is as following.

2) For loop :

In for loop, the first part begins with variable’s initialization, followed by the condition and increment/decrement operation on the variable.
The first part (initialization) and the last part (increment/decrement) may not be present, but the semicolon is there.
general syntax for for loop is,

     for (initialization; condition; increment/decrement)
       {
           // body statement to be executed
       }

1. C++ program to print the given pattern using for loop :

1

1  2

1  2  3

1  2  3  4

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"enter the limit";
cin>>n; for(int i=1; i<=n; i++) {
for(int j=1; j<=i ; j++)
{
cout<<j <<" ";
}
cout<<endl;
}
return 0;
}

Output of the above program is as following.

3) Do While loop :

In do while loop, body of the loop is executed first and after that, the condition is checked.
Do while loop executes at least once even if the condition is not true for a single time.
general syntax for do while loop is,
      do
       {
            // body statement to be executed
        }
      while (condition here);

Enquiry about Course

Ask your Question

Click Here

Sharp (2) Tutorials

Video/ C Introduction

Watch video in full size

Video tutorial

Follow by Email
Facebook
Facebook
fb-share-icon
YouTube
Copyright © Sharp Tutorial
Build with WordPress
Wordpress Social Share Plugin powered by Ultimatelysocial
Sharp Tutorial