Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.

Similar presentations


Presentation on theme: "Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012."— Presentation transcript:

1 Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012

2 Last Time… Branching statements –specify alternate paths of execution, depending on the outcome of a logical condition –Do one thing if some condition is met or another if not (Decisions)

3 If …else if … else if (condition1) statement1 else if (condition2) statement2... else if (conditionN-1) statementN-1 else statementN

4 #include using namespace std; int main() { int x, y; cout << "Enter the first number: "; cin >> x; cout << "Enter the second number: "; cin >> y; if (x > y) cout<<x<<" is greater than " <<y<<endl; else if (x == y) cout<<x<<" is equal to "<<y<<endl; else cout<<x<<" is less than "<<y<<endl; return 0; }

5 Syntax: switch (expression) { case constant1: statements break; case constant2: statements break; … case constantN: statements break; default: statements } An integer expression, whose value we want to test Unique, possible values that we are testing for equality with expression Statements to be executed when expression becomes equal to constantN Statements to be executed when expression is not equal to any of the N constants The break statement causes the Program to exit the switch statement

6 Example char choice; cout << "Enter A, B, or C: "; cin >> choice; switch (choice) { case 'A': cout << "You entered A.\n"; break; case 'B': cout << "You entered B.\n"; break; case 'C': cout << "You entered C.\n"; break; default: cout << "You did not enter A, B, or C!\n"; }

7 int num; cout << "Enter a number: "; cin >> num; if (num >= 1 && num <= 5) { switch (num) { case 1: cout << "Recharging..."; break; case 2: cout << "Remaining Balance..."; break; case 3: cout << "Block number..."; break; default: cout << "Change Language..."; } else cout << "Invalid command"; return 0;

8 Looping A loop is a control structure that causes a statement or group of statements to repeat. C++ has three looping control structures: the while loop, the do-while loop, and the for loop. The difference between each of these is how they control the repetition.

9 Loop statements –specify computations, which need to be repeated until a certain logical condition is satisfied. –Do a certain action repeatedly based on a condition (Looping)

10 The while Loop Statement Repeatedly executes a statement if a certain condition is true. It is a pretest loop

11 While Loop Statement Each repetition is known as an iteration. The while loop is known as a pretest loop, which means it tests its expression before each iteration. An important characteristic of the while loop is that the loop will never iterate if the test expression is false to start with.

12 While Semantics

13 Example_01

14 Example_02 count = 0;// Initialization while (count < 3)// Loop Condition { cout << "Hi ";// Loop Body count++;// Update expression } –Loop body executes how many times?

15 Example_03 int i = 0; while(i<10) { cout<<i<<” “; i++; } cout<<”done!”; This outputs: 0 1 2 3 4 5 6 7 8 9 done!

16 Example_04 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;

17 The do… while Loop Statement There are cases where we may want to execute the body of the loop at least once before testing the condition. In other words, there are times where we may need a posttest loop. This is where the do … while loop comes in.

18 do-while Loop Syntax

19 do-while Loop Example count = 0; // Initialization do { cout << "Hi "; // Loop Body count++; // Update expression } while (count < 3); // Loop Condition – Loop body executes how many times? – do-while loops always execute body at least once!

20 Example WhileDo While int x = 1; while (x < 0) cout << x << endl; int x = 1; do cout << x << endl; while (x < 0); Never executesExecutes once because the expression x<0 is not evaluated until the end of the loop

21 #include using namespace std; int main() { char reply; do { cout<<“Perform Any Operation Here”<<endl; cout << "Do you want to do it again? (y/n)"; cin >> reply; } while (reply == 'y' || reply == 'Y'); return 0; }

22 The for Loop Statement The for loop is ideal for working with counters because it provides built-in expressions that initialize and update variables. It’s a pretest loop.

23

24 #include using namespace std; // Counting Up void main() { int i; for (i=0; i<=10; i++) { cout<<i <<endl; }

25 The for Loop Statement

26

27 Break ing out of a loop Sometimes it’s necessary to stop a loop before it goes through all its iterations. The break statement causes a loop to terminate early.

28 The break Statements The break statement can also be placed inside a loop. When it is encountered, the loop stops and program jumps to the statement following the loop.

29 Example int count = 0; while (count++ < 10) { cout << count << endl; if (count == 5) break; } The while loop in the following program segment appears to execute 10 times, but the break statement causes it to stop after the fifth iteration.

30 #include using namespace std; int main() { int i = 0; while (true) { if (i % 5 == 0) cout << i << " "; i++; if (i > 25) break; } system("pause"); return 0; }

31 #include using namespace std; int main() { int max; cout << "Enter max: "; cin >> max; int sum = 0; for (int i = 0; i <= max ; i+=2) { sum += i; if (i > 10) break; } cout << "The sum is: " << sum; return 0; }

32 The continue Statements The continue statement, placed inside a loop, causes the loop to stop its current iteration and begin the next one. All statements in the body of the loop that come after it are ignored.

33 The continue statement The continue statement causes the current iteration of a loop to end immediately. 1)In a while loop, this means the program jumps to the test expression at the top of the loop. 2) In a do-while loop, the program jumps to the test expression at the bottom of the loop, which determines if the next iteration will begin. 3) In a for loop, continue causes the update expression to be executed, and then the test expression to be evaluated.


Download ppt "Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012."

Similar presentations


Ads by Google