Intro to Programming Week # 5 Repetition Structure Lecture # 9 Department of Computer Science & Engineering Air University Intro to Programming Week # 5 Repetition Structure Lecture # 9 By: Saqib Rasheed
do-while
while loop while (condition) { statements; : } statements;
While loop executes zero or more times. What if we want the loop to execute at least one time?
Do while loop execute one or more times
Syntax of do-while loop { statements ; } while ( condition ) ;
The Do-While Statement Syntax do Action while (Expression) Semantics Execute Action If Expression is true then execute Action again Repeat this process until Expression evaluates to false Action is either a single statement or a group of statements within braces Action true Expression false
Example Develop a program in C++ that calculate the factorial of a given number. Use for loop to calculate the factorial, & do – while loop to perform the operation as many times as user want. Air University
#include<iostream> using namespace std; int main() { char ch; int value , factorial ; do cout<< "\n\n\t\tEnter an integer value: "; cin >> value; Air University
factorial = 1; for(int i = 2; i <= value; i++) factorial factorial = 1; for(int i = 2; i <= value; i++) factorial *= i; cout << "\nFactorial " << value << " is " << factorial; cout<< "\nDo you want to enter another value (y or n) "; cin >> ch; cout<<endl; } while((ch == 'y') || (ch == 'Y')); return 0; Air University
Example Create the equivalent of a four-function calculator. The program should Ask the user to enter a number, an Operator, and another number (10 + 20), using floating point. It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use switch statement to select the operation. Finally, display the Result. Air University
When it finishes the calculation, the program should ask wheather the user wants to do another calculation. The response can be 'y' or 'n'. Some sample interaction with the program might look like this: Enter Num1, Oper, Num2 : 100 / 10 Answer = 10 Do you wnat to Continue ('y' or 'n')? y Again Enter Num1, Oper, Num2 : 20 * 3 Answer = 60 Do you wnat to Continue ('y' or 'n')? n Air University
#include<iostream #include<iostream.h> void main () { int num1,num2,ans; char oper,ch; do cout<<" Enter Num1, Oper, Num2 : "; cin>>num1>>oper>>num2; Air University
switch(oper) { case '+': ans=num1+num2; break; case '-': ans=num1-num2; break; case '*': ans=num1*num2; break; case '/': ans=num1/num2; break; default: ans=0; } cout<<"Answer= "<<ans; cout<<"\n\nDo you wnat to Continue ('y' or 'n')"; cin>>ch; } while (ch!='n'); Air University
The break Statement The break statement causes an exit from a loop Just as it does from a switch statement. The next statement after the break is executed is the statement following the loop
The break Statement
Break Statement for(int i=0; i<10; i++) { cout<<"\nLine from loop = "<<i; if (i>5){ break; } cout<<“Next Line after loop”; Air University
Continue statement The break statement takes you out of the bottom of a loop. Sometimes, however, you want to go back to the top of the loop when something unexpected happens. Executing continue has this effect. (Strictly speaking, the continue takes you to the closing brace of the loop body, from which you may jump back to the top.)
Continue Statement int main() { long dividend, divisor; char ch; do cout << "Enter dividend: "; cin >> dividend; cout << "Enter divisor: "; cin >> divisor; if( divisor == 0 ) cout << "Illegal divisor\n"; continue; //go to top of loop }
cout << "Quotient is " << dividend / divisor; cout << ", remainder is " << dividend % divisor; cout << "\nDo another? (y/n): "; cin >> ch; } while( ch != 'n' ); return 0; }
goto Statement Label for goto int x = 0; thisline: cout<<"Line 1\n"; x++; cout<<"Line 2\n"; Sleep(20); cout<<"Line 3\n"; goto thisline; Air University