lecture (9)repetition

26
8/14/2019 Lecture (9)Repetition http://slidepdf.com/reader/full/lecture-9repetition 1/26 1 U N I V E R S I T I K U A L A L U M P U R M a l a y s i a F r a n c e I n s t i t u t e FSB23103 FSB23103 Object Oriented Programming Lecture 10 Repetition Mdm Ratnawati Ibrahim

Upload: safuanalcatra

Post on 30-May-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 126

1

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FSB23103 Object OrientedProgramming

Lecture 10

Repetition

Mdm Ratnawati Ibrahim

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 226

2

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Topics

bull Repetition definitionbull Three types of repetition

FOR

WHILE

DO

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 326

3

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition

bull It is used when a block of statements in aprogram are to be repeatedly executed

bull In a computer program a repetition is called aloop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 426

4

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition ndash examples (1)

bull eg Draw 10 random spots on a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 526

5

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition ndash examples (2)

bull Flow diagram

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Repetition

or loop

Check

condition

The method DrawASpot

is defined in Lecture 8

Initialize

counter

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 626

6

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition structures

bull In VB there are three kinds of loops FOR

WHILE

DO

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 726

7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (1)

bull eg Draw 10 random spots

Dim i as Integer

For i = 1 To 10

DrawASpot()

Next

FOR

loop

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Check

condition

Initialize

counter

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 826

8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (2)

bull eg List all odd numbers under 20

ListBox

lstBox

i = 1

falsei lt= 20

lstBoxItemsAdd(CStr(i))

i = i + 2

true

Initialize

counter

Test

condition

Increment

counter by 2

Dim i As Integer

For i = 1 To 20 Step 2

lstBoxItemsAdd(CStr(i))

Next

FOR

loop

Step

Step

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 926

9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (3)

bull For counter = startVal To endVal Step inc statements

Next

bull counter startVal endVal and inc are numbers

bull Step inc may be omitted when inc equals to 1bull For inc gt 0 endVal gt startVal

bull For inc lt 0 endVal lt startVal

bull The number of repetitions is fixed

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 102610

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (1)

bull While condition statements

End While

bull Repeats as long as the condition is met

bull Once repetition stops the statement after theEnd While is the next to be executed

bull The test of the condition is always performedat the beginning of the repetition

bull Consequently the body of the loop may not beexecuted at all

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 112611

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (2)

bull eg Draw 10 random spots

i = 1

While i lt= 10

DrawASpot()i = i + 1

End While

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Initialize counter

Testcondition

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 122612

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (1)

bull Dostatements

Loop

bull Two types of test conditions

While Until

bull Test condition can be placed at either thebeginning or the end of the loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 132613

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (2)

bull Test condition at the beginning of the loop the body of the loop may not be executed at

all

Do While conditionstatements

Loop

Do Until conditionstatements

Loop

Repeats as long as the condition is met

(This is the same as the While loop)

Repeats as long as the condition

is not met

It stops once the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 2: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 226

2

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Topics

bull Repetition definitionbull Three types of repetition

FOR

WHILE

DO

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 326

3

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition

bull It is used when a block of statements in aprogram are to be repeatedly executed

bull In a computer program a repetition is called aloop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 426

4

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition ndash examples (1)

bull eg Draw 10 random spots on a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 526

5

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition ndash examples (2)

bull Flow diagram

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Repetition

or loop

Check

condition

The method DrawASpot

is defined in Lecture 8

Initialize

counter

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 626

6

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition structures

bull In VB there are three kinds of loops FOR

WHILE

DO

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 726

7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (1)

bull eg Draw 10 random spots

Dim i as Integer

For i = 1 To 10

DrawASpot()

Next

FOR

loop

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Check

condition

Initialize

counter

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 826

8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (2)

bull eg List all odd numbers under 20

ListBox

lstBox

i = 1

falsei lt= 20

lstBoxItemsAdd(CStr(i))

i = i + 2

true

Initialize

counter

Test

condition

Increment

counter by 2

Dim i As Integer

For i = 1 To 20 Step 2

lstBoxItemsAdd(CStr(i))

Next

FOR

loop

Step

Step

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 926

9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (3)

bull For counter = startVal To endVal Step inc statements

Next

bull counter startVal endVal and inc are numbers

