cw-v1 sdd 0901 principals of software design and development loops starter: water jugswater jugs

14
CW-V1 SDD 090 1 Principals of Software Design and Development Loops Starter: Water Jugs

Upload: damon-young

Post on 17-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 1

Principals of Software Design and Development

Loops

Starter: Water Jugs

Page 2: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 2

Activity 1 - Review prior learning -10 mins

A-Z GameUsing scrap paper write down all the letters

of the alphabetEg A

B C

Now try to write down all keywords relating to VB but especially relating to the last lesson

Page 3: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 3

Learning outcomes

Describe loops and their uses Describe various types of loop Create short programs which incorporate loops Explain to others code used to create loops Explain design methods used to design any program Apply learning to various scenarios (loop programs) Judge which loops to use in various scenarios Describe task 4 a

Page 4: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 4

What is a Loop?

A piece of code that repeats Saves on a lot of typing! There are 3 main types in VB6

Do…While Do…Loop Until For…Next

Block statements that contain code

Page 5: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 5

Code without LoopsDim UserNumber as Integer

UserNumber = InputBox(“Please enter a number 1 - 10”)

If UserNumber < 10 then Print UserNumber UserNumber = UserNumber + 1 Print UserNumberEndIf

If UserNumber < 10 then Print UserNumber UserNumber = UserNumber + 1 Print UserNumberEndIf

Page 6: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 6

LOOP 1 TYPEDo…While Loop Has a test at the beginning – like an if statement

Will not execute (run) if the test result is false eg see below code - so if the user number gets to 10 or above it will stop running!!

Make a new form in a new project and put code on a command button

Sub cmdLooper_click

Dim UserNumber as Integer

UserNumber = InputBox(“Please enter a number 1 - 10”)

Print UserNumber

Do While UserNumber < 10

UserNumber = UserNumber + 1

Print UserNumber

Loop

Page 7: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 7

Do…While FlowchartStart

End

UserNumber

< 10 ?

Print UserNumber

Print UserNumber

LoopTest

YesNo

Page 8: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 8

LOOP 2 TYPEDo…Loop Until

Has a test at the end of the statement This means the loop will always

execute at least once (it will run once because you don’t ask the question until the end of the loop

Very useful for password routines!

Page 9: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 9

LOOP 2 TYPE Do…Loop Until Example – Loop until the product is greater than 25Attach this code to the click event of the formPrivate Sub Form_Click()

Dim FirstNo As Integer

Dim SecondNo As Integer

Dim Product As Integer

Do

FirstNo = InputBox("Enter the First Number")

SecondNo = InputBox("Enter the Second Number")

Product = FirstNo * SecondNo

Print "The Product of " & FirstNo; " And " & _

SecondNo & " Is " & Product

Loop Until Product > 25

Print "Product is now over 25. Time to stop!"

End Sub

Page 10: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 10

Do…Loop Until FlowchartStart

End

Input FirstNo

Input SecondNo

FirstNo * SecondNo

Print Product

Product > 25?

Loop

Test

Page 11: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 11

LOOP 3 TYPE For…Next Loop

The loop will execute a specified number of times – so you can set how many times you want the program to repeat EG 3 FOR A PASSWORD SYSTEM

Controlled by a counter that increments each time the loop executes

Change the increments on the counter by using the step keyword

Page 12: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 12

LOOP 3 TYPE For…Next Code & FlowchartAttach this code to the load event of a form

Sub frmPrinter_load

Dim i As Integer

For i = 65 To 90

Print i; Tab; Chr(i)

Next i

Dim i as Integer

For i = 1 to 100 Step 2

Print i

Next i

Start

End

Print counter value

Print letter

Max counter value

reached?

i is the counter which counts how many times the loop has run

Page 13: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 13

Activity 3 – Apply your learning - 25 mins

Using SDD 100 create loop programs Complete tasks 1& 2

Early finishers to try task 3&4

Be prepared to answer questions on all the loops in SDD 100

All to try out tasks 3&4 as homework The ultimate is to complete tasks 5 & 6

Page 14: CW-V1 SDD 0901 Principals of Software Design and Development Loops Starter: Water JugsWater Jugs

CW-V1 SDD 090 14

What have you learned today?

Described loops and their uses Described various types of loop Created short programs which incorporate loops Explained to others code used to create loops Explained design methods used to design any

program Applied learning to various scenarios (loop

programs) Judged which loops to use in various scenarios Now task 4 a