CP1020 - Week 7 Looping constructs Aims and Objectives zUnderstand what the while group of loops are and why they are needed zBe able to design and code.

Slides:



Advertisements
Similar presentations
Introduction to Flowcharting
Advertisements

UNIT 3 PROBLEM SOLVING WITH LOOP AND CASE LOGIC STRUCTURE
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
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.
CP Week 10 Modularising programs using Procedures.
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
CP Week 6 Repetition. Aims and Objectives zUnderstand what loops are and why they are needed zUnderstand the FOR loop used in programming zUnderstand.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson1 Week 1 - Principles of programming Welcome from the Presenters Steve Garner and Dr Ian.
CP Principles of Programming Steve Garner and Ian Coulson CP Week 8 Arrays.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
(C)opyright 2003 Scott/Jones Publishers Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Scott/Jones Publishers.
5.05 Apply Looping Structures
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.
PROGRAMMING In Lesson 5. RECAP  Complete the starter activity.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Looping While-continue.
MATLAB® While Loops.
Chapter 12: How Long Can This Go On?
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
CHAPTER SIX LOOPS © Prepared By: Razif Razali 1. FORMAT OR REFRESH!! WHAT HAVE WE LEARN? Differentiate between the types of selection structure? Which.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
Controlling Program Flow with Looping Structures
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Steps for Developing a Visual C# 2010 Application MIT By: S. Sabraz Nawaz.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
Selection Using IF THEN ELSE CASE Introducing Loops.
CSC111 Quick Revision.
while Repetition Structure
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
Introduction To Flowcharting
Introducing Do While & Do Until Loops & Repetition Statements
Starter Write a program that asks the user if it is raining today.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
CIS 16 Application Development Programming with Visual Basic
Lec 4: while loop and do-while loop
Introduction to Problem Solving and Control Statements
Case & Repetitive Statements
A LESSON IN LOOPING What is a loop?
Prepared By: Deborah Becker
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.
Week 3 – Repetition (ctd.)
Presentation transcript:

CP Week 7 Looping constructs

Aims and Objectives zUnderstand what the while group of loops are and why they are needed zBe able to design and code while loops in QBasic

Do While.. Loop Construct zThe DO WHILE…LOOP construct can be used when you don’t know how may times the loop should be executed before entering the loop zThe WHILE statement checks to see if the condition is True, if it is then the loop continues to execute zThe statements between the DO WHILE and LOOP are executed repeatedly until the condition is FALSE

While loop zGeneral structure: DO WHILE Condition statements to be repetitively executed…. LOOP zThe while loop only terminates when the condition is False zIf the condition is initially False, the loop may not execute at all !

The problem - filling of a grain store Grain store will hold up to 20 tonnes Lorry will hold up to 1 tonne Programme must tell the operator that this load will exceed the capacity of the store

The problem - filling of a grain store Grain store will hold up to 20 tonnes Lorry will hold up to 1 tonne Programme must tell the operator that this load will exceed the capacity of the store Grain store will hold up to 20 tonnes

The design 1) While store has room, add grain. 1.1 While the grain store can accommodate grain in lorry Get weight of grain on lorry add the lorry load to the grain store weight 1.2 Loop 2) Display message not to accept load 3) End

The programme DIM fLorryWeight AS SINGLE DIM fStoreWeight AS SINGLE PRINT “Grain Store weight system” DO WHILE (fStoreWeight < 20) LOOP PRINT “Do not accept this load as the store will be overfull” END Condition tested INPUT “Enter weight of grain on the lorry”; fLorryWeight fStoreWeight = fStoreWeight + fLorryWeight Statements within loop that will be repetitively executed

The DO...LOOP WHILE zThe DO WHILE...LOOP will only execute the statements within it, if the condition is initially true. You may wish to have a loop construct that executes it’s statements at least once before exiting the loop zThe DO..LOOP WHILE does it’s test at the end of the loop and so will execute it’s statements at least once This last construct is not in the QBasic book

CP1020 principles of programming - Steve Garner and Ian Coulson An example problem zA programme is required to determine the wages of employees given the number of hours worked and their rate of pay  The programme should be capable of repeating the calculation for an unspecified number of employees

The design 1) Calculate the wage of the employees 1.1 do work out employees wage 1.2 Loop till told otherwise get the employees hourly rate get the employees hours worked calculate wage display result ask the user if they want to quit 2) End

The Programme DIM fWage, fRate, fHours AS SINGLE DIM sQuit AS STRING PRINT “This programme calculates an employees wage” DO LOOPWHILE (sQuit <> “q”) END Test condition at end of loopStatements executed within loop INPUT “Hours worked by employee ”; fHours INPUT “Rate of pay for employee”; fRate fWage = fRate * fHours PRINT “employee has earned “; fWage; “ this week” INPUT “Enter q to quit the programme”, sQuit

Revision questions 1 Why would we use a WHILE loop instead of a FOR loop? 2 Write a section of code to calculate the average of a number of inputs ranging from Use 66 to quit. 3Write in three loop constructs a running total for five entered numbers