0% found this document useful (0 votes)
77 views13 pages

Repetition Structures

Repetition structures

Uploaded by

flique1233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views13 pages

Repetition Structures

Repetition structures

Uploaded by

flique1233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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.

There three types of repletion structures which are:

 Repeat …. Until
 While ….. End While
 For …… Next

These repetition structures function in a similar way but are implemented in unique scenarios.

These repletion structures are categorized into the following ideas.

 Post-Conditioned loop
 Pre-conditioned loop
 Count controlled loop

Post-conditioned loop

 The condition is tested after the code to be repeated executes once


 The code in the loop will start to repeat if the condition is False until it becomes True

Pre-Conditioned loop

 The condition is tested before the code in the loop is executed


 The code in the loop will start to execute and repeat if the condition is True until it becomes
False
 But if the condition is False the Code in the loop will not be executed at all

Count controlled 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.

What do you really do?

Well, the idea is that to go to the next number you add the value 1 and continue adding 1 until you reach
9.

So the steps you will be repeating are:

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.

This same idea is what is applied in programming.

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

Count controlled loop Pre-Condition(-Tested) Post-conditioned loop


loop (Bottom tested)
For……Next While……End while Repeat …. until

Repeat…..Until

Looping is applied in algorithms as well.

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]

Solution  Code and expected results

Sub Main()

Dim num As Integer

1: num = 1

2: Console.Write(num & " ")

3: num = num + 1

4: If num <= 10 Then GoTo 2

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]

Solution  code and expected results

Imports System.Console

Module Module1

Sub Main()

Dim num, Product As Integer


Dim a, b As Integer

1: WriteLine("Enter the first number: ")


a = ReadLine()

WriteLine("Enter the second number: ")


b = ReadLine()

2: Product = Product + a

3: num = num + 1

4: If Not (num = b) Then GoTo 2

5: WriteLine("Product = {0}", Product)


ReadLine()

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

Code Structure of the looping Structure

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

Write(a & " ")

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]

Solution  Code and expected results

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()

Dim Product As Integer


Dim a, b As Integer

WriteLine("Enter the first number: ")


a = ReadLine()

WriteLine("Enter the second number: ")


b = ReadLine()

For i = 1 To b Step 1
Product = Product + a
Next

WriteLine("Product = {0}", Product)


ReadLine()

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.

Structures of the while Statement in Vb.Net

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.

Try the following code in console to check.

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]

Solution  code and expected results

Imports System.Console

Module Module1
Sub Main()

Dim a As Integer = 1
While a <= 30

If (a Mod 2) = 0 Then Write(a & " ")

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]

Solution  code and expected results


Dim numbers() As Integer = {2, 4, 3, 12, 5, 6, 18, 17}
Dim a, b As Integer
Dim value1, value2, product, sum As Decimal

While (sum <> 9) And (product <> 18) And a < numbers.Length

While Not ((sum = 9) And (product = 18)) And b < numbers.Length


value1 = numbers(a)
value2 = numbers(b)
product = value1 * value2
sum = value1 + value2
b += 1
End While
b = 0
a += 1
End While

WriteLine("The values are {0} and {1}", value1, value2)


ReadLine()
4. Repeat…..until loop
 Pre-Tested loop
 It test the condition first then the loop repeats when the condition False until it becomes
True
 It will not execute the loop if the condition is True

 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

The basic Structure of Repeat…Until


Pre-conditioned

Do Until (condition)

Steps (Code) to be executed

Loop

Post-conditioned
Do

Steps (Code) to be executed

Loop Until (condition)

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

Loop Until a > 10


ReadLine()
Qn

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]

Solution  code and expected results


Imports System.Console
Module Module1
Sub Main()

Dim hrs, min, sec As Integer

WriteLine("Please enter the number of seconds: ")


sec = ReadLine()
Do Until sec < 60
min += 1
sec -= 60
Loop
Do Until min < 60
min -= 60
hrs += 1
Loop
WriteLine("{0} hrs {1} min {2} sec", hrs, min, sec)
ReadLine()

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]

Solution  code and expected results

Imports System.Console
Module Module1
Sub Main()

Dim num, factoral, a As Integer


a = 0
factoral = 1
WriteLine("Enter the Integer value: ")
num = ReadLine()
If num > 0 Then
Do
a += 1
factoral = factoral * a
Loop Until a = num
End If
WriteLine("Factorial of {0} = {1}", num, factoral)
ReadLine()

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

5. Write a console application to calculate the 8 times table

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)

Madanaime: Contact: 0771936890 / 0718830036


Email: [email protected]
Email: [email protected]

You might also like