JavaScript: Control Structures 1
Any computing problem can be solved by executing a series of actions in a specific order. A procedure for
solving a problem in terms of
1. The actions to be executed and
2. The order in which the actions are to be executed is called an algorithm. The following example
demonstrates that correctly specifying the order in which the actions are to execute is important. Consider
the “rise-and-shine algorithm” followed by one junior executive for getting out of bed and going to work:
(1) Get out of bed, (2) take off pajamas, (3) take a shower,
(4) get dressed, (5) eat breakfast, (6) carpool to work. This routine gets the executive to work well
prepared to make critical decisions. Suppose, however, that the same steps are performed in a slightly
different order: (1) Get out of bed, (2) take off pajamas, (3) get
dressed, (4) take a shower, (5) eat breakfast, (6) carpool to work. In this case, our juniorexecutive shows
up for work soaking wet. Specifying the order in which statements are to be executed in a computer
program is called program control.
if Selection Structure
A selection structure is used to choose among alternative courses of action in a program. For example,
suppose that the passing grade on an examination is 60 (out of 100). Then the pseudo code statement
If student’s grade is greater than or equal to 60
Print “Passed”
if ( studentGrade >= 60 )
[Link]( "Passed" );
If/else Selection Structure
The if selection structure performs an indicated action only when the condition evaluates to true; otherwise, the
action is skipped. The if/else selection structure allows the programmer to specify that a different action is to be
performed when the condition is true than when the condition is false. For example, the pseudocode statement
If student’s grade is greater than or equal to 60
Print “Passed”
Else
Print “Failed”
if ( studentGrade >= 60 )
[Link]( "Passed" );
else
[Link]( "Failed" );
Nested if/else structures test for multiple cases by placing if/else structures inside if/else structures. For
example, the following pseudocode statement will print A for exam grades greater than or equal to 90, B for
grades in the range 80 to 89, C for grades in the range 70 to 79, D for grades in the range 60 to 69 and F for all
other grades:
If student’s grade is greater than or equal to 90
Print “A”
Else
If student’s grade is greater than or equal to 80
Print “B”
Else
If student’s grade is greater than or equal to 70
Print “C”
Else
If student’s grade is greater than or equal to 60
Print “D”
Else
Print “F”
This pseudocode may be written in JavaScript as
if ( studentGrade >= 90 )
[Link]( "A" );
else
if ( studentGrade >= 80 )
[Link]( "B" );
else
if ( studentGrade >= 70 )
[Link]( "C" );
else
if ( studentGrade >= 60 )
[Link]( "D" );
else
[Link]( "F" );
while Repetition Structure
A repetition structure allows the programmer to specify that a script should repeat an action
while some condition remains true. The pseudocode statement
While there are more items on my shopping list
Purchase next item and cross it off my list describes the repetition that occurs during a shopping trip. The
condition “there are more items on my shopping list” may be true or false. If it is true, then the action “Purchase
next item and cross it off my list” is performed. This action will be performed repeatedly while the condition
remains true. The statement(s) contained in the While repetition structure constitute the body of the While. The
body of the While structure may be a single statement or a compound statement. Eventually, the condition
becomes false (i.e., when the last item on the shopping list has been purchased and crossed off the list). At this
point, the repetition terminates, and the first pseudocode statement after the repetition structure executes.
Formulating Algorithms:
Case Study 1 (Counter-Controlled Repetition)
To illustrate how to develop algorithms, we solve several variations of a class-averaging
problem. Consider the following problem statement:
A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz
are available to you. Determine the class average on the quiz.
The class average is equal to the sum of the grades divided by the number of students (10
in this case). The algorithm for solving this problem on a computer must input each of the
grades, perform the averaging calculation and display the result. Let us use pseudocode to list the actions to
execute and specify the order in which the actions should execute. We use counter-controlled repetition to input
the grades one at a time. This technique uses a variable called a counter to control the number of times a set
of statements executes. In this example, repetition terminates when the counter exceeds 10. In this section, we
present a pseudocode algorithm (Figure 8.6) and the corresponding program
(Fig. 8.7). In the next section, we show how to develop pseudocode algorithms.
Counter-controlled repetition often is called definite repetition, because the number of repetitions
is known before the loop begins executing.