bull Step inc may be omitted when inc equals to 1bull For inc gt 0 endVal gt startVal

bull For inc lt 0 endVal lt startVal

bull The number of repetitions is fixed

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 102610

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (1)

bull While condition statements

End While

bull Repeats as long as the condition is met

bull Once repetition stops the statement after theEnd While is the next to be executed

bull The test of the condition is always performedat the beginning of the repetition

bull Consequently the body of the loop may not beexecuted at all

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 112611

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (2)

bull eg Draw 10 random spots

i = 1

While i lt= 10

DrawASpot()i = i + 1

End While

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Initialize counter

Testcondition

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 122612

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (1)

bull Dostatements

Loop

bull Two types of test conditions

While Until

bull Test condition can be placed at either thebeginning or the end of the loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 132613

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (2)

bull Test condition at the beginning of the loop the body of the loop may not be executed at

all

Do While conditionstatements

Loop

Do Until conditionstatements

Loop

Repeats as long as the condition is met

(This is the same as the While loop)

Repeats as long as the condition

is not met

It stops once the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 3: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 326

3

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition

bull It is used when a block of statements in aprogram are to be repeatedly executed

bull In a computer program a repetition is called aloop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 426

4

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition ndash examples (1)

bull eg Draw 10 random spots on a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 526

5

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition ndash examples (2)

bull Flow diagram

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Repetition

or loop

Check

condition

The method DrawASpot

is defined in Lecture 8

Initialize

counter

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 626

6

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition structures

bull In VB there are three kinds of loops FOR

WHILE

DO

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 726

7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (1)

bull eg Draw 10 random spots

Dim i as Integer

For i = 1 To 10

DrawASpot()

Next

FOR

loop

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Check

condition

Initialize

counter

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 826

8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (2)

bull eg List all odd numbers under 20

ListBox

lstBox

i = 1

falsei lt= 20

lstBoxItemsAdd(CStr(i))

i = i + 2

true

Initialize

counter

Test

condition

Increment

counter by 2

Dim i As Integer

For i = 1 To 20 Step 2

lstBoxItemsAdd(CStr(i))

Next

FOR

loop

Step

Step

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 926

9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (3)

bull For counter = startVal To endVal Step inc statements

Next

bull counter startVal endVal and inc are numbers

bull Step inc may be omitted when inc equals to 1bull For inc gt 0 endVal gt startVal

bull For inc lt 0 endVal lt startVal

bull The number of repetitions is fixed

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 102610

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (1)

bull While condition statements

End While

bull Repeats as long as the condition is met

bull Once repetition stops the statement after theEnd While is the next to be executed

bull The test of the condition is always performedat the beginning of the repetition

bull Consequently the body of the loop may not beexecuted at all

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 112611

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (2)

bull eg Draw 10 random spots

i = 1

While i lt= 10

DrawASpot()i = i + 1

End While

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Initialize counter

Testcondition

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 122612

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (1)

bull Dostatements

Loop

bull Two types of test conditions

While Until

bull Test condition can be placed at either thebeginning or the end of the loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 132613

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (2)

bull Test condition at the beginning of the loop the body of the loop may not be executed at

all

Do While conditionstatements

Loop

Do Until conditionstatements

Loop

Repeats as long as the condition is met

(This is the same as the While loop)

Repeats as long as the condition

is not met

It stops once the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 4: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 426

4

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition ndash examples (1)

bull eg Draw 10 random spots on a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 526

5

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition ndash examples (2)

bull Flow diagram

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Repetition

or loop

Check

condition

The method DrawASpot

is defined in Lecture 8

Initialize

counter

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 626

6

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition structures

bull In VB there are three kinds of loops FOR

WHILE

DO

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 726

7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (1)

bull eg Draw 10 random spots

Dim i as Integer

For i = 1 To 10

DrawASpot()

Next

FOR

loop

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Check

condition

Initialize

counter

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 826

8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (2)

bull eg List all odd numbers under 20

ListBox

lstBox

i = 1

falsei lt= 20

lstBoxItemsAdd(CStr(i))

i = i + 2

true

Initialize

counter

Test

condition

Increment

counter by 2

Dim i As Integer

For i = 1 To 20 Step 2

