Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structure Senior Lecturer

Similar presentations


Presentation on theme: "Control Structure Senior Lecturer"— Presentation transcript:

1 Control Structure Senior Lecturer
Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences Universiti Teknologi MARA Kedah (e): (b):

2 Control Structure Previously… Sequential Selection
Creating a set of instructions to complete a task. Selection A decision within a computer program when the program decides to move on based on the results of an event.

3 Control Structure Today… Write I LOVE YOU 100 times
Iteration / Looping

4 Iteration Control Structure
What will happen if the algorithm need the user to repeat inserting more then 5 numbers? 10 numbers ? 100 numbers? 1000 numbers? Write an algorithm that will ask user to input 5 numbers, then calculate the average of it. Output the average value. Start initialize sum with 0 display “Enter 5 number :“ read no1,no2,no3,no4,no5 sum = sum + no1 sum = sum + no2 sum = sum + no3 sum = sum + no4 sum = sum + no5 average = sum / 5 display “The total is“, average End Solution ?

5 Iteration Control Structure
Iteration is the process of looping or repeating sections of a program. There are two types of iteration: count-controlled count-controlled loops repeat the same steps a specific number of times, regardless of the outcome. condition-controlled condition-controlled loop will keep repeating the steps over and over…and over…and over…and over, until it gets a specific result.

6 Iteration Control Structure
Start initialize sum with 0 display “Enter 5 number :“ read no1,no2,no3,no4,no5 sum = sum + no1 sum = sum + no2 sum = sum + no3 sum = sum + no4 sum = sum + no5 average = sum / 5 display “The total is“, average End Start initialize sum with 0 display “Enter 5 number :“ repeat the following actions 5 times read num sum  sum + num endRepeat average = sum / 5 display “The total is“, average End iteration / looping

7 Iteration Control Structure
Write an algorithm that will ask user to input 1000 numbers, then calculate the average of it. Output the average value. Start initialize sum with 0 display “Enter 1000 number :“ repeat the following actions 1000 times read num sum  sum + num endRepeat average = sum / 1000 display “The total is“, average End Solution

8 Iteration Control Structure
There are two types of repetition can be implemented. Counter Loop where the number of repetition is identified when implementing counter loop, the following 3 statements must be available; Initialize counter with 1 : initialization Counter < N : evaluation / condition / end value Increase counter by 1 : update statement / increment * N represent the number of repetition Sentinel Loop Where the number of repetition is unknown Initialize the loop : initialization Condition of the loop : evaluation / condition / end value Statement to make the condition false

9 Iteration Control Structure
The above i, ii & iii (initialization, condition & increment) statements will make sure that the loop starts and stops correctly. If we forget any one of the statement , the loop will not be successful. Example: If we miss the statement with false condition in the algorithm , we will get an infinite loop. an infinite loop is scenario where the loop does not stop.

10 Iteration Control Structure
There are 3 statement for repetition (loop control structure) : Loop statements while for do…while pretest loop pretest loop posttest loop

11 Iteration Control Structure
PRETEST LOOP In each iteration, the loop control expression is tested first. If it is true, the loop action(s) is executed; if it is false, the loop is terminated. POSTTEST LOOP In each iteration, the loop action(s) is executed. Next, the loop control expression is tested. If it is true, a new iteration is started; otherwise, the loop terminates. Action(s) Figure 1: Pretest loop True False Action(s) Figure 2: Posttest loop True False

12 Iteration Control Structure
When you write a loop, you must control the number of repetition it performs. If you don’t, you run the risk of creating an infinite loop. Commonly, you control a loop’s repetitions by using either a counter or a sentinel value. You can use a loop to execute a body of statements continuously as long as some condition continues to be true. To make a loop ends correctly, three separate actions should occur: A variable, the loop control variable is initialized (before entering the loop) The loop control variable is tested, and if the result is true, the loop body is entered. The body of the loop must take some action that alters the value of the loop control variable (so that the while expression eventually evaluates as false)

13 Iteration Control Structure
In addition to the loop control expression, there are two other processes associated with almost all loop: initialization and updating. LOOP INITIALIZATION Before a loop can start, some preparation is usually required. Such preparation is called loop initialization. Initialization must be done before the first execution of the body. It sets the stage for the loop actions. LOOP UPDATE How can the condition that controls the loop be true for a while and then change to false…? The answer is that something must happen inside the body of the loop to change the condition. Otherwise, we would have an infinite loop. This changes the resulting condition from true to false (are known as loop update). Updating is done in each iteration, usually as the last action. If the body of the loop is repeated m times, then the updating is also done m times.

