ANALYSIS AND ALGORITHM DESIGN - IV. Repeat statements/looping/counting/iterations It is often necessary to repeat certain parts of a program a number.

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

Repetition Control Structures
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Chapter 04 (Part III) Control Statements: Part I.
UNIT 2. Introduction to Computer Programming
UNIT 3 PROBLEM SOLVING WITH LOOP AND CASE LOGIC STRUCTURE
Subject: Information Technology Grade: 10
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
Computer Science 1620 Loops.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
An Introduction to Programming with C++ Fifth Edition
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
Computer Science 1620 Programming & Problem Solving.
Repetition Control Structures
Adapted from slides by Marie desJardins
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
ITEC113 Algorithms and Programming Techniques
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Basic Control Structures
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Chapter 7 Problem Solving with Loops
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
ANALYSIS AND ALGORITHM DESIGN - III
REPETITION CONTROL STRUCTURE
while Repetition Structure
Lecture 7: Repeating a Known Number of Times
Programming Logic and Design Fourth Edition, Comprehensive
CHAPTER 5A Loop Structure
Chapter 5: Control Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Ch 7: JavaScript Control Statements I.
Control Structure Senior Lecturer
Control Structure Senior Lecturer
Algorithms & Pseudocode
Chapter (3) - Looping Questions.
Iteration: Beyond the Basic PERFORM
Topics Introduction to Repetition Structures
Let’s all Repeat Together
CHAPTER 4 Iterative Structure.
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

ANALYSIS AND ALGORITHM DESIGN - IV

Repeat statements/looping/counting/iterations It is often necessary to repeat certain parts of a program a number of times. One way of doing this would be to write that part of the program as many times as needed. This is impractical, however, as it would make the program lengthy. In addition, in some cases the number of times the section of code needs to be repeated is not known beforehand.

Repeat statements/looping/counting/iterations Two of the methods used to repeat sections of instructions/codes are the FOR construct and the WHILE construct. You need to keep track of how many times the instructions are repeated. Counting or iteration involves increasing the value of a counter variable by a fixed number (this can be any value such as 1, 2, 5, etc.) every time the instructions are repeated. This counter can be part of a condition for stopping the instructions from repeating when the value of the counter becomes equal to a certain value.

Counting Syntax: = + 1 The counter variable must be initialised before any instructions are carried out. It is usually set to 0; this is often, but not always, the case. The choice of the name for the counter variable can reflect what is being counted: in the following example, NumofCars for the number of cars.

Examples Example 1: A car rental firm leases its cars; read a car registration number and count the car. Print the number of cars. Solution 1: NumofCars = 0 PRINT “Enter registration number” READ regno NumofCars = NumofCars + 1 PRINT NumofCars

Examples Example 2: A car rental firm leases its cars; read the car registration number and count the car. If the car is a station wagon, also count the number of station wagons. Print the number of cars and the number of station wagons.

Cumulative Totals It often becomes necessary to keep adding values to a current total to get a new total. It is important to initialise the Cumulative Total variable to 0 before the values are added. Syntax: = +

Examples Solution 2: NumofCars = 0 NumStationWagons = 0 PRINT “Enter a car registration number and car type” READ regno, cartype IF cartype = “Station Wagon” THEN NumStationWagons = NumStationWagons + 1 ENDIF NumofCars = NumofCars + 1 PRINT “Number of Cars: ”, NumofCars PRINT “Number of Station Wagons: ”, NumStationWagons

Cumulative Totals Example 3: A car rental firm leases its cars; read the number of cars leased in one day and add it to the total number of cars leased. Print the total number of car leased. Solution 3: TotalCars = 0 PRINT “ Enter the number of cars leased in one day” READ carsleased TotalCars = TotalCars + carsleased PRINT “Total Number of Cars Leased: ”, TotalCars

Loops When a group of statements/instructions are repeated it is called a loop. There are two types of loops:  Finite loop – the number of times a repetition is to be made is known.  Indefinite loop - the number of times repetition is to be made is not known.

The FOR construct – Finite loop In this construct the loop is controlled by a counter which increases each time a set of instructions is executed. This construct is used when the number of times a set of instructions has to be repeated is known. Syntax 1: FOR = TO DO ENDFOR

The FOR construct – Finite loop Syntax 2: FOR = TO STEP DO ENDFOR The STEP clause allows you to increase the variable by the increment value every time the instructions are repeated, until it becomes equal or exceeds the end value. It is often used when we want to print tables.

Examples Example 4: A car rental firm leases 4 cars in one day. Read the number days for lease for each car and calculate the total rent paid to the firm if a car is leased for $ Print the total rent paid to the rental firm.

