Introduction to Repetition Control Statements

A repetition statement (or the looping statements or loops) enables the program to repeat an action as long as a particular set condition remains true. Its form goes as follow, “As long as you are at school, I will continue to pay your allowances”. This means that the action of paying you allowances will be done over and over again on condition that you go to school, but if you finish or drop out of school, you will have to fend for yourself. There are three types of repetition (loop) statements. They include the while,do.....while and for loop statements

while Repetition Statements

The while loop statements allows the program to perform a particular action while a set condition is true. In the case of our going to school example, the while statement can be stated as “while you go to school, I will pay your allowances”. Now this statement gives you the ability to determine how much you will earn while you go to school. Let us assume that going to school begins from nursery through secondary to completing university with a bachelor’s degree. Now consider that years of schooling are 3, 7 , 5 and 3 for nursery, primary, secondary and university education respectively . We also assume that you get your allowances on school term basis which an equivalent of three months. So in all you have 18 years of schooling resulting into 72=(18*12/3) terms for earning your allowances.
Let us say that each term you will be paid US$ 100 and this pay will remain constant until the end of the school time. With this information we can use the while loop to calculate for the total allowances you will get during entire school time. A while loop is written as follows  

while(condtion) { statements to execute; }

First we need to break down the example. We know the terms you will spend at school (i.e terms=72). And we also know how much you will earn each term (pay=100). So we can use the while statement as follows.
#include<iostream>
using namespace std;
int totalAllowance(int schoolTerms, int allowances)
{
 int terms=schoolTerms;
 int pay=allowances ;
 int totalPay=0;
int t=1; // T is a specific term of the 72
while(t<=terms)
{
 totalPay=totalPay+pay; //add the allowance for each term to the total
t++; //increament the term by one
}
 return totalPay;
}
 int main()
{
int terms=72; \
int pay=100;
cout<<"The total allowance for your school time is US$ " < <totalAllowance(terms,pay)<<endl;
return 0;
}
The total allowance for your school time is US$7200

do..while statement

The do....while loop allows for executing an action at least before a condition is tested. To enable the loop automatically execute its statement at least once, the while condition is put at the end instead of the beginning. The loop executes its body before the condition is tested. So whether condition is true or false, the loop will execute at least once. The do...while statment is written as follows;

do { statement to execute at least once } while ( condition );

In a do...while statement, a semi-colon must be included at the end of the while statement. In the example of getting allowances while you go to school, we can say that we are assure of your going to school at least the first time. We are however not sure that you will continue till the end. So we modify the example to allow you earn your allowance the first time you go to school. The subsequent allowances will depend on whether you keep at school or not.
#include<iostream>
using namespace std;
int totalAllowance(int schoolTerms, int allowances)
{
 int terms=schoolTerms;
int pay=allowances ;
int totalPay=0;
int t=1; // T is a specific term of the 72
do
{
totalPay=totalPay+pay; //add the allowance for each term to the
total t++; //increament the term by one
}
while(t<=terms);
return totalPay;
}
 int main()
{
int terms=72;
int pay=100;
cout<<"The total allowance for your school time is US$" <<totalAllowance(terms,pay)<<endl;
 return 0;
}
The total allowance for your school time is US$7200

for loop statement

The for loop is another form of repetition statements in C++. The for loop provides for the beginning of the condition, the limit of the condition and updates after every execution. The for loop takes the following form; for ( starting point; limiting condition; update ) { Code to execute while the condition is true }
The stating point can include an initialized control variable either declared internally or using an already existing variable. Any expression however that evaluates to a value can also be used. The limiting condition tells the program that while the conditional expression is true the loop should continue to repeat itself. The update section modifies the control variable by way of increasing or decreasing it to the next level before a next repetition takes place. A semicolon separates the three elements of the for loop condition. It is possible to have an empty section for each of three of them. A semicolon must be provided even if the section is empty. An empty section is evaluated as true and the loop will repeat until something else stops it. Using our example of allowances while going to school, we can write the for loop as follows;
#include<iostream>
using namespace std;
int totalAllowance(int schoolTerms, int allowances)
{
int terms=schoolTerms;
int pay=allowances ;
int totalPay=0; // T is a specific term of the 72
for (int t=1; t<=terms; t++) //t++ increaments the term by one
{
totalPay=totalPay+pay; //add the allowance for each term to the total
}
return totalPay;
}
 int main()
{
 int terms=72;
int pay=100;
cout<<"The total allowance for your school time is US$" <<totalAllowance(terms,pay)<<endl;
return 0;
}
The total allowance for your school time is US$7200

Four important conditions must always be remembered when dealing with loops.
  1. The control variable to be used in the loop condition
  2. The initial value of the variable
  3. The continuation condition within which the loop should repeat execution
  4. The increment (or decrement) condition by which the control variable is modified every time a repetition is made
PREV:Introduction to Control Statements


NEXT:Introduction to Pointers

No comments:

Post a Comment