14 Iteration Control Structure
In addition to the loop control expression, there are two other processes associated with almost all loop: initialization and updating. Updating Figure 4: Posttest loop Action(s) Initialization Action(s) Figure 3: Pretest loop Initialization Updating

15 Iteration Control Structure
While Looping Structure

16 Iteration Control Structure
Pseducode for while … endWhile statement for sentinel loop. initialize the loop Start initial value of a loop while (condition) statement(s) to be repeated statement to update the condition endWHILE End condition statement(s) to repeat statement to make the loop stops EXIT FROM THE LOOP

17 Basic Algorithm Using Repetition (Loop Control Structure)
Calculate the total or sum Calculate the average Find maximum value and minimum value Count initialize the loop condition statement(s) to repeat statement to make the loop stops EXIT FROM THE LOOP

18 Basic Algorithm Using Repetition (Loop Control Structure)
Calculate the total Calculate the total number or sum of number for a group of numbers. Example: Calculate the total for a set of 20 numbers. Start total = 0 count = 1 while (count < 20) read number total = total+number Count++ endWHILE display total End

19 Basic Algorithm Using Repetition (Loop Control Structure)
b) Calculate the average Calculate the average for a group of numbers. Example: Calculate the average for a set of 20 numbers. Start total = 0 count = 1 while (count < 20) read number total = total+number count = count + 1 endWHILE average= total/count display average End

20 Basic Algorithm Using Repetition (Loop Control Structure)
c) Find max and min value When the limit values are known, we may apply the general algorithm to find the highest or lowest value.

21 Basic Algorithm Using Repetition (Loop Control Structure)
Find max and min value Example: General algorithm to find the biggest value from 20 data. Example: General algorithm to find the lowest value from 20 data. Start read firstDATA max=firstDATA count = 2 while (count < 20) read data if (data > max) max = data EndIF count = count + 1 endWHILE display max End Start read firstDATA min=firstDATA count = 2 while (count < 20) read data if (data < min) min = data EndIF count = count + 1 endWHILE display min End *count start with 2 because first data had been entered before the loop

22 Basic Algorithm Using Repetition (Loop Control Structure)
Count When we want to count something, the initial value for variable to hold the count is 0. Then we search what we want to count. Every time that we find the value that we want, then we increase the value of the count by 1. Repeat the process until end of data. Example: Calculate how many odd numbers in a group of 20 numbers. Start countODD = 0 count = 1 while (count < 20) read number If (number%2==1) countODD = countODD + 1 EndIF count = count + 1 endWHILE display countODD End

23 Condition A condition is a comparison at least between two values or variables. To make a condition (comparison between two values), the following relational operator can be used. Relational operator Meaning Greater than Less than == Equals to, is ≤= Less or equals ≥= Greater or equals != Not equal

24 Condition Examples of conditions :
There are few special words can be used in a condition like even, odd, negative, positive or divisible. Examples: num1 < num2 age is greater or equals to 40 grade equals to ‘A’ grade == ‘A’ grade is ‘A’ status is “excellent” if ( number is odd ) if ( year is divisible by 4 ) If ( number is positive ) Note: When we use single character data as in above (iii) (iv) and (v), use single quotes (‘ ‘) and use double quotes( “ “ ) for a string (vi). Condition used in above (iii), (iv) and (iv) are the same.

25 EXERCISES Write psedudocodes and flowcharts for each of the following problems:

26 EXERCISE 6C (pg no 340) Question 1
Calculate the total of even numbers that is divisible by 5. How many numbers involved is determined by the user. Question 3 Calculate the total area and total perimeter of N rectangles Question 4 Display the biggest area and perimeter among 10 circles. Question 5 Ask the user to enter 20 numbers. Display all numbers which are odd and divisible by 3.

27 EXERCISE 6C (pg no 340) Question 6
Calculate the total area of 10 squares and total perimeter of 5 rectangles. Question 7 Ask the user to enter N numbers. Find : a. how many odd numbers b. how many negative numbers total of number which is divisible by 10 calculate the total area and total perimeter of N rectangles


Download ppt "Control Structure Senior Lecturer"

Similar presentations


Ads by Google