Examples Solution 4: TotalRent = 0 FOR Cars = 1 TO 4 DO PRINT “ Enter number of days for lease” READ noofdays rent = nofodays * 250 TotalRent = rent + TotalRent ENDFOR PRINT “Total rent paid to firm: ” TotalRent

Examples Example 5: Print a table to find the square and cube of numbers 1 t0 10. Solution 5: PRINT “Number”, “Square”, “Cube” FOR Number = 1 TO 10 DO Square = Number * Number Cube = Number * Number * Number Print Number, Square, Cube ENDFOR

Examples Example 6: Print a table to find the square and cube of all even numbers between 2 and 20 inclusive. Solution 6: PRINT “Number”, “Square”, “Cube” FOR Number = 2 TO 20 STEP 2 DO Square = Number * Number Cube = Number * Number * Number Print Number, Square, Cube ENDFOR

Examples Example 7: Calculate the sum of all the odd numbers between 1 and 20. Print the total. Solution 7: Sum = 0 FOR Oddnumber = 1 TO 20 STEP 2 DO Sum = Sum + Oddnumber ENDFOR PRINT Sum

Examples Example 8: A company gives out bonuses based on the amount of income generated by their sales representatives per month. Once the income is greater than or equal to $10,000.00, a bonus of 20% is given. If the income generated is greater than or equal to $ but less than $10,000.00, a bonus of 15% is given. If the income generated is greater than or equal to $ but less than $8,000.00, a bonus of 10% is given. If the income generated is less than $ , a bonus of 3% is given.

Examples Example 8 (cont’d): Write an algorithm to read 10 employees’ numbers, their income generated, and determine the bonuses of 10 employees. Print the employee number, income generated and the bonus earned by each employee.

Examples Solution 8: PRINT “Employee Number”, “Income generated”, “Bonus” FOR Numemployees = 1 TO 10 DO PRINT “ Enter employee number, income generated” READ emp _num, inc_gen IF (inc_gen >= $10,000.00) THEN bonus = inc_gen * 20% ELSE IF (inc_gen >= $8,000.00) AND (inc_gen < $10,000.00) THEN bonus = inc_gen * 15%

Solution 8 (cont’d): ELSE IF (inc_gen >= $5,000.00) AND (inc_gen < $8,000.00) THEN bonus = inc_gen * 10% ELSE bonus = inc_gen * 3% ENDIF PRINT emp_num, inc_gen, bonus ENDFOR

Examples Example 9: Write an algorithm to read 25 numbers and print the lowest. Solution 9: Lowest = FOR count = 1 TO 25 DO PRINT “Enter a number” READ number IF number < lowest THEN Lowest = number ENDIF ENDFOR PRINT “The lowest number entered is: ”, Lowest

The WHILE construct - Indefinite loop In the WHILE construct the computer executes a set of statements/instructions repeatedly for as long as a given condition is true. When you do not know beforehand how many times a set of instructions has to be repeated, use the WHILE construct.

The WHILE construct - Indefinite loop In the WHILE construct the condition is tested; if it is true the instructions within the WHILE ENDWHILE are executed until the condition becomes false and the loop is executed. The trigger that causes the loop to stop is a value that is entered as input data. This value is called a sentinel value, dummy value or terminating value.

The WHILE construct - Indefinite loop Syntax: WHILE DO ENDWHILE

Examples Example 10: Write an algorithm to enter the age and count the number of students in a class. Calculate the average age of the group of students if the data is terminated by 999. Print the number of students in the class and the average age of the students.

Examples Solution 10: total = 0 count = 0 PRINT “Enter student’s age” READ age WHILE age 999 DO total = total + age count = count + 1 PRINT ““Enter student’s age” READ age ENDWHILE average = total/count PRINT count, average

Examples Example 11: Write an algorithm to read a set of numbers terminated by 0; print the number of positive and negative numbers entered.

Examples Solution 11: negative = 0 positive = 0 PRINT “Enter a number” READ number WHILE number 0 DO IF number > 0 THEN positive = positive + 1 ELSE negative = negative + 1 ENDIF PRINT “Enter a number” READ number

Solution 11 (cont’d): ENDWHILE PRINT negative, positive

Examples Example 12: Write an algorithm to read a set of numbers terminated by 0 and print the highest number.

Examples Solution 12: highest = 0 PRINT “Enter a number” READ number WHILE number 0 DO IF number > highest THEN highest = number ENDIF PRINT “Enter a number” READ number ENDWHILE PRINT highest