This Resource Was Taken from Visual Basic for AVCE, Derek Christopher
Total Page:16
File Type:pdf, Size:1020Kb
Resources available S:\Higher Education\Unit Resources\HN Computing\Programming Concepts
Task 4.1 –Loops (This resource was taken from Visual Basic for AVCE, Derek Christopher)
What is a loop? A program loop is a section of code that may be repeatedly executed. The loop contains a Boolean condition that will determine when it terminates.
What is a Boolean? A Boolean is a data type; a Boolean value can only be true or false.
Types of loop in Visual Basic Visual Basic has six types of loop, but you only ever need to understand three of them. These are: • For…Next • Do While…Loop • Do…Loop Until
The code, known as the loop body, is inserted in place of the three dots. Each loop works in a slightly different way and is useful in different circumstances.
What we will be during this task We will be experimenting with the DO While…Loop and DO…Loop Until. Towards the end of the unit we will work with the For…Next.
Do While…Loop The general form of Do While…Loop is Do While condition is true statement(s) = body of loop Loop
This type of loop executes as long as the Boolean condition in the first line of the loop is true, otherwise it exits.
Page 1 of 4 Brendan Coulson HNC Programming Resources available S:\Higher Education\Unit Resources\HN Computing\Programming Concepts
Task 1
1. Create a new form in your MDI, name the form FrmLoop
2. Place a command button on the form, call it btnDoWhile, and give the button the caption of “Do While…Loop”.
3. Enter the following code in the event of the command button you have just created
Dim intNumber As Integer 'stores a number intNumber = 5
Do While intNumber < 10 ' loop while less than 10 Msgbox (intNumber * intNumber) ‘Displays the value to the form intNumber = intNumber + 1 Loop
Write down your findings: (notice what happens if you change intNumber to 10)
Page 2 of 4 Brendan Coulson HNC Programming Resources available S:\Higher Education\Unit Resources\HN Computing\Programming Concepts
If the first line of code was Number = 10 then the condition 10 < 10 would be false, and the loop would not be executed at all. This is one key feature of Do While…Loop which distinguishes it from Do… Loop Until.
Note: (Approach this carefully – save your work)
If you omitted the line Number = Number + 1 the loop would never end, since Number would never equal 10. This is called an infinite loop. This is a common mistake and a good reason to always save your program before running it, because you will not be able to get out of an infinite loop without exiting Visual Basic and thus losing your code.
Do…Loop Until The general form of this loop is: Do statement(s) = body of loop Loop Until condition is true Since the condition is at the end of the loop, the loop body must be executed at least once.
Page 3 of 4 Brendan Coulson HNC Programming Resources available S:\Higher Education\Unit Resources\HN Computing\Programming Concepts
Task 2
1. Place another command button on the form, call it btnDoLoop and give the button the caption of “Do…Loop Until”.
2. Enter the following code in the event of the command button you have just created
Dim intNumber As Integer 'stores a number intNumber = 5
Do msgbox (intNumber * intNumber) 'Displays the value to the form intNumber = intNumber + 1
Loop Until (intNumber > 10) '> means greater than
Write down your findings: (notice what happens if you change intNumber to 11)
Page 4 of 4 Brendan Coulson HNC Programming