Control Structure Senior Lecturer

Slides:



Advertisements
Similar presentations
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 5: Loops and Files.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
Chapter 12: How Long Can This Go On?
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
ANALYSIS AND ALGORITHM DESIGN - IV. Repeat statements/looping/counting/iterations It is often necessary to repeat certain parts of a program a number.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Computer Programming -1-
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
ALGORITHMS AND FLOWCHARTS
Lecture 4b Repeating With Loops
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
CHAPTER 5A Loop Structure
Chapter 5: Control Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Chapter 5: Repetition Structures
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Ch 7: JavaScript Control Statements I.
( Iteration / Repetition / Looping )
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Siti Nurbaya Ismail Senior Lecturer
Logical Operators and While Loops
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Control Structure Senior Lecturer
Control Structure Senior Lecturer
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Algorithm Discovery and Design
Chapter 5: Control Structure
Let’s all Repeat Together
Logical Operators and While Loops
Topics Introduction to Repetition Structures
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Basic Concepts of Algorithm
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
REPETITION Why Repetition?
Presentation transcript:

Control Structure Senior Lecturer Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences Universiti Teknologi MARA Kedah (e): sitinurbaya@kedah.uitm.edu.my (b): https://sitinur151.wordpress.com

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.

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

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 ?

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.

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

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

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

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 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) Figure 1: Pretest loop True False Action(s) Figure 2: Posttest loop True False

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)

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.

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

Iteration Control Structure While Looping Structure

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

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

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

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

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) 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

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

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 : 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.

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

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.

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