Home / Computer Courses / Loop Structure In C++ Language

Loop Structure In C++ Language

Loop Structure In C++ Language

Loop Structure:

A Loop is a control structure. It uses to repeat a statement or a block of statements. Loop structure also called repetitive and iterative structure. I am going to explain an example of the loops concept. When we want to print table then the code without loops will look like this:

int table=2, i=1;

cout <<  table << “*” << i <<  “=” << table * i;

i++;

cout <<  table << “*” << i <<  “=” << table * i;

i++;

cout <<  table << “*” << i <<  “=” << table * i;

i++;

In this program, we need to write multiple lines of code for an iteration. So for repetition of a statement or statements, loops are used. There is two basic purpose of loops:

  • Repeat statement(s) for specifying the number of time. For example, print “Hello MyFSTech” 10 times.
  • Use to print the values in a sequence. For example, print the natural numbers from 1 to 10.

Now we will study the types of loops. There are three types of loops:

  1. for loop
  2. while loop
  3. do-while loop

Loop structure

  • while loop:

while loop executes when the condition will remain true. It uses when we do not know the number of iterations in advance. The syntax of while loop is:

while(condition){

    statement(s);

}

Here is exaample in which we will print “Hello MyFSTech” 5 times.

#include<iostream>

using namespace std;

int main(){

int n = 0;

while ( n < 5 ){

cout<<“Hello MyFSTech/n “;

n++;

}

return 0;

}

  • do-while loop:

The do-while loop executes one or more time if condition will true. In this loop, condition writes at the end of the body. It uses when we need to execute the statement(s) at least one time without knowing the number of iterations. The syntax of the do-while loop is:

do {

    statement(s);

            }while(condition);

Now we will print “Hello MyFSTech” 5 times using do-while loop.

#include<iostream>

using namespace std;

int main(){

int n = 0;

do {

cout<<“Hello MyFSTech/n “;

n++;

}while ( n < 5 );

return 0;

}

  • for loop:

The for loop also called counter-controlled loop. It is used when we know the specify numbers of the itereation. It used by mostly programmers because it is more flexible than others. The Syntax of for loop is:

for(initialization;condition;increment){

    statement(s);

}

Now we will print “Hello MyFSTech” 5 times using for loop.

#include<iostream>

using namespace std;

int main(){

int n;

for(n=0;n<5;n++) {

cout<<“Hello MyFSTech/n “;

}

return 0;

}

If you face any problem or query then contact us.

We have the collection of other topics of the C++ course and technology updates. Also, like our facebook page.

About Shumaiza Asghar

Shumaiza Asghar

Check Also

Blogger Website

How To Make Blogger Website and Earn Money | Basic Introduction

Contents 1 How To Make Blogger Website and Earn Money | Basic Introduction1.1 What is …

Leave a Reply

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