Repetition Structures
Repetition Structures
Repetition structure are the looping codes or methods used to repeat the execution of the of the same code
or process until a certain condition is met.
Repeat …. Until
While ….. End While
For …… Next
These repetition structures function in a similar way but are implemented in unique scenarios.
Post-Conditioned loop
Pre-conditioned loop
Count controlled loop
Post-conditioned loop
Pre-Conditioned loop
The code in the loop is executed for the number of times stated at the beginning of the loop
Note *
There is a difference in all the types of loops but the trickiest one to remember is the difference
between Post-conditioned and Pre-conditioned loop despite the idea on when it’s tested.
The loop behaves differently.
What is looping in Programming?
Looping is a process of repeating the same steps followed to solve a task for certain number of
times.
Example
Think of counting from 0 to 9.
Well, the idea is that to go to the next number you add the value 1 and continue adding 1 until you reach
9.
1. number = 0
2. Say the number
3. Add 1 to the number
4. If the number is less than or equal to 9 repeat from step 2
So the steps above will enable you to count from 1 to 9 and saying out all the numbers between 0 to 9
with 9 and 0 inclusive.
The purpose of repetition structures is to prevent writing the same steps over and over again.
Note: The variables declared inside a loop only exist inside that loop
Basing the looping structures on their methods of application
Repeat…..Until
Example
1. x=0
2. x=x+1
3. answer = x * 3
4. output x, answer
5. if x < 4 then repeat from 2
OR
1. x=0
2. Repeat
3. x=x+1
4. answer = x * 3
5. output x, answer
6. Until x = 4
Application of loops in programming
1. Steps controlled loop or line controlled loop.
Note * This is a VB.Net Recent development method of loops
It uses line numbers to indicate a point to be executed and the Goto statement to indicate
which line to go to and start executing from.
It also allows programmers to skip codes not desired to be executed.
Qn
Write a program that counts from 1 to 10. The program should display all the values counted in the
sequence of the counting. Use the console interface. [5]
Sub Main()
1: num = 1
3: num = num + 1
5: Console.ReadLine()
End Sub
Qn
Write a program that accepts two integer numbers and finds the product of the two numbers using
repetitive addition. Use the console interface. [10]
Imports System.Console
Module Module1
Sub Main()
2: Product = Product + a
3: num = num + 1
End Sub
End Module
2. For….Next Loop
Note*
This is a count controlled loop
The variable in the loop initialization increments in terms of its values
The loop counts onwards and backwards
The loop can skip other numbers
For variable = (starting point value) to (Stopping point value) Step (values to be moved)
Steps (code) to be repeated
Next
Example
For a = 0 To 20 Step 1
Print a
Next
The value in the variable “a” will increase by a value 1, therefore data in „a‟ changes.
0 is the starting point and 20 is the stopping point
Note that the variable should be an integer.
To prove it, try out the following code in VB.net console application
For a = 0 To 15
Next
ReadLine()
The for…next loop is usually used in traversing data structures, analysis and developing a
sequence of executing code
Qn
Write a program that counts from 1 to 10. The program should display all the values counted in the
sequence of the counting. Use the console interface. [5]
Imports System.Console
Module Module1
Sub Main()
For a = 1 To 10 Step 1
Write(a & " ")
Next
ReadLine()
End Sub
End Module
Qn
Write a program that accepts two integer numbers and finds the product of the two numbers using
repetitive addition. Use the console interface. [10]
Imports System.Console
Module Module1
Sub Main()
For i = 1 To b Step 1
Product = Product + a
Next
End Sub
End Module
3. While…..End While Loop
This loop tests a condition before executing the code in the loop.
If the condition if the condition is found to be True the loop will execute the code in the loop
until the condition tested becomes False.
1. Do While (condition)
Steps (Code) to be executed
Loop
2. While (condition)
Steps (Code) to be executed
End While
So as the loop says it self it executes while a certain condition is true. It only repeats the potion in the
loop over and over and over again until the condition is met.
Example
a=0
While a < 10
Print a
a += 1
End While
The algorithm above will repeat running while the value of „a‟ is less than 10.
Dim a As Integer
While a < 10
Console.Write(a & " ")
a += 1
End While
Console.ReadLine()
Precautions
Check the condition if it will be met in the loop to prevent endless looping
Check if the condition will be met before the loop runs
Qn
Write a program that will display all the even numbers between 1 and 30 with 30 inclusive. Use the
console interface. [8]
Imports System.Console
Module Module1
Sub Main()
Dim a As Integer = 1
While a <= 30
a += 1
End While
ReadLine()
End Sub
End Module
Qn
Write a program that looks for two numbers from the following list that gives a product of 18 and a sum
of 9 and display the numbers. {2, 4, 3, 12, 5, 6, 18, 17}. This sequence should be store in a data
structure called numbers. [20]
While (sum <> 9) And (product <> 18) And a < numbers.Length
Post-Tested loop
The code in the loop is run for the first time and then the condition is tested if the whole
code in the loop is run. If the loop finds the condition False it then repeats the loop until
the condition becomes True
If the condition at the bottom of the loop is found to be True it then exit the loop.
Note in Vb.Net
Repeat Do
End Repeat Loop
Do Until (condition)
Loop
Post-conditioned
Do
Try out the following code in console. It will count to 10 and print the sequence. The loop will repeat
until the value in „a‟ is greater than 10
Dim a As Integer
a = 0
Do
Write(a & " ")
a += 1
Write a program that accept a number of seconds and converts them hours, minutes and seconds
and display them as follows. 12 hrs 12 min 12 sec [15]
End Sub
End Module
Qn
Write a program that accepts an integer value and the program should display the factorial value of the
number. [10]
Imports System.Console
Module Module1
Sub Main()
End Sub
End Module
Note*
Steps controlled, while…End While and Repeat…Until are the loops that can go into endless
looping if the conditions are never met when the loop is being executed.
Practice Questions
1. Write a program that reads in a series of numbers and adds them up until the user enters
zero. (This stopping value is often called a rogue value.) You may assume that at least 1
number is entered. Expand your program to display the average as well as the sum of the
numbers entered. Make sure you do not count the rogue value as an entry.
2. Write a program that asks the user for a number between 10 and 20 inclusive and will
validate the input. It should repeatedly ask the user for this number until the input is
within the valid range. Make changes to your program so it will also work if the user does
not want to type in any numbers and uses the rogue value straight away.
3. Write a program that displays a conversion table for pounds to kilograms, ranging from 1
pound to 20 pounds [1 kg = 2.2 pounds].
4. Write a program that asks the user to enter 8 integers and displays the largest integer.
i. Adapt your program so that the user can type in any number of positive integers.
Input will terminate with the rogue value of—l.
ii. Adapt your program so that it will also display the smallest integer. Adapt your
program from if necessary, so that it works if the user types in the rogue value as
the first value
6. Write a program to display the squares of all the integers from 1 to 12 in two columns
headed 'Number' and 'Square of Number'.
7. Write a program that displays all the numbers between 1 and 10,000. How long did it
take to execute?
Fun Time
Write a program that asks for a number and converts this into a binary
number. You will need to use \ and mod.
Write a program that shows the first 10 numbers in the Fibonacci series.
A prime number is a number that can only be divided by the number itself
and 1. Write a program that displays all the prime numbers between 2 and
1000.
Write a game in which the user guesses what random number between 1 and
1000 the computer has 'thought of, until he or she has found the correct
number. The computer should tell the user whether each guess was too high,
too low or spot on. (TIP: use the Maths library, which has a random
function)