A computer is good at doing one thing and one thing onlyexecuting a series of instructions. From the computer's point of view, it is simply handed a sequence of instructions to execute, and it does so accurately and quickly. The instructions executed by the computer are spelled out using a programming language, such as Visual Basic. For example, if you want the computer to create an integer variable, assign the value 4 to it, and then multiply the variable's value by 2, you could use the following code: 'Create a variable of type Integer and assign it the value 4 Dim someVariable as Integer = 4 'Multiply the value of someVariable by 2 and assign the product 'back to the variable someVariable (this is equivalent to using ' someVariable = someVariable * 2) someVariable *= 2 When this program is executed, the first line of code is executed (the Dim statement), which declares a variable of type Integer and assigns the value 4 to this variable. Next, the code someVariable *= 2 is executed, which multiplies the value of someVariable by 2 and then assigns the result (8) to someVariable.
What if we don't necessarily want to have someVariable assigned twice its value? Perhaps we want to execute the someVariable *= 2 line of code only if someVariable is less than 5. Or perhaps we want to continue to double the value of someVariable until the value is greater than 100. To alter the flow of instructions, we use control structures. The three primary classes of control structures are
In this hour we will examine the syntax and semantics of all three classes of control structures. |