Using the Repetition Structure EENG 225 High Level

  • Slides: 27
Download presentation
Using the Repetition Structure EENG 225 High Level Programming-Alex Green

Using the Repetition Structure EENG 225 High Level Programming-Alex Green

Introduction The repetition structure, also called a loop, is one of the three basic

Introduction The repetition structure, also called a loop, is one of the three basic control structures used in programming. The other two control structures are sequence and the selection structure, which have been covered previously.

Components of a Loop Condition is true Loop Condition Loop Body Check the loop

Components of a Loop Condition is true Loop Condition Loop Body Check the loop condition

Loop Condition and Loop Body • The loop condition determines the number of times

Loop Condition and Loop Body • The loop condition determines the number of times the instructions within the loop, referred to as the loop body, will be processed. • A loop condition must result in either a true or false value

Types of Loops Loop Pretest loop Counter Sentinel Post-test loop Counter Sentinel

Types of Loops Loop Pretest loop Counter Sentinel Post-test loop Counter Sentinel

Pretest Loop The loop condition appears at the beginning of the loop. Loop Condition

Pretest Loop The loop condition appears at the beginning of the loop. Loop Condition condition false true Loop Body

Pretest Loop Cont’d Remember that the diamond symbol represents a decision and is called

Pretest Loop Cont’d Remember that the diamond symbol represents a decision and is called a decision box. CONDITION TRUE FALSE The two flowlines leaving the symbol represent the true and false paths and should be labelled accordingly.

Important note about pretest loops Because the loop condition in a pretest loop is

Important note about pretest loops Because the loop condition in a pretest loop is evaluated before any of the instructions within the loop are processed, it is possible that the loop body instructions may not be processed. This would be true if the loop condition is initially evaluated to false.

Coding the Pre-test Loop While loop The while loop repeats while a condition is

Coding the Pre-test Loop While loop The while loop repeats while a condition is true. The condition is tested at the top of the loop. Format: while (condition) statement; BODY

Example of a Pretest Loop i<=3

Example of a Pretest Loop i<=3

Coding the Pre-test Loop cont’d Please Note: If you want to have more than

Coding the Pre-test Loop cont’d Please Note: If you want to have more than 1 statement inside the body of a loop then you must put them between a pair of braces. Format: while(condition) { statement 1; statement 2; … statementn; }

Example 1. #include <iostream> 2. using namespace std; 3. int main() 4. { 5.

Example 1. #include <iostream> 2. using namespace std; 3. int main() 4. { 5. int i = 0; 6. while( i <= 10) 7. { 8. i++; 9. cout << i << endl; 10. } 11. system(“pause”); 12. return 0; 13. }

Post-test Loop the condition is tested AFTER the body of the loop executes. The

Post-test Loop the condition is tested AFTER the body of the loop executes. The statements in the body execute at least once Loop Body

An Infinite Loop a is always less than 6 therefore the loop condition is

An Infinite Loop a is always less than 6 therefore the loop condition is always true resulting in an infinite loop. a = 5; while (a < 6); More on Infinite Loops

Example of a Post-test Loop START COUNT =1 Display ‘Hello’ False COUNT = COUNT

Example of a Post-test Loop START COUNT =1 Display ‘Hello’ False COUNT = COUNT +1 true COUNT>10 STOP

Coding the Post-test Loop do-while Loop Format: do { statement 1; staement 2; …

Coding the Post-test Loop do-while Loop Format: do { statement 1; staement 2; … } while (condition);

Example #include <iostream> using namespace std; START int main() { int count = 1;

Example #include <iostream> using namespace std; START int main() { int count = 1; do { cout << “Hellon”; count = count + 1; } while (count <= 10); COUNT =1 Display ‘Hello’ False COUNT = COUNT +1 true COUNT> 10 STOP } system(“pause”); return 0;

Summary Pretest Loop Post-test Loop Condition comes before the Loop Body Loop Condition comes

Summary Pretest Loop Post-test Loop Condition comes before the Loop Body Loop Condition comes after the Loop body Depending on the value of the condition initially, the body of the loop may not be executed at all The body of the loop is executed at least once regardless of the value of the condition initially

Counter-Controlled Loops These are loops that are executed a fixed numbers of times i.

Counter-Controlled Loops These are loops that are executed a fixed numbers of times i. e. the number of times the body of the loop is to be repeated is known before the loop is executed. e. g. Write ‘Hello’ on the screen 4 times A counter is used to control the loop.

Sentinel Controlled Loops A sentinel controlled loop is a loop where the number of

Sentinel Controlled Loops A sentinel controlled loop is a loop where the number of times that the body is to be repeated is not known before hand. The loop is terminated only when some value which is not considered to be valid input is entered This value is called a sentinel.

More on Sentinel Controlled Loops With a pretest loop an input instruction(s) appears just

More on Sentinel Controlled Loops With a pretest loop an input instruction(s) appears just previous to the loop condition and is referred to as the priming read It is used to prime (prepare) the loop. The priming read gets only the first value(s) from the user. Another input statement is need in the body of the loop to get the remaining values from the user.

Counters and Accumulators Counters and accumulators are used within a repetition structure to calculate

Counters and Accumulators Counters and accumulators are used within a repetition structure to calculate subtotals, and averages. All counters and accumulators must be initialized and updated. Counters are updated by a constant value, whereas accumulators are updated by an amount that varies.

A Complete Example E. g. A company wants a program that the sales manager

A Complete Example E. g. A company wants a program that the sales manager can use to display the average amount the company sold during the prior year. The sales manager will enter the amount of each sales person’s sales. The program will use both a counter and an accumulator to calculate the average sales amount which will then be displayed on the screen.

Discussion Ouput : Average Sales Input: Sales Figures How Many? We don’t know so

Discussion Ouput : Average Sales Input: Sales Figures How Many? We don’t know so have to count them. Need a counter Need a sentinel to stop the loop Since sales cannot be –ve, use a –ve sales value as sentinel Need to total the sales so we need an accumulator View the solution

Conclusion Please Complete the Review Quiz Please View the Handout Please Complete the following

Conclusion Please Complete the Review Quiz Please View the Handout Please Complete the following exercises For more information please visit http: //www. greenai. com End of Presentation

Key Term 1: Counter �Counter : a numeric variable used for counting something �Initializing

Key Term 1: Counter �Counter : a numeric variable used for counting something �Initializing means to assign a beginning value to the counter (usually 0) �The initialization is usually performed outside the loop body, because it needs to be done only once. �Updating, also called incrementing, means adding a constant number to the value stored in the counter (the number can be positive or negative). Back to Presentation

Key Term 2: Infinite Loop An infinite loop (or endless loop) is a loop

Key Term 2: Infinite Loop An infinite loop (or endless loop) is a loop that processes its instructions indefinitely. When dealing with the repetition structure, one reason an infinite loop occurs is that the loop condition continues to be true. Remember that as long as the loop condition is true, the loop body is continually processed. Back to the presentation