In this example, the program does the following:
- Three integer variables called Num(0), Num(1) and Num(2) are created (step 1).
- The program asks and then prompts the user to enter a number (steps 2 to 4).
- It then asks and then prompts the user to enter a second number (steps 5 to 6).
- The sum of these two numbers is calculated (step 7).
- The result is displayed on screen (step 8)
- When the user inputs the enter key, the program terminates (step 9).
Selection / Decision Flow
Rather than being processed consecutively, selection allows the program to deviate and provide different routes of execution. The route that is taken depends on if specified criteria have been met. (Anderson, 2010) In Visual Basic, this is done using the if keyword. The following diagram illustrates this process as a flowchart:
(Chapman, 2011)
What is happening here is that if a variable equals a specified value as described by the if keyword, it will commence executing the commands in the first subroutine. If it hasn’t, it will execute the commands proceeded by else in the second subroutine. In order to show how this flow works in practicality, the following is a program I have written in Visual Basic to help illustrate this:
As you can see, it is the same as my previous example, except several lines of code have been added post script. These new lines I have highlighted in yellow, and they do the following:
-
A if statement is initiated. The value of Num(2) is checked to see if it is 1,000 or higher (step 9).
-
If this is to the affirmative, the program is instructed to follow the then clause, which starts at the end of step 9 and continues in step 10. In this case, it prints “That’s a big number!” on the screen.
-
If this is not to the affirmative, it is instructed to follow the else clause, which starts at step 11 and continues onto step 12. In this case it prints, “That is not a big number…” on the screen.
-
Finally, the end if stated in step 13 encapsulates and signals the end of this process, and the program is allowed to continue processing the instructions as a sequence in step 14.
(The VB Programmer LLC, 2006)
Iteration / Repetition Flow
Instructions are repeated in a loop. The loop can be either a fixed repeat loop (where the loop repeats a specified number of times) or a conditional loop (where the loop will continue until a certain condition has been met). The syntax for implementing these two scenarios differs in Visual Basic, but the basic idea of repeating code until an objective has been completed is the same. The following diagram illustrates this process as a flowchart:
(The VB Programmer LLC, 2006)
This diagram is simplified to be able to account for both kinds of loops, but the general idea is the same. The statement asks if a condition has been met, and if it has, it continues as normal. If the condition has not been met, the program flow becomes stuck in the loop until it meets the specified condition or has completed the specified number of loops.
How these two types differ, again, is through use of different syntax. A repeat loop uses for to begin a loop and specifies how many times it is to be repeated by specifying a start value and an end value to a variable. When the loop reaches next, this is what instructs it to continue the loop again, incrementing the variable up one. This continues until the end value for the variable has been reached. (Anderson, 2010) This process is shown in this example of code:
A conditional loop uses do. This loop can be used in one of two ways, as either a do while or loop until loop. These fundamentally work the same way, except the program will check if the condition has been met at the start of a do until loop, and at the end of a do while loop. The follow table illustrates how this can be used both ways with one example piece of code:
Finally, an annotated example of a loop in a full program is as follows:
(Anderson, 2010)
In this example, the pre-check loop has been highlighted in yellow and does the following:
-
A loop is opened in step 7 with do until, and the criteria for ending the loop is specified (the value entered into variable numbers by the user is “X”)
- The value of total is overwritten by the sum of numbers and the existing value of total in step 8
- The value of counter is overwritten by the sum of 1 and the existing value of counter in step 9
- The program prompts the user in step 10 to enter a number, or to enter X to begin calculating the average of all numbers enters by the user
- The user’s input is recorded into the numbers variable in step 11
- Finally, this loop statement in step 12 instructs the program that the end of the loop has been reached, and to go back to the beginning of the loop at step 7
Summery
In this report, I presented how the control structures sequence, selection and iteration are used in computer programming. We learned that the sequence structure processes instructions in a linear fashion to its conclusion. In regards to selection, we learned that it allows us to create two or more different paths for a program to follow by setting criteria. Finally, we learned that loops allow sections of code to be run either a predetermined or unset number of times, depending on the aim of the loop.
Bibliography
Anderson, J. K. (2010). BTEC National Level 3 IT Student Book 1. Edexcel.
Chapman, S. (2011). Structured Programming. Retrieved October 7, 2011, from About.com: http://javascript.about.com/od/reference/a/struc.htm
The VB Programmer LLC. (2006). VB Tutorials - Classic VB (VB6). Retrieved October 7, 2011, from The VB Programmer LLC: http://www.thevbprogrammer.com/classic_vbtutorials.asp