lstBoxItemsAdd(CStr(i))

Next

FOR

loop

Step

Step

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 926

9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (3)

bull For counter = startVal To endVal Step inc statements

Next

bull counter startVal endVal and inc are numbers

bull Step inc may be omitted when inc equals to 1bull For inc gt 0 endVal gt startVal

bull For inc lt 0 endVal lt startVal

bull The number of repetitions is fixed

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 102610

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (1)

bull While condition statements

End While

bull Repeats as long as the condition is met

bull Once repetition stops the statement after theEnd While is the next to be executed

bull The test of the condition is always performedat the beginning of the repetition

bull Consequently the body of the loop may not beexecuted at all

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 112611

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (2)

bull eg Draw 10 random spots

i = 1

While i lt= 10

DrawASpot()i = i + 1

End While

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Initialize counter

Testcondition

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 122612

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (1)

bull Dostatements

Loop

bull Two types of test conditions

While Until

bull Test condition can be placed at either thebeginning or the end of the loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 132613

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (2)

bull Test condition at the beginning of the loop the body of the loop may not be executed at

all

Do While conditionstatements

Loop

Do Until conditionstatements

Loop

Repeats as long as the condition is met

(This is the same as the While loop)

Repeats as long as the condition

is not met

It stops once the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 5: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 526

5

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition ndash examples (2)

bull Flow diagram

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Repetition

or loop

Check

condition

The method DrawASpot

is defined in Lecture 8

Initialize

counter

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 626

6

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition structures

bull In VB there are three kinds of loops FOR

WHILE

DO

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 726

7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (1)

bull eg Draw 10 random spots

Dim i as Integer

For i = 1 To 10

DrawASpot()

Next

FOR

loop

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Check

condition

Initialize

counter

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 826

8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (2)

bull eg List all odd numbers under 20

ListBox

lstBox

i = 1

falsei lt= 20

lstBoxItemsAdd(CStr(i))

i = i + 2

true

Initialize

counter

Test

condition

Increment

counter by 2

Dim i As Integer

For i = 1 To 20 Step 2

lstBoxItemsAdd(CStr(i))

Next

FOR

loop

Step

Step

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 926

9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (3)

bull For counter = startVal To endVal Step inc statements

Next

bull counter startVal endVal and inc are numbers

bull Step inc may be omitted when inc equals to 1bull For inc gt 0 endVal gt startVal

bull For inc lt 0 endVal lt startVal

bull The number of repetitions is fixed

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 102610

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (1)

bull While condition statements

End While

bull Repeats as long as the condition is met

bull Once repetition stops the statement after theEnd While is the next to be executed

bull The test of the condition is always performedat the beginning of the repetition

bull Consequently the body of the loop may not beexecuted at all

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 112611

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (2)

bull eg Draw 10 random spots

i = 1

While i lt= 10

DrawASpot()i = i + 1

End While

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Initialize counter

Testcondition

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 122612

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (1)

bull Dostatements

Loop

bull Two types of test conditions

While Until

bull Test condition can be placed at either thebeginning or the end of the loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 132613

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (2)

bull Test condition at the beginning of the loop the body of the loop may not be executed at

all

Do While conditionstatements

Loop

Do Until conditionstatements

Loop

Repeats as long as the condition is met

(This is the same as the While loop)

Repeats as long as the condition

is not met

It stops once the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 6: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 626

6

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

Repetition structures

bull In VB there are three kinds of loops FOR

WHILE

DO

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 726

7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (1)

bull eg Draw 10 random spots

Dim i as Integer

For i = 1 To 10

DrawASpot()

Next

FOR

loop

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Check

condition

Initialize

counter

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 826

8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (2)

bull eg List all odd numbers under 20

ListBox

lstBox

i = 1

falsei lt= 20

lstBoxItemsAdd(CStr(i))

i = i + 2

true

Initialize

counter

Test

condition

Increment

counter by 2

Dim i As Integer

For i = 1 To 20 Step 2

lstBoxItemsAdd(CStr(i))

Next

FOR

loop

Step

Step

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 926

9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (3)

bull For counter = startVal To endVal Step inc statements

Next

bull counter startVal endVal and inc are numbers

bull Step inc may be omitted when inc equals to 1bull For inc gt 0 endVal gt startVal

