C++ Iteration statements
There are certain activities we humans perform every day, for example brushing our teeth, we brush our teeth every day unless we encounter one of the following condition :
- there is toothpaste
- there is no brush
- we are lazy
Now if we were to program a robot to do the exact same task every day until it encounters any of the above conditions, well actually we would have to eliminate laziness because a computer is never lazy!
So now let us talk about how we can program a computer to brush its teeth until the toothpaste is over.
To make a computer perform a sequence of actions over and over again we make use of iteration statements.
When a task is repeated again and again its called a loop. A loop is like a circle everything inside a loop keeps repeating unless an exit condition is met, in which case we exit the loop.
In programming, a loop is a block of code that is repeated again and again until a certain condition is met. C++ offers two types of looping statements.
- Entry controlled loop.
- Exit controlled loop.
Entry controlled loop
When we say entry controlled we mean loops in which the condition for which the loop is exited is checked for in the beginning of the loop. C++ offers 2 entry controlled looping statements.
FOR LOOP
Think about how we could program a computer to brush its teeth daily unless the toothpaste is over.
We start from day= 1 and tell the computer to brush its teeth until the toothpaste is over. Now let us assume the quantity of toothpaste available is 50mg and each time the computer brushes its teeth 2 mg is useisd up. We would basically tell the computer
Basic syntax of a for loop in C++
Below is the flow of control in a for loop −
- The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
- Next, the test condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the for a loop.
- After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement can be left blank, as long as a semicolon appears after the condition.
- The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of the loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
WHILE LOOP
This is another loop in which the test condition is tested before entering the loop.
*In general, you should use a |**for|*loop when you know how many times the loop should run. In case you want the loop to break based on a condition other than the number of times it runs, a |while| loop should be used.
In the above problem we don't know the number of times the computer will brush its teeth before the toothpaste is over, therefore it is appropriate to use |while| loop instead of |for| loop.
Syntax of while loop
Exit Controlled loop
Consider a situation in which we, first enter a loop and at the end of the loop we check the test condition, if it is true we proceed with the loop again and once again at the end of the loop we check the test condition and so on, whereas if it is false we exit the loop.
So the loop executes at least once even if the test condition is false.
We use a do while to solve these type of problems. For example when we build a calculator we want the calculator to perform multiple operation, before closing the application not just one therefore we make use of the do while loop in such conditions.
Syntax