Understanding Control Structures


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.

By the Way

Note that the lines of code in this snippet executed exactly once, with the first line of code executing before the second. This style of code execution is known as sequential flow.


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

  • The conditional control structure, which executes a set of instructions only if some condition is met

  • Looping control structures, which repeatedly execute a set of instructions until some condition is met

  • Modularizing control structures, which group sets of instructions into modules that can be invoked at various places in the program

In this hour we will examine the syntax and semantics of all three classes of control structures.




Sams Teach Yourself ASP. NET 2.0 in 24 Hours, Complete Starter Kit
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
ISBN: 0672327384
EAN: 2147483647
Year: 2004
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net