Consider a program that solves the equation:
ax2 + bx + c = 0
for any values of a, b, and c. We remember this problem from high school algebra: the solution is given by the quadratic formula,
However, this equation also depends upon the coefficient a being non-zero. If a is zero, an attempt to use this formula will fail with an attempt to divide by zero. In the case where a is zero, the equation becomes a linear equation, bx + c = 0, and its solution is
However, this formula also fails if, in addition to a being zero, b is zero. If both a and b are zero, the equation becomes c = 0. If in addition to a and b being zero, c is zero, the equation is satisfied by any x; just for symmetry, we can claim x = 0 as “the” solution. On the other hand, if a and b are zero, but c is non-zero, then the equation has no solution. The overall flowchart for the program is then as follows:
There are four computations that can be performed by this program, including one, printing “no solution,” which is not a computation in the sense of yielding a result. The computation performed depends upon the values of the coefficients a, b, and c. As these are zero or non-zero, the flow chart chooses alternative computations to perform.
This type of construct, the alternative, is one of the three basic types of control flow needed to produce arbitrary programs. In general, the overall structure of an alternative is:
That is, there is a single entry into the construct, and the construct does one of two things depending upon a single computable condition, and the two alternative join together into a single exit. Either of the alternatives may be empty, although it is traditional (and in some languages mandatory) to use the “true” alternative if only one alternative is to be used.
It is possible to put completely unrelated processes into the various branches of an alternative construct. However, such practices are confusing and lead to increased maintenance costs. Generally, alternative constructs have a single entry point (the beginning of the construct) and a single exit point (the end of the construct), and a single statement should be true of the computations performed in the alternatives. This means that a single process block — such as “compute the whatsis” — can replace the alternative construct at a higher level of understanding. Alternately, a single process block can, if necessary, be expanded into an alternative.
For example, in an exercise to appear later in this section, it is necessary to find the smallest positive root of a quadratic equation, where at least one root is guaranteed to be positive. This is done with the following flow chart segment:
In the first process block, the computation of r gives the common part of the two roots. In the second process block, t1 and t2 are computed as the two roots of the quadratic equation. After the first alternative, tmin is the smaller of t1 and t2, and tmax is the larger of t1 and t2. After the second alternative, t is the smaller positive value of t1 and t2. There are simple descriptions which summarize the effects of the entire alternative construct.
In the calculator, the syntax for an alternative is one of the forms:
conditionstatement
or
If condition Then alternative IfEnd
or
If condition Then alternative Else alternative IfEnd
where condition is an expression, statement is a single calculator statement, and each alternative is a sequence of calculator statements. Note that several statements can be in each alternative. The start of each alternative is the Then or Else keyword; the end of the Then alternative is either the Else keyword of the IfEnd keyword; and the end of the Else alternative is the IfEnd keyword. In alternative constructs using the If keyword, the IfEnd keyword must be specified so the calculator knows where the alternative construct ends.
In Visual Basic for Applications, the syntax for an alternative is one of the forms:
If condition Then statement
or
If condition Then
Alternative
End If
or
If condition Then
Alternative
Else
Alternative
End If
where condition is an expression, statement is a single statement, and each alternative is a series of statements. Note that, as with the calculator, several statements can be in each alternative, and so the End If must be used to indicate the end of the alternative construct.
In JavaScript, the syntax for an alternative is one of the forms
condition ? expression1 : expression2
or
if (condition)
statement
or
if (condition)
statement
else
statement
where condition is an expression controlling the selection of alternatives, expression is an expression giving a value to the conditional expression, and statement is a single statement.
There is a substantial difference between the first form (the one with a question mark) and the other forms (the ones with the if keyword). The first form is an expression — if condition is non-zero, expression1 is evaluated and is the result of the expression; if condition is zero, expression2 is evaluated and is the result of the expression. The first form can be used anywhere an expression can occur. The other two forms are statements, and can be used only where statements can occur.
In the statement forms, as in the expression forms, condition is compared to zero. If condition is non-zero, the statement following the condition is executed. If condition is zero, the statement (if any) following the else keyword is executed. Note that only a single statement can be in each alternative. However, in JavaScript, a block is a type of statement: and a block can contain many other statements. Typically, programmers will write JavaScript alternatives as
if (condition)
{
alternative
}
where alternative is a series of statements. However, see also the section on Coding Standards for disputes regarding the placement of the braces.
One odd, but usually unimportant, JavaScript fact is that the if statement does not require a semicolon. The reason for this is that the if statement ends in another, included, statement; and this other statement either is a block (which does not require a semicolon) or has its own semicolon.
[ Previous page | Top of page | Next page ]
Copyright © 2001 Brian Hetrick
Page last updated 30 December 2001.
Tutorial
Building Blocks I
Control Flow I
Alternatives
Control Flow II
Basic I/O
Algorithms
A First Program
Answers
Modularization
Data Structures I
Recursion
Program Attributes
Building Blocks II
Algorithm Analysis
Structuring
Data Structures II
Abstract Types
Objects
Problem Analysis
Reference Card
![]()