CONTROLLING THE FLOW – JAVASCRIPT CONTROL STATEMENTS (Page 1)

All statements are executed line-by-line, one after another, so the JavaScript code is quite static. However, there is a big difference between the JavaScript and HTML codes, since JavaScript has the conditional statement. It tells the browser to execute one or another part of the code depending on Boolean values.

How does it work?

unnamed-file-815 CONTROLLING THE FLOW - JAVASCRIPT CONTROL STATEMENTS
       (Page 1)

Using Control Statements and Loops

A loop is a set of commands that executes repeatedly until a specified condition is met. JavaScript supports the for, do while, while, and label loop statements (label is not itself a looping statement, but is frequently used with these statements). In addition, you can use the break and continue statements within loop statements.

for Statement

A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop. A for statement looks as follows:

for (initialExpression; condition; incrementExpression) {
statements
}

The for loop works as follows:

  • The initializing expression initialExpression, if there is any, is executed. This expression usually initializes one or more loop counters, but the syntax allows using an expression of any complexity.
  • The condition expression is evaluated. If the value of condition is true, the loop statements execute. If the value of condition is false, the for loop terminates.
  • The statements is executed.
  • When the update expression incrementExpression is executed, the control returns to Step 2

for (var i=0; i < 10; i++) {
// do something ...
}

do…while Statement

The do…while statement repeats until a specified condition evaluates to false. The do…while statement looks as follows:

do {
statement
} while (condition)

This statement is executed once before the condition is checked. If condition returns true, the statement executes again. At the end of every execution, the condition is checked. When the condition returns false, execution stops and control passes to the statement following do…while.

do {
i+=1;
result += 1;
} while (i<5);

while Statement

A while statement executes its statements as long as a specified condition evaluates to true. A while statement looks as follows:

while (condition) {
statements
}

If the condition becomes false, the statements within the loop stop executing and control passes to the statement following the loop.

The condition test occurs before the statements in the loop are executed. If the condition returns true, the statements are executed and the condition is tested again. If the condition returns false, execution stops and control is passed to the statement following while.

while( n < 3 ) {
n ++
}

Conditional Statements: If…Else

Conditional Statements

A conditional statement is a set of commands that is executed if a specified condition is true. JavaScript supports two conditional statements: if…else and switch.

if…else Statement

Use the if statement to perform certain statements if a logical condition is true; use the optional else clause to perform other statements if the condition is false. The if statement looks as follows:

if (condition) {
statements1
}
[else {
statements2
} ]

The condition can be any JavaScript expression that is evaluated as true or false. The statements to be executed can be any JavaScript statements. If you want to use more than one statement after the if or else statement, you must enclose the statements in curly braces, {}.

Var x=10
if(x > 5) {
// do something...
}