CP1020 - Week 6 Repetition. Aims and Objectives zUnderstand what loops are and why they are needed zUnderstand the FOR loop used in programming zUnderstand.

Slides:



Advertisements
Similar presentations
Looping Structures: Do Loops
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.1 Chapter 5 Loops.
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
Computer Science 1620 Loops.
CP Week 10 Modularising programs using Procedures.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
CP Week 2 zReserved words zVariables zInput and output zData types zTesting and Documentation.
For Next Looping My first Looping Structure. For…Next FOR counter_variable = initial_value TO end_value STEP increment Statement-1 Statement-2 ……. Statement-n.
CP Week 9 Working with arrays. What are we doing this week ? zHow to store information in an array zHow to retrieve information from an array zA.
CP Principles of Programming Steve Garner and Ian Coulson CP Week 8 Arrays.
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.
1 Principles of Software Design and Development LOOPS / REPITITION.
Iteration Conditional Loops Counted Loops. Charting the Flow of Control We’ve used flow charts to visualise the flow of control. The simplest form is.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
Chapter 5 Repetition or loop structure. What is repetition or loop? repeat the execution of one or a group (block; instruction enclosed in a pair of braces)
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
ENGR 112 Decision Structures.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
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.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
Tutorial 6 The Repetition Structure
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
Adding 10 items to a list… lstTest.Items.Add(1) lstTest.Items.Add(2) lstTest.Items.Add(3) lstTest.Items.Add(4) lstTest.Items.Add(5) lstTest.Items.Add(6)
6.2 For…Next Loops General Form of a For…Next Loop
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Controlling Program Flow with Looping Structures
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Loops ISYS 350. Write a Program that asks user to enter any numbers and displays the largest number Process: Largest = the first number Get the next number.
Chapter 6 Controlling Program Flow with Looping Structures.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
REPETITION CONTROL STRUCTURE
Clearly Visual Basic: Programming with Visual Basic nd Edition
Lists, Loops, Validation, and More
Counted Loops.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter (3) - Looping Questions.
CIS 16 Application Development Programming with Visual Basic
Let’s all Repeat Together
The structure of programming
Java Programming Loops
ASP control structure BRANCHING STATEMENTS
Conditional Loops Counted Loops
Programming Fundamental
Presentation transcript:

CP Week 6 Repetition

Aims and Objectives zUnderstand what loops are and why they are needed zUnderstand the FOR loop used in programming zUnderstand the concept of nesting loops and why they may be necessary zBe able to design and code loops in QBasic

The Three Main Programming Constructs zSo far we have looked at: zSequence - the ordering of program statements INPUT “the wholesale cost of the article”; fCost LET fRetail = 2 * fCost PRINT “the retail cost of the article is ”; fRetail zSelection - making decisions in our code If...Then...Else... If...Then...Elseif... Select...Case

Repetition zWe can now introduce the third construct: zIteration - looping or repetition yallows one or more statements to be repeated either a fixed number of times or until a particular condition(s) is satisfied

Count Down Loop If Problem THEN Abort Else Ignite rocket Disconnect From Launch Pad

Times Table Problem A program is required to display the times table for a number inputted by the user. zSuppose the number inputted was 6, the program should then generate the 6 times table as follows: 1 x 6 is 6 2 x 6 is 12 3 x 6 is 18 etc x 6 is x 6 is 72

Times Table Initial Design zBased only on what we have done so far: 1 Read value in from keyboard 2 Display times table for value entered 3 End program zThis looks OK, but let’s expand step 2...

Times Table Expanded Design z1Read value in from keyboard 2Display times table for value entered 2.1Display title 2.2Display 1 * value entered 2.3Display 2 * value entered 2.4Display 3 * value entered 2.5Display 4 * value entered 2.6Display 5 * value entered 2.7Display 6 * value entered

Times table continued 2.8Display 7 * value entered 2.9Display 8 * value entered 2.10Display 9 * value entered 2.11Display 10 * value entered 2.12Display 11 * value entered 2.13Display 12 * value entered 3 End program

Times Table Code zCode for the long winded solution: DIM iTimes AS INTEGER INPUT iTimes PRINT "The "; iTimes; " Times Table" PRINT "1 * "; iTimes; " = "; 1 * iTimes PRINT "2 * "; iTimes; " = "; 2 * iTimes PRINT "3 * "; iTimes; " = "; 3 * iTimes PRINT "4 * "; iTimes; " = "; 4 * iTimes PRINT "5 * "; iTimes; " = "; 5 * iTimes PRINT "6 * "; iTimes; " = "; 6 * iTimes PRINT "7 * "; iTimes; " = "; 7 * iTimes

Times table problem zPRINT "8 * "; iTimes; " = "; 8 * iTimes PRINT "9 * "; iTimes; " = "; 9 * iTimes PRINT "10 * "; iTimes; " = "; 10 * iTimes PRINT "11 * "; iTimes; " = "; 11 * iTimes PRINT "12 * "; iTimes; " = "; 12 * iTimes END zClearly this is very tedious and becomes unworkable as the number of repeat task increases zHowever I have cunning plan!

Times Table Looping Design zA new design, this time using a For...Next loop 1Read value in from keyboard 2Display title 3Display times table for value entered 3.1 For Counter (1 to 12) 3.1.1Display Counter X value entered End For 4End program zThis type of loop is used to perform a statement (or set of statements) a fixed number of times zThis is ideal for our times table problem

For...Next Loop Construct zThe For...Next construct should be used when you know how may times the loop should be executed before entering the loop zGeneral structure: FOR Count = StartValue TO EndValue [STEP increment] statements... NEXT [Count] Count can be any of these types: yInteger or Long ySingle or Double

Times Table Looping Code zCode for the looping version of the times table: DIM iCount AS INTEGER DIM iTimes AS INTEGER INPUT iTimes CLS PRINT "The "; iTimes; " Times Table" FOR iCount = 1 TO 12 PRINT iCount;" * ";iTimes;" = ";iCount * iTimes NEXT iCount END E.g. 2 * 6 = 12

6 Times Table Example Output The 6 times table 1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36 7 * 6 = 42 8 * 6 = 48 9 * 6 = * 6 = * 6 = * 6 = 72

For...Next Loop Step zWe can also specify the step size and count backwards: DIM iCount AS INTEGER FOR iCount = 100 TO 0 STEP -10 PRINT "iCount = "; iCount NEXT iCount END SUB zUnless a STEP is specified the For loop will increment by 1 each time zYou should never change the value of the control variable (iCount) inside the For loop

Nesting Loops zConsider the following: A program is required to print out the full 12 times table z A typical screen could be as follows: 1 times table times table times table times table times table times table and so on……...

Design For Full Times Tables Step 1 For RowCounter (1 to 12) 1.1 For ColumnCounter (1 to 12) Do Display RowCounter x ColumnCounter End For 1.2 Display a new line End For 2End program

Coding DIM iRowCount AS INTEGER ‘line counter DIM iColumnCount AS INTEGER ‘item on line counter CLS FOR iRowCount = 1 TO 12 PRINT iRowCount; "Times table: "; FOR iColumnCount = 1 TO 12 PRINT iColumnCount * iRowCount; NEXT iColumnCount PRINT NEXT iRowCount END

Questions 1 When would you use a For loop? 2 What will the output from the following code be? For iRow = 6 To 4 Step -1 For iCol = -3 To 1 Step 2 Print iRow; " * "; iCol; " = "; iRow * iCol Next iCol Print Next iRow Return to view another lecture