Lesson 9 Repetition Structures Loops 2 Review Structures

  • Slides: 13
Download presentation
Lesson - 9 Repetition Structures (Loops) - 2

Lesson - 9 Repetition Structures (Loops) - 2

Review �Structures which changes the program flow are: �Decision Structures �Repetition Structures are: �While

Review �Structures which changes the program flow are: �Decision Structures �Repetition Structures are: �While � Counter Controlled Repetition Structures � Sentinel Controlled Repetition Structures �Do / While �For

The "do/while" Loop �The statement "do/while" is similar to the statement "while" with an

The "do/while" Loop �The statement "do/while" is similar to the statement "while" with an important difference: �The "do/while" statement performs a test after each execution of the loop body. �Therefore, the loop body is executed at least once.

The "do/while" Loop �The statement "do/while" is usually preferred to design menu-driven programs. �A

The "do/while" Loop �The statement "do/while" is usually preferred to design menu-driven programs. �A menu-driven program has an interface that offers the user a simple menu from which to choose an option. �There usually exists one option to exit in the menu driven programs. Make your choice: 1. New record 2. Modify 3. Delete 4. Print 5. Exit

Do / While Syntax do { Statements } while (Condition); �Example: do { cout

Do / While Syntax do { Statements } while (Condition); �Example: do { cout <<counter<<" "; counter++; } while(counter<=10)

Example Program �Make a program which prints the numbers from 1 to 10. #include

Example Program �Make a program which prints the numbers from 1 to 10. #include <iostream> using namespace std; int main() { int counter = 1; do { cout <<counter<<" "; counter++; } while (counter <= 10); system("PAUSE"); return 0; }

The "for" Loop �Like "while" and "do/while" the “for” loop enables you to evaluate

The "for" Loop �Like "while" and "do/while" the “for” loop enables you to evaluate a sequence of expressions multiple numbers of times. �“for” loops are best when you know the number of times that the expressions are needed to be evaluated in advance (countercontrolled repetition).

Syntax of “for” statement for( initializations; condition; increment or decrement) { Statements; } Example:

Syntax of “for” statement for( initializations; condition; increment or decrement) { Statements; } Example: For(counter=1; counter<=10; counter++) { cout<<counter<<“ ”; } � for: � keyword � Initializations: � You may initialize the counter and other variables here. This part is executed only once at the beginning. � Condition: � Set the continuation-condition here to continue or end the loop. The condition is tested each time before the statements are executed. � Increment or decrement: � You may increment or decrement the counter here. This part is executed each time after the statements have been executed. � Statements: � The statements are executed each time in the loop.

Example Program �Make a program which prints the numbers from 1 to 10. �Solution:

Example Program �Make a program which prints the numbers from 1 to 10. �Solution: #include <iostream> using namespace std; int main() { int counter; for (counter=1; counter<=10; counter++) { cout<<counter<<" "; } system("PAUSE"); return 0; }

Which loop should I use? Loop Type while do/while for Description This is a

Which loop should I use? Loop Type while do/while for Description This is a good, solid looping process with applications to numerous situations. This looping process is a good choice when you are asking a question, whose answer will determine if the loop is repeated. This loop is a good choice when the number of repetitions is known, or can be supplied by the user.

Example - 1 � Question: Make a program which reads a number from keyboard

Example - 1 � Question: Make a program which reads a number from keyboard and displays on screen it’s factor. (n!) � Solution: #include<iostream> using namespace std; int main() { int i, n; long result=1; cout<<"N=? "; cin>>n; for(i=2; i<=n; i++) result *= i; cout<<"N! = "<<result<<endl; system("pause"); return 0; }

Example - 2 � Question: Make a program which reads X and Y and

Example - 2 � Question: Make a program which reads X and Y and displays on screen X raised to the power of Y (XY) � Solution: #include <iostream> using namespace std; int main() { int base, power; long result = 1; power = 0; cout <<"Enter the base and the power? "; cin >> base >> power; for (int i=1; i<=power; i++) result *= base; cout <<"Result is "<<result<<endl; system("pause"); return 0; }

Home Work You are given the list of N integers. Make a program to

Home Work You are given the list of N integers. Make a program to find the item in the list.