ThePlace

Home ] Search ] Resources ] Site Map ] Contact Me ]
Dave's Information Technology Resource

Up ]

Computers & Programming ] Operating Systems ] Programming Environments ] Windows-Based Applications ] Objects and Programming ] Data and Variables ] Operational Expressions ] Functions and Procedures ] [ Programming Control ] Algorithms ] Files, Graphics & Databases ] Software Development ] 10 Things... ]

--- Programming Control ---

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-else

This 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
    action(s)
Elseif condition then
   action(s)
Else
  action(s)
End if

The conditional clause must evaluate to True or False.

The conditional clause is based upon boolean logic...

The AND truth table...

inputs T F
T T F
F F F

The OR truth table...

inputs T F
T T T
F T F

 

The conditions can be quite complex...

Consider variables a, b, c, and d with values of...

bullet

a is true

bullet

b is -2

bullet

c = "dog"

bullet

d = false

if ((a = false) or (b > 3)) and ((c = "cat") or (d = false))

interprets to...
if  (F or F) and (F or T)
if F and T
-->will evaluate to False
Note: groupings of clauses

on the other hand...

if ((a = false) or (b > 3) and (c = "cat")) or (d = false))

interprets to...
if  (F or F and F) or T 
if F or T
--> will evaluate to True
Note: evaluation is in sequence

 

Remember, at a minimum, you need at least one "If condition then action" for a valid if-then statement.

Example:

If A > B then
  Do_this
Else
  Do_this_instead
End if

 

Select-Case

Similar to an if-then, allows you to evaluate a series of conditions (cases) and take action.

Syntax:

Select variable

   Case variable-value

     action(s)

   Case variable-value

     action(s)

   Case variable-value

     action(s)

   ...

End Select

 

 

 

For-next

Cycles 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-to

Often 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

 

 

Home ] Up ] Computer Architecture ] Database Bootcamp ] Visual BasicS ] Web Basics ] Web Multimedia ] Web Programming ] Advanced Web Topics ] Developing Web Sites ] XML Technology ] Web Glossary ]

Copyright © 1999 - 2005 
ThePlace - Written and Sponsored by Dave Hillman