Control Structure Siti Nurbaya Ismail Senior Lecturer Faculty
Control Structure Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences Universiti Teknologi MARA Kedah (e): sitinurbaya@kedah. uitm. edu. my (b): https: //sitinur 151. wordpress. com 1
Control Structure Previously… • Sequential • 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. If : One Way Selection If … Else : Two Ways Selection If … Else If…. . . Else : Statement Multiple Ways Selection
Control Structure • A simple problem may be solved using a simple statements. • The arrangement of statements in this case would be implemented using the sequential control structure in an algorithm. • Problem that involve decision making and having to choose an action, the selection control structure will be implemented. • There also problems that needs to do the same process several times in order to be solved, repetition or loop or iteration control structure can be used in this situations.
Control Structure Today… • Iteration / Looping Write I LOVE YOU 100 times
Iteration Control Structure 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 no 1, no 2, no 3, no 4, no 5 sum = sum + no 1 sum = sum + no 2 sum = sum + no 3 sum = sum + no 4 sum = sum + no 5 average = sum / 5 display “The total is“, average End What will happen if the algorithm need the user to repeat inserting more then 5 numbers? 10 numbers ? 100 numbers? 1000 numbers?
Iteration Control Structure • Iteration is the process of looping or repeating sections of a program. • There are two types of iteration: • count-controlled loops repeat the same steps a specific number of times, regardless of the outcome. • condition-controlled loop will keep repeating the steps over and over…and over, until it gets a specific result.
Iteration Control Structure Start initialize sum with 0 display “Enter 5 number : “ read no 1, no 2, no 3, no 4, no 5 sum = sum + no 1 sum = sum + no 2 sum = sum + no 3 sum = sum + no 4 sum = sum + no 5 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 + num end. Repeat average = sum / 5 display “The total is“, average End
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 + num end. Repeat average = sum / 1000 display “The total is“, average End
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; i. initialize counter with 1 : initialization ii. counter < N : evaluation / condition / end value iii. increase counter by 1 : update statement / increment * N represent the number of repetition • Sentinel Loop • Where the number of repetition is unknown • when implementing counter loop, the following 3 statements must be available; i. iii. initialize the loop : initialization condition of the loop : evaluation / condition / end value statement to make the condition false
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.
Iteration Control Structure • There are 3 statement for repetition (loop control structure) : Loop statements while for do…while pretest loop posttest loop
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) True False Figure 1: Pretest loop Figure 2: Posttest loop
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: 1. A variable, the loop control variable is initialized (before entering the loop) 2. The loop control variable is tested, and if the result is true, the loop body is entered. 3. 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)
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. Initialization Action(s) Updating Figure 4: Posttest loop 15
Iteration Control Structure While Looping Structure
Iteration Control Structure Pseducode for while … end. While 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 end. WHILE End condition statement(s) to repeat statement to make the loop stops EXIT FROM THE LOOP
Basic Algorithm Using Repetition (Loop Control Structure) a) b) c) d) 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
Basic Algorithm Using Repetition (Loop Control Structure) a) 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 = 0 while (count < 20) read number total = total+number count++ end. WHILE display total End
Basic Algorithm Using Repetition (Loop Control Structure) b) Calculate the average for a group of numbers. • Example: Calculate the average for a set of 20 numbers. Start sum = 0 i = 1 while (i < 20) read number sum = sum+number i = i + 1 end. WHILE average= total/I display average End
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.
Basic Algorithm Using Repetition (Loop Control Structure) c) Find max and min value Example: General algorithm to find the biggest value from 20 data. Start read first. DATA max=first. DATA count = 2 while (count < 20) read data if (data > max) max = data End. IF count = count + 1 end. WHILE display max End Example: General algorithm to find the lowest value from 20 data. Start read first. DATA min=first. DATA *count start with 2 count = 2 because first data had while (count < 20) been entered before read data the loop if (data < min) min = data End. IF count = count + 1 end. WHILE display min End
Start max. Area=0 max. Peri=0 count = 1 while (count = 10)x Display “enter radius” read radius area=3. 14*radius peri=2*3. 142*radius if (area > max. Area) max. Area = area End. IF if (peri > max. Peri) max. Peri = peri End. IF count = count + 1 end. WHILE display “Maximum Area is “ max. Area display “Maximum Perimeter is “ max. Peri End
Basic Algorithm Using Repetition (Loop Control Structure) d) 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 count. ODD = 0 count = 1 while (count < 20) read number If (number%2==1) count. ODD = count. ODD + 1 End. IF count = count + 1 end. WHILE display count. ODD End
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
Condition • Examples of conditions : i. iii. iv. v. vi. num 1 < num 2 age is greater or equals to 40 grade equals to ‘A’ grade == ‘A’ grade is ‘A’ status is “excellent” 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. • There are few special words can be used in a condition like even, odd, negative, positive or divisible. • Examples: vii. if ( number is odd ) viii. if ( year is divisible by 4 ) ix. If ( number is positive )
EXERCISES Single Looping Nested Looping Pseudo code & Flowchart
Iteration Control Structure Determine Output
SEPTEMBER 2015 (Question 2 (b))
MARCH 2015 (Question 3 (b))
SEPTEMBER 2014 (Question 2 (b))
SEPTEMBER 2014 (Question 3 (a))
MARCH 2017 (QUESTION 4)
MARCH 2017 (QUESTION 2)
MARCH 2016 (Question 2(b))
- Slides: 35