Contents
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:
- for loop
- while loop
- do-while loop
-
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 related to Loop Structure then contact us.
We have the collection of other topics of the C++ course and technology updates. Also, like our facebook page.
Howdy would you mind letting me know which
web host you’re using? I’ve loaded your blog in 3 different internet browsers and I must say this blog loads a
lot faster then most. Can you recommend a good internet hosting provider at a honest price?
Many thanks, I appreciate it!
Hi Indiana!!!
Thanks for showing interest in our Blog. Hosting has nothing to do with the speed of Blog, Speed is because of Better Search Engine Optimization Technique & also Because of Better Blog Optimization. If you are interested in SEO of your website then you may contact us through comment…
Thanks.