Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Programming Week # 5 Repetition Structure Lecture # 9

Similar presentations


Presentation on theme: "Intro to Programming Week # 5 Repetition Structure Lecture # 9"— Presentation transcript:

1 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

2 do-while

3 while loop while (condition) { statements; : } statements;

4 While loop executes zero
or more times. What if we want the loop to execute at least one time?

5 Do while loop execute one
or more times

6 Syntax of do-while loop
{ statements ; } while ( condition ) ;

7 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

8 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

9 #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

10 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

11 Example Create the equivalent of a four-function calculator. The program should Ask the user to enter a number, an Operator, and another number ( ), 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

12 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

13 #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

14 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

15 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

16 The break Statement

17 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

18 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.)

19 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 }

20 cout << "Quotient is " << dividend / divisor;
cout << ", remainder is " << dividend % divisor; cout << "\nDo another? (y/n): "; cin >> ch; } while( ch != 'n' ); return 0; }

21 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


Download ppt "Intro to Programming Week # 5 Repetition Structure Lecture # 9"

Similar presentations


Ads by Google