# Computer Programming by dfhdhdhdhjr

VIEWS: 8 PAGES: 13

• pg 1
```									Software Development
Programming Language Features – Loops

1
Lesson Aims and Objectives

After completing this topic, you should be able to:

1. Use a list box for scrolling output
2. implement fixed loops using For..Next

3. use a counter in a fixed loop

4. implement conditional loops using Do .. Loop Until

5. Use Random numbers

6. Compare Do .. Until and Do .. While loops

2
Repetition using For ..Next

Design, write and test a program to display the
message “Hello” 25 times

Fixed Loop
With a fixed loop the programmer knows in
advance how many times the instructions within
the loop are going to be executed

Look at the design of the solution on page 3 of
Topic 4 notes.

3
Configuring The Number Of Times The
Fixed Loop Runs

Can we change the program so that we can repeat
the instructions within the loop by a number that
is entered by the user?

1. Enter, test and print the code on page 4 of
Topic 4 notes

4
Counting using a Fixed Loop

Can we use a fixed loop to display a count?

1. Design, write and test a program to display the
message 1,2,3,4,5, … 99,100

2. Look at the design of the solution on page 6 of
Topic 4 notes.

3. Do the implementation task on page 7 of the
notes
4. Try modification (1) and modification (6) on
page 7

***** Do Assessment 4: For Loops *****
5
Lesson Aims and Objectives

After completing this lesson, you should be able to:

1. implement conditional loops using Do .. Loop Until

2. Use Random numbers

3. Compare Do .. Until and Do .. While loops

6
Do .. Loop Until

What about times when the number of repetitions is not

For example, a quiz program might give the user repeated
chances to get the answer correct. The programmer doesn’t
know in advance whether the user will get the question right
first time, or take 2, 3, 4 or more attempts.

Do
Line(s) of code
to be repeated

Loop Until condition
7
Example – Do .. Loop Until

Page 8 of Topic 4

1.   Enter code
2. Test, screenshot and print

8
Random Numbers

Visual BASIC provides the programmer with a pre-defined
function Rnd to generate random numbers.

The Rnd function produces random numbers between 0 and
1.
To produce random whole numbers between 1 and 10, we
need to do 3 things:
1. multiply by 10 to produce a random
fraction between 0 and 10
2. “chop off” the fraction part using the pre-
defined function Int
3. add 1, otherwise the highest number will
always be 9, as it is rounded down by the Int
function.
9
Example - Random Numbers

Private Sub cmdStart_Click()
' generates list of random number

Dim number As Integer
Dim counter As Integer
lstRandoms.Clear

For counter = 1 To 10
number = Int(Rnd * 10) + 1
Next counter

End Sub
10
Truly Randomised

If this program runs then it will always generate THE
SAME SET of random numbers.

Private Sub cmdStart_Click()
' generates list of random number

Dim number As Integer
Dim counter As Integer

Randomize
lstRandoms.Clear

For counter = 1 To 10
number = Int(Rnd * 10) + 1
Next

End Sub
11
Practical Do .. Loop Until with random

Code at foot of Page 12

1.   Enter code
2. Test, screenshot and print

12
Finishing Off

1. Look at example 3.6.1. on page 14 (CLASS LISTS)

2. Carefully study the pseudocode and how you
represent looping – you will need to know how to do this

3. Complete Stage 3 (implementation) and stage 4
(Testing) on Page 15

4. Study 4.7 Other Forms of Conditional Loop on page 17

5. Read the Summary of Topic 4 on page 18

13

```
To top