bull For inc lt 0 endVal lt startVal

bull The number of repetitions is fixed

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 102610

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (1)

bull While condition statements

End While

bull Repeats as long as the condition is met

bull Once repetition stops the statement after theEnd While is the next to be executed

bull The test of the condition is always performedat the beginning of the repetition

bull Consequently the body of the loop may not beexecuted at all

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 112611

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (2)

bull eg Draw 10 random spots

i = 1

While i lt= 10

DrawASpot()i = i + 1

End While

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Initialize counter

Testcondition

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 122612

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (1)

bull Dostatements

Loop

bull Two types of test conditions

While Until

bull Test condition can be placed at either thebeginning or the end of the loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 132613

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (2)

bull Test condition at the beginning of the loop the body of the loop may not be executed at

all

Do While conditionstatements

Loop

Do Until conditionstatements

Loop

Repeats as long as the condition is met

(This is the same as the While loop)

Repeats as long as the condition

is not met

It stops once the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 7: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 726

7

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (1)

bull eg Draw 10 random spots

Dim i as Integer

For i = 1 To 10

DrawASpot()

Next

FOR

loop

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Check

condition

Initialize

counter

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 826

8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (2)

bull eg List all odd numbers under 20

ListBox

lstBox

i = 1

falsei lt= 20

lstBoxItemsAdd(CStr(i))

i = i + 2

true

Initialize

counter

Test

condition

Increment

counter by 2

Dim i As Integer

For i = 1 To 20 Step 2

lstBoxItemsAdd(CStr(i))

Next

FOR

loop

Step

Step

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 926

9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (3)

bull For counter = startVal To endVal Step inc statements

Next

bull counter startVal endVal and inc are numbers

bull Step inc may be omitted when inc equals to 1bull For inc gt 0 endVal gt startVal

bull For inc lt 0 endVal lt startVal

bull The number of repetitions is fixed

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 102610

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (1)

bull While condition statements

End While

bull Repeats as long as the condition is met

bull Once repetition stops the statement after theEnd While is the next to be executed

bull The test of the condition is always performedat the beginning of the repetition

bull Consequently the body of the loop may not beexecuted at all

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 112611

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (2)

bull eg Draw 10 random spots

i = 1

While i lt= 10

DrawASpot()i = i + 1

End While

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Initialize counter

Testcondition

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 122612

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (1)

bull Dostatements

Loop

bull Two types of test conditions

While Until

bull Test condition can be placed at either thebeginning or the end of the loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 132613

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (2)

bull Test condition at the beginning of the loop the body of the loop may not be executed at

all

Do While conditionstatements

Loop

Do Until conditionstatements

Loop

Repeats as long as the condition is met

(This is the same as the While loop)

Repeats as long as the condition

is not met

It stops once the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 8: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 826

8

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (2)

bull eg List all odd numbers under 20

ListBox

lstBox

i = 1

falsei lt= 20

lstBoxItemsAdd(CStr(i))

i = i + 2

true

Initialize

counter

Test

condition

Increment

counter by 2

Dim i As Integer

For i = 1 To 20 Step 2

lstBoxItemsAdd(CStr(i))

Next

FOR

loop

Step

Step

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 926

9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (3)

bull For counter = startVal To endVal Step inc statements

Next

bull counter startVal endVal and inc are numbers

bull Step inc may be omitted when inc equals to 1bull For inc gt 0 endVal gt startVal

bull For inc lt 0 endVal lt startVal

bull The number of repetitions is fixed

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 102610

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (1)

bull While condition statements

End While

bull Repeats as long as the condition is met

bull Once repetition stops the statement after theEnd While is the next to be executed

bull The test of the condition is always performedat the beginning of the repetition

bull Consequently the body of the loop may not beexecuted at all

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 112611

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (2)

bull eg Draw 10 random spots

i = 1

While i lt= 10

DrawASpot()i = i + 1

End While

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Initialize counter

Testcondition

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 122612

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (1)

bull Dostatements

Loop

bull Two types of test conditions

While Until

bull Test condition can be placed at either thebeginning or the end of the loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 132613

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (2)

bull Test condition at the beginning of the loop the body of the loop may not be executed at

all

Do While conditionstatements

Loop

Do Until conditionstatements

Loop

Repeats as long as the condition is met

