Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Control Structures - JavaScript(JS)

Chapter: Internet & World Wide Web HOW TO PROGRAM - The Ajax Client - JavaScript: Control Statements I

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Control Structures - JavaScript(JS)

Normally, statements in a program execute one after the other in the order in which they are written.

Control Structures

 

Normally, statements in a program execute one after the other in the order in which they are written. This process is called sequential execution. Various JavaScript statements we will soon discuss enable the programmer to specify that the next statement to execute may not be the next one in sequence. This is known as transfer of control.

 

During the 1960s, it became clear that the indiscriminate use of transfers of control was the root of much difficulty experienced by software development groups. The finger of blame was pointed at the goto statement, which allowed the programmer to specify a transfer of control to one of a wide range of possible destinations in a program. The notion of so-called structured programming became almost synonymous with goto elimina-tion.” JavaScript does not have a goto statement.

 

The research of Bohm and Jacopini demonstrated that programs could be written without goto statements.1 The challenge of the era for programmers was to shift their

styles to “goto-less programming.” It was not until the 1970s that programmers started taking structured programming seriously. The results were impressive, as software devel-opment groups reported reduced development times, more frequent on-time delivery of systems and more frequent within-budget completion of software projects. The key to these successes is that structured programs are clearer, easier to debug and modify and more likely to be bug free in the first place.

 

Bohm and Jacopini’s work demonstrated that all programs could be written in terms of only three control structures, namely the sequence structure, the selection structure and the repetition structure. The sequence structure is built into JavaScript. Unless directed otherwise, the computer executes JavaScript statements one after the other in the order in which they are written (i.e., in sequence). The flowchart segment of Fig. 7.1 illus-trates a typical sequence structure in which two calculations are performed in order.

 

A flowchart is a graphical representation of an algorithm or of a portion of an algo-rithm. Flowcharts are drawn using certain special-purpose symbols, such as rectangles, dia-monds, ovals and small circles; these symbols are connected by arrows called flowlines, which indicate the order in which the actions of the algorithm execute.

 

Like pseudocode, flowcharts often are useful for developing and representing algo-rithms, although pseudocode is strongly preferred by many programmers. Flowcharts show clearly how control structures operate; that is all we use them for in this text. Care-fully compare the pseudocode and flowchart representations of each control structure.

 

Consider the flowchart segment for the sequence structure on the left side of Fig. 7.1. We use the rectangle symbol (or action symbol) to indicate any type of action, including a calculation or an input/output operation. The flowlines in the figure indicate the order in which the actions are performed—the first action adds grade to total, then the second action adds 1 to counter. JavaScript allows us to have as many actions as we want in a sequence structure. Anywhere a single action may be placed, as we will soon see, we may place several actions in sequence.

 

In a flowchart that represents a complete algorithm, an oval symbol containing the word “Begin” is the first symbol used; an oval symbol containing the word “End” indicates where the algorithm ends. In a flowchart that shows only a portion of an algorithm, as in Fig. 7.1, the oval symbols are omitted in favor of using small circle symbols, also called connector symbols.

 

Perhaps the most important flowcharting symbol is the diamond symbol, also called the decision symbol, which indicates that a decision is to be made. We discuss the dia-mond symbol in the next section.

 

JavaScript provides three types of selection structures; we discuss each in this chapter and in Chapter 8. The if selection statement performs (selects) an action if a condition is true or skips the action if the condition is false. The ifelse selection statement per


forms an action if a condition is true and performs a different action if the condition is false. The switch selection statement (Chapter 8) performs one of many different actions, depending on the value of an expression.

 

The if statement is called a single-selection structure because it selects or ignores a single action (or, as we will soon see, a single group of actions). The ifelse statement is a double-selection structure because it selects between two different actions (or groups of actions). The switch statement is a multiple-selection structure because it selects among many different actions (or groups of actions).

 

JavaScript provides four repetition structure types, namely while, dowhile, for and forin. (dowhile and for are covered in Chapter 8; forin is covered in Chapter 10.) Each of the words if, else, switch, while, do, for and in is a JavaScript

 

keyword. These words are reserved by the language to implement various features, such as JavaScript’s control structures. Keywords cannot be used as identifiers (e.g., for variable names). A complete list of JavaScript keywords is shown in Fig. 7.2.


As we have shown, JavaScript has only eight control structures: sequence, three types of selection and four types of repetition. A program is formed by combining control struc-tures as necessary to implement the program’s algorithm. As with the sequence structure in Fig. 7.1, we will see that each control structure is flowcharted with two small circle sym-bols, one at the entry point to the control structure and one at the exit point.


 

JavaScript keywords

 

break  case    catch  continue        default

delete do        else     false   finally

for       function         if          in         instanceof

new    null     return switch this

throw  true     try        typeof var

void     while  with                

 

Keywords that are reserved but not used by JavaScript

 

abstract          boolean         byte     char    class

const  debugger       double            enum  export

extends          final    float    goto    implements

import int        interface        long    native

package         private            protected        public short

static   super  synchronized            throws            transient

volatile                                              

 

 

Single-entry/single-exit control structures make it easy to build programs; the con-trol structures are attached to one another by connecting the exit point of one to the entry point of the next. This process is similar to the way in which a child stacks building blocks, so we call it control-structure stacking. We will learn that there is only one other way in which control structures may be connected—control-structure nesting. Thus, algorithms in JavaScript programs are constructed from only eight different types of control structures combined in only two ways.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.