Description
Computers are great at crunching piles of data, and piles of data usually takes the form of sequences, streams, tables and so on. Such data sets usually need to be iterated over. At the very least, some statements may need to be executed more than once. CPP Loops and iteration statements are used for this purpose.
A block of code that performs a task repetitively for many times in a sequential order is called as a Loop. CPP Loops are of three types. They are:
- For loop
- While loop
- Do-while loop
Description
For Loop in CPP Loops, is used to initialize a variable, check condition and increment (or) decrement the variable. All these three functions are declared in a single statement and can be repeated as per requirement.
Syntax
for( initialization; condition; increment/decrement)
{
statements;
}
Flow of control for ''for'' loop
1)
Initialization
: In this section, the variables are declared and initialized. Initialization will be executed only once and if it satisfies it goes to condition section.
2)
Condition
: In this section, the condition is checked. If true, then control enters the loop and the statements are executed. Then the control goes to increment section. But if the condition is false, the CPP Loops gets terminated.
3)
Increment or Decrement
: Once the CPP Loops gets executed the flow of control jumps back to this section. Here the value gets incremented (or) decremented and again the control goes to condition section. This section is optional.
Flow chart
Example
[c]
#include <iostream>
using namespace std;
int main()
{
int x, n, fact= 1;
cout<<"Enter a positive integer: "; cin>>n;
for (x = 1; x <= n; ++x)
{
fact= fact* x;
}
cout<< "Factorial of "<< n <<" = "<<factorial;
return 0;
}[/c]
Output:
[c]
Enter a positive integer:
5
Factorial of 5 = 120[/c]
Description
While loop is considered when it is unknown how many times to execute.
Compiler only checks the condition is true or false.The While loop will only execute the statement if the expression is true. After the statement is executed, it will once again evaluate the expression and repeat as needed.
Syntax
while( condition )
{
statements;
}
Flowchart
Example
[c]
#include<iostream>
using namespace std;
int main()
{
int x = 0;
while ( x < 10 )
{
cout<< x << endl;
x++;
cout<<"after incrementing, the value of x is ";
}
return 0;
}[/c]
Output:
[c]
0
after incrementing, the value of x is 1
after incrementing, the value of x is 2
after incrementing, the value of x is 3
after incrementing, the value of x is 4
after incrementing, the value of x is 5
after incrementing, the value of x is 6
after incrementing, the value of x is 7
after incrementing, the value of x is 8
after incrementing, the value of x is 9
after incrementing, the value of x is[/c]
Description
do...while loop is mainly used when want to execute the loop statements at least once, irrespective of the condition given. Those statements are kept in do and then the condition is checked.
Syntax
do
{
Statements;
}
while( condition);
Flow chart
Example
[c]
#include <iostream>
using namespace std;
int main()
{
float n, sum = 0.0;
do
{
cout<< "Enter a number: "; cin>> n;
sum= sum+n;
}
while(n<= 0.0);
cout<<"Total sum = "<< sum;
return 0;
}[/c]
Output:
[c]
Enter a number: 5
Total sum = 5[/c]
Description
Loop controllers alter the execution of the loop by destroying the created objects and move to other point in the program.
There are three Loop Control statements in C++:
- Break statement.
- Continue statement.
- Goto statement.
Description
Break statement allows to exit from the loop at any time of loop execution.
Example
[c]
#include <iostream>
using namespace std;
int main()
{
int a;
a = 0;
while ( a < 15 )
{
a++;
cout << "Welcome to splessons"<<endl;
if ( a == 5)
break;
}
return 0;
}[/c]
Output:
[c]
Welcome to splessons
Welcome to splessons
Welcome to splessons
Welcome to splessons
Welcome to splessons[/c]
Description
Continue Statement is used to pass a warning to the compiler to check the condition once again. The statement after Continue is never executed as the control goes back to check the condition.
Example
[c]
#include <iostream>
using namespace std;
int main ()
{
for (int n=5; n>0; n--)
{
if (n==5)
{
cout << n << endl;
continue;
}
}
cout << "go and check the condition";
}[/c]
Output:
[c]
5
go and check the condition[/c]
Description
Goto() statement alters the loop execution and move the control to the given position. Do not use Goto in loops as it disturbs the execution and makes the program complex.
Syntax
goto identifier_name;
//statements;
//statements;
identifier_name:
//statements;
//statements;
Example
[c]
# include <iostream>
using namespace std;
int main()
{
float number, avg, sum = 0;
int x, a;
cout<< "Maximum number of inputs: "; cin >> a;
for(x=1; x <= a; ++x)
{
cout<<"Enter a number"<< x <<": "; cin>>number;
if(number < 0)
{
goto splessons; /* Control of the program moves to splessons */
}
}
splessons:
sum =sum+number;
avg=sum/(x-1);
cout<<"\nAverage = "<<avg;
return 0;
}[/c]
Output:
[c]
Maximum number of inputs: 5
Enter a number1: 3
Enter a number2: 3
Enter a number3: 4
Enter a number4: 8
Enter a number5: 9
Average = 1.8[/c]
Description
Infinite statements can also be generated using loops. This is done using "For Loop" by making condition section empty. By default, the compiler takes the condition as true and executes the loop.
Example
[c]
#include <iostream>
using namespace std;
int main ()
{
for( ; ; )
{
cout <<"This is infinite loop and you are reading splessons\n" << endl;
}
return 0;
}[/c]
To stop this infinite loop use "Ctrl+C" key.
Key Points
- CPP Loops performs repeated execution to make a condition become true
- For loop initializes variables, checks condition and increments/decrements. This can be used when there is sequential input/output.
- While loop executes after the condition becomes true
- Do-while loop executes at least once even if the condition is false
Programming
Tips
In for loop, initialization cannot be changed once the execution goes to condition checking. So, ensure that check it before execution.