(This is the same as the While loop)

Repeats as long as the condition

is not met

It stops once the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 9: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 926

9

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceI

nstit

ute

FSB23103

FOR loop (3)

bull For counter = startVal To endVal Step inc statements

Next

bull counter startVal endVal and inc are numbers

bull Step inc may be omitted when inc equals to 1bull For inc gt 0 endVal gt startVal

bull For inc lt 0 endVal lt startVal

bull The number of repetitions is fixed

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 102610

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (1)

bull While condition statements

End While

bull Repeats as long as the condition is met

bull Once repetition stops the statement after theEnd While is the next to be executed

bull The test of the condition is always performedat the beginning of the repetition

bull Consequently the body of the loop may not beexecuted at all

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 112611

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (2)

bull eg Draw 10 random spots

i = 1

While i lt= 10

DrawASpot()i = i + 1

End While

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Initialize counter

Testcondition

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 122612

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (1)

bull Dostatements

Loop

bull Two types of test conditions

While Until

bull Test condition can be placed at either thebeginning or the end of the loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 132613

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (2)

bull Test condition at the beginning of the loop the body of the loop may not be executed at

all

Do While conditionstatements

Loop

Do Until conditionstatements

Loop

Repeats as long as the condition is met

(This is the same as the While loop)

Repeats as long as the condition

is not met

It stops once the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 10: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 102610

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (1)

bull While condition statements

End While

bull Repeats as long as the condition is met

bull Once repetition stops the statement after theEnd While is the next to be executed

bull The test of the condition is always performedat the beginning of the repetition

bull Consequently the body of the loop may not beexecuted at all

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 112611

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (2)

bull eg Draw 10 random spots

i = 1

While i lt= 10

DrawASpot()i = i + 1

End While

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Initialize counter

Testcondition

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 122612

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (1)

bull Dostatements

Loop

bull Two types of test conditions

While Until

bull Test condition can be placed at either thebeginning or the end of the loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 132613

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (2)

bull Test condition at the beginning of the loop the body of the loop may not be executed at

all

Do While conditionstatements

Loop

Do Until conditionstatements

Loop

Repeats as long as the condition is met

(This is the same as the While loop)

Repeats as long as the condition

is not met

It stops once the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 11: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 112611

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

WHILE loop (2)

bull eg Draw 10 random spots

i = 1

While i lt= 10

DrawASpot()i = i + 1

End While

i = 1

falsei lt= 10

DrawASpot()

i = i + 1

true

Initialize counter

Testcondition

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 122612

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (1)

bull Dostatements

Loop

bull Two types of test conditions

While Until

bull Test condition can be placed at either thebeginning or the end of the loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 132613

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (2)

bull Test condition at the beginning of the loop the body of the loop may not be executed at

all

Do While conditionstatements

Loop

Do Until conditionstatements

Loop

Repeats as long as the condition is met

(This is the same as the While loop)

Repeats as long as the condition

is not met

It stops once the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 12: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 122612

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (1)

bull Dostatements

Loop

bull Two types of test conditions

While Until

bull Test condition can be placed at either thebeginning or the end of the loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 132613

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (2)

bull Test condition at the beginning of the loop the body of the loop may not be executed at

all

Do While conditionstatements

Loop

Do Until conditionstatements

Loop

Repeats as long as the condition is met

(This is the same as the While loop)

Repeats as long as the condition

is not met

It stops once the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 13: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 132613

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (2)

bull Test condition at the beginning of the loop the body of the loop may not be executed at

all

Do While conditionstatements

Loop

Do Until conditionstatements

Loop

Repeats as long as the condition is met

(This is the same as the While loop)

Repeats as long as the condition

is not met

It stops once the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 14: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 142614

U

NIVERS

ITIKUAL

A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (3)

bull eg Draw 10 random spots

i = 1

Do Until i gt 10

DrawASpot()i = i + 1

Loop

Initialize counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 15: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 152615

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (4)

bull Test condition at the end of the loop the body of the loop is executed at least

once

Dostatements

Loop While condition

Dostatements

Loop Until condition

Repeats as long as the

condition is met

Repetition stops once

the condition is met

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 16: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 162616

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

DO loop (5)

bull eg Draw 10 random spots

NoteWhen test condition is

at the end of a DO loop

the loop body is

executed at least once

i = 1

Do

DrawASpot()i = i + 1

Loop Until i gt 10

Initialize

counter

Test

condition

i = 1

truei gt 10

DrawASpot()

i = i + 1

false

Increment

counter by 1

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 17: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 172617

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

FOR or WHILE amp DO loops

bull Use FOR loop when the number of repetitions is fixed

the loop control variable is incrementedregularly

bull Use DO or WHILE loops when the precise number of repetitions is

unknown or

the loop control variable is not being

changed in a regular manner

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 18: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 182618

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (1)

bull Exit For - FOR loop bull Exit While - WHILE loop

bull Exit Do - DO loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 19: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 192619

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (2)

bull eg List a series of numbers

and their sumif the sum is less than 100

sum = 0

false

i lt= 20

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

i = i + 1

true

IF

Selection

i = 1

false

true

sum lt 100

sum += iFOR

loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 20: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 202620

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Exit from a loop (3)

bull eg use For loop

Dim i sum As Integer

lstBoxItemsAdd(Number amp Tab amp Sum amp NewLine)

sum = 0

For i = 1 To 20

sum += i

If sum lt 100 Then

lstBoxItemsAdd(CStr(i) amp Tab amp _

CStr(sum) amp NewLine)

Else

Exit ForEnd If

Next

To use control characters you need to add

Imports MicrosoftVisualBasicControlChars

at the top of your programNote In this case it is

better to use While loop

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 21: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 212621

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (1)

bull eg Draw a given number of random spotson a picture box

Random Spots

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 22: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 222622

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (2)

bull Form design

Button

cmdDrawASpot

Button

cmdClear

Button

cmdClose

Button

cmdDraw

TextBoxtxbNSpotPictureBox

picBox

Label

lblEnter

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 23: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 232623

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (3)

bull Event handing method cmdDraw_Click Private Sub cmdDraw_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) Handles cmdDrawClick

Dim i As Integer

Dim n As Integer = GetNumber(txbNSpotsText)

If n gt 0 Then

For i = 1 To n

DrawASpot()

Next

ElseMessageBoxShow(Please enter a positive integer)

txbNSpotsText =

txbNSpotsFocus()

End If

End Sub

FOR

loop

Get userinput

Check input

Handle invalid input

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 24: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 242624

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (4)

Private Function GetNumber(ByVal str As String) As Integer

validate input string

If IsNumeric(str) Then

If CInt(str) gt= 0 Then

Return CInt(str)Else

Return -1 to notify user about negative input

End If

Else

Return -2 to notify user about non-numeric input

End If

End Function

Note The built-in function IsNumeric(str) returns true

if the string str contains a number false otherwise

Function GetNUmber

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 25: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 252625

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Example ndash random spots (5)

Private Sub DrawASpot()

(xy) is the top-left corner of the binding square of a circle

Dim x y diameter red green blue As Integer

paper = picBox1CreateGraphics

diameter = aRandomNoNext(20 50) set circle diameter

set x and y within the picture box

x = aRandomNoNext( 0 picBox1SizeWidth-diameter )

y = aRandomNoNext( 0 picBox1SizeHeight-diameter )

set amount of colour for red green and blue

red = aRandomNoNext(0 255)

green = aRandomNoNext(0 255)blue = aRandomNoNext(0 255)

myBrush = New SolidBrush(ColorFromArgb(red green blue))

paperFillEllipse(myBrush x y diameter diameter)

End Sub

NoteColour range in RGB is 0 to 255

Method DrawASpot

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition

Page 26: Lecture (9)Repetition

8142019 Lecture (9)Repetition

httpslidepdfcomreaderfulllecture-9repetition 2626

U

NIVERS

ITIKUAL A

LUMPUR

M

alaysi a

FranceInstit

ute

FSB23103

Summary

bull Three kinds of repetitions or loops

FOR

WHILE

DO

To exit from a FOR loop

Exit For

To exit from a WHILE loop

Exit While

To exit from a DO loop

Exit Do

For counter = startVal To endVal Step inc

statements

Next

While condition

statements

End While

Do While condition

statementsLoop

Do

statements

Loop While condition

Do Until condition

statementsLoop

Do

statements

Loop Until condition