| ||||||||
|
Program control statements are the heart of computer operations. Up to now, we have discussed how to store data, add and subtract data, and organize it in procedures. Program control statements such as if-then and for-next allow us to manipulate data. The following sections define some of the more common program control statements.
If-then-elseThis is the most commonly used statement in programming to control program execution. Syntax: If condition then action ---a single action ---following
is for multiple actions and else conditions If condition then The conditional clause must evaluate to True or False. The conditional clause is based upon boolean logic... The AND truth table...
The OR truth table...
The conditions can be quite complex... Consider variables a, b, c, and d with values of...
Remember, at a minimum, you need at least one "If condition then action" for a valid if-then statement. Example: If A > B then Select-CaseSimilar to an if-then, allows you to evaluate a series of conditions (cases) and take action. Syntax:
For-nextCycles through statements until a value is reached. Starting and end point values must known. Also an increment amount may be set (e.g., increment by one, by two, etc.). Syntax: For counter = startval to endval <step stepvalue> <actions> next
Counter, startval, endval, stepvalue may be variables or explicit values. Step is assumed as 1 if not given. Example: For I = 0 to 100 'repeat 101 times <actions> Next 'increment
Do-loop (While)Cycles through a loop until the necessary condition is met. Syntax: Do <while, until> <condition> <actions> Loop <while, until> <condition> In JavaScript, the syntax is a little different: Syntax: Do <actions> While <condition> Condition can be any logical expression. Example: I = 0 Do Until I = 1000 Action Action I = I + 1 Loop
Do-loops are often used to monitor an on-going process such as reading from data from a file when the file size is unknown. The do-loop continues until the end of the file is reached.
Go-toOften used in error checking to process unexpected events. Go-to usually jumps to another section of the function or procedure. Go-to expressions require a label in the code for a destination. Syntax: GoTo <label> …code <label:> …code following the label Example: Sub subname () On error GoTo errfound 'if error found, go to this label <action> <action> Exit sub 'leave if no error detected errfound: 'label for error processing <action> End sub
|
|
Copyright © 1999
- 2005 |