COMPUTER
SCIENCE
Loops/iteration
-3 main loops used in computer science
- One example is the COUNT CONTROLED LOOP, it is also known as the FOR…..TO…..NEXT loop.
Example;
For count = 1 to 4
[Link] (“my name is Jewel”)
Next (makes the variable count take the next value)
count Output
1 My name is Jewel
2 My name is Jewel
3 My name is Jewel
4 My name is Jewel
- Well after 4, maybe 5, the NEXT makes the variable count takes it and then 5 if examined by the loop if
it’s within the range. Well 5 is not, so the loop terminates.
Programming constructs
There are 3 programming constructs namely; sequence, selection, and iteration
1. Sequence
Programing statements are executed one after the other in the order in which they are written.
1. A = 1
2. b= a + 10
3. C = 15
4. A = b + c
5. B = c
6. A = 0
a b c
1
11
15
26
15
0
Selection
- Is when a program has to check a condition, and follow different paths depending on the stated
conditions?
- Conditional statements are used for selection.
Examples are:
1. IF…THEN…ENDIF
2. CASE…OF…OTHERWISE…ENDCASE
Examples:
write a program which prompts the user to input a student name and mark. The program must output A*
if the mark is between 90-100, B if mark is 80-89, C if the mark is 70-79… up to U.
The If statement
Module Module1
Dim name As String
Dim mark As Integer
Sub Main()
[Link]("please enter your name")
name = [Link]()
[Link]("please enter your mark")
mark = [Link]()
If mark > 89 And mark < 101 Then
[Link]("you have an A*")
ElseIf mark > 79 And mark < 91 Then
[Link]("you have an A")
ElseIf mark > 69 And mark < 80 Then
[Link]("you have a B")
ElseIf mark > 59 And mark < 70 Then
[Link]("you have a C")
ElseIf mark > 49 And mark < 60 Then
[Link]("you have a D")
ElseIf mark > 39 And mark < 50 Then
[Link]("you have an E")
ElseIf mark < 39 Then
[Link]("you are ungraded, poor and pathetic performance ")
End If
[Link]()
End Sub
End Module
“CASE”
Module Module1
Dim stname As String
Dim mark As Integer
Sub Main()
[Link]("please enter your name")
stname = [Link]()
[Link]("please enter your mark")
mark = [Link]()
Select Case mark
Case 90 To 100
[Link]("you have an A")
Case 80 To 89
[Link]("you have an A")
Case 70 To 79
[Link]("you have a B")
Case 60 To 69
[Link]("you have C")
Case 50 To 59
[Link]("you have a D")
Case 40 To 49
[Link]("you have E")
Case 0 To 39
[Link]("you have a U, pathetic")
Case Else
[Link]("you have entered an invalid mark")
End Select
[Link]()
End Sub
End Module
Iteration/repetition/looping
- When a group of statements have to be repeated we use iteration.
- There are 3 types of loops used:
1. Count controlled loop (for…to…next).
2. Pre-conditional loop (while…do…endwhile).
3. Post condition loop (do…until).
Count Controlled loop.
- It is used when the number of iterations is known or when you are given the lower and upper limits.
- In V.B we use a for…to…next (FOR…TO…ENDFOR in Pseudocode)
Example:
Write a program that outputs your name 10 times.
Module Module1
Dim name As String
Sub Main()
For count = 1 To 10
[Link]("My name is Jewel")
name = [Link]()
Next
[Link]()
End Sub
End Module
Pre-condition loop
- It tests the condition at the top, which means the program may not enter the loop at all if
the condition is not met and the condition does not give the value of true.
- It cannot count on its own. If counting is needed then it requires the user to declare the
counting variable, initialise it and increment it.
- As long as the condition is false, it will keep on repeating (iterate) until the condition is
true.
Example:
Write program which outputs your name 10 times.
Module Module1
Dim count As Integer = 10
Dim name As String
Sub Main()
count = 0
While count <= 9
[Link]("my name is bijou")
count = count + 1
End While
[Link]()
End Sub
End Module
Write a program which asks the user if they want to withdraw more money and they must
press y if they want to withdraw and n if they don’t want.
Module Module1
Dim choice As Char
Sub Main()
choice = "y"
While choice = "y"
[Link]("voici ta US$10")
[Link]()
[Link] ("est ce que vous voulez prener le d'argent encore?")
choice = [Link]()
choice = "n"
While choice = "n"
[Link]("merci beaucoup, vous allez pouvez maintenant")
choice = [Link]()
End While
End While
End Sub
End Module
Or rather, instead of doing another while loop.
Pseudocode example
while choice = “y”
enter
end while
1. Write a program which prompts the user to input the amount received and keep adding it
to a running total. The program must stop when the total is greater than 1000 and
display the name and total (12)
Break the problem down.
1. Prompt the user to input amount received.
2. Keep a running total.
3. Stop when total >1000
4. Output name and total at the end.
Module Module1
Dim amountreceived, runningtotal As Double
Dim name As String
Sub Main()
[Link]("please enter your name")
name = [Link]()
runningtotal = 0
Do
[Link]("please enter amount received")
amountreceived = [Link]()
runningtotal = runningtotal + amountreceived
Loop Until runningtotal > 1000
[Link]("your total is" & runningtotal)
[Link]("your name is" & name)
[Link]()
End Sub
End Module
2. Write a program which prompts the user to input 10 number, and then displays the total
of negative numbers and the total of positive numbers, the number of positive and
negative numbers input.
Module Module1
Dim num, sum, possum, negsum, poscount, negcount, Posav, negav As Double
Sub Main()
poscount = 0
negcount = 0
sum = 0
For count = 1 To 10
[Link]("enter a number")
num = [Link]()
If num > 0 Then
poscount = poscount + 1
possum = possum + num
ElseIf num < 0 Then
negcount = negcount + 1
negsum = negsum + 1
End If
Next
Posav = possum / poscount
negav = negsum / negcount
[Link]("the total of negative numbers is: " & negsum)
[Link]("the number of negative numbers are: " & negcount)
[Link]("the total of positive numbers is: " & possum)
[Link]("the number of positive numbers is: " & poscount)
[Link]()
End Sub
End Module
Post condition loop (do…until) = (repeat…until)
- It tests the condition at the bottom, the first input/iteration will not be checked against the
condition.
Pseudocode example;
choice = y
repeat
output “enter name”
INPUT Name
output “do you want to enter another name
press Y or N”
input choice
until choice = “N”
problems to be done
Start
Engine = 0, Count = 0,
Num = 0
Input size
Yes
Average Output
=engine/n average,
um count
Is size >1.5 Count = count + 1
Stop
no
Num = num + 1
Engine = engine + size
Cambridge IGCSE May/June 2017 paper 2 task 1
Module Module1
Dim coachprice As Integer = 550
Dim parkentry As Integer = 30
Dim freetickets, costoffreetickets, studentlimit As Integer
Dim studenttotalcost, numberofstudents As Integer
Dim everyonecost As Double
Sub Main()
[Link]("please enter the estimated number of students to take part")
numberofstudents = [Link]()
While numberofstudents > 45
[Link]("the number of students is either above or less than limit
required of 45")
End While
freetickets = numberofstudents / 10
costoffreetickets = freetickets * parkentry
everyonecost = coachprice + parkentry - costoffreetickets
studenttotalcost = 1 / numberofstudents * everyonecost
[Link]("the number of students going is" & numberofstudents)
[Link]("the free tickets we have are" & freetickets)
[Link]("the cost of free tickets is" & costoffreetickets)
[Link]("the total cost for everyone is" & everyonecost)
[Link]("for one student the cost is" & studenttotalcost)
[Link]()
End Sub
End Module
Task 2
Module Module1
Const coachprice As Integer = 550
Const parkentry As Integer = 30
Dim freetickets, costoffreetickets, studentlimit As Integer
Dim studenttotalcost, numberofstudents As Integer
Dim everyonecost As Double
Dim studentnames(45) As String
Dim studentpaid(45) As Boolean
Dim paidcount As Integer
Dim pay As Char
Sub Main()
paidcount = 0
[Link]("please enter the estimated number of students to
take part")
numberofstudents = [Link]()
While numberofstudents < 0 Or numberofstudents > 45
[Link]("the number of students is either above or
less than limit required of 1 to 45, please re-enter")
numberofstudents = [Link]()
End While
For count1 = 0 To 44
studentpaid(count1) = False
Next
For count2 = 0 To 44
[Link]("please enter student name")
studentnames(count2) = [Link]()
[Link]("is the student paying")
pay = [Link]()
If pay = "y" Then
studentpaid(count2) = True
paidcount = paidcount + 1
End If
Next
freetickets = Int(paidcount / 10)
costoffreetickets = freetickets * parkentry
everyonecost = (paidcount * parkentry) - costoffreetickets +
coachprice
studenttotalcost = everyonecost / numberofstudents
[Link]("the number of students going is" &
numberofstudents)
[Link]("the free tickets we have are" & freetickets)
[Link]("the cost of free tickets is" &
costoffreetickets)
[Link]("the total cost for everyone is" & everyonecost)
[Link]("for one student the cost is" & studenttotalcost)
[Link]()
End Sub
End Module