Table of Contents
Back Button
Back to Chapter
C++ Setting Up
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.

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

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/490bfe76-bb21-410d-ad4e-b7a889f48701/Untitled_Diagram_(13).png

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

for (day= 1; until toothpaste quantity not equal to zero; day increments by 1)br { "display you have brushed your teeth" and subtract 2 mg from the toothpaste quantity; }

Basic syntax of a for loop in C++

for ( initialization; test condition; increment ) { ....... //lines of code to be executed }

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.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e0747242-aa96-4948-8a76-c034c138ce5e/Untitled_Diagram_(14).png

Syntax of while loop

while(condition) { //lines of code; };
#include using namespace std; main() { int days; int toothpaste=50; while(toothpaste!=0) { cout<<"I have brushed my teeth"; toothpaste=toothpaste-2; }; }

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.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/cad4a68b-de22-4886-84e4-1e794f43fc18/Untitled_Diagram_(15).png

Syntax

do { ......//lines of code }while(test condition);
# include using namespace std; int main() { char op;int ch=0; float num1, num2; do{ cout<<"do you wish to calculate:"<>ch; if(ch==1) { cout << "Enter operand 1: "; cin >> num1; cout<<"enter operand 2:"; cin>>num2 cout << "Enter operator either + or - or * or /: "; cin >> op; switch(op) { case '+': cout << num1+num2; break; case '-': cout << num1-num2; break; case '*': cout << num1*num2; break; case '/': if(num1>num2) cout << num1/num2; else cout<
Output: do you wish to calculate: 1.yes 2.no 1 Enter operand 1: 10 enter operand 2:5 Enter operator either + or - or * or /: * 50 do you wish to calculate: 1.yes 2.no
Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?