HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
CONTROLLING THE FLOW - JAVASCRIPT CONTROL STATEMENTS

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

To make something happen, you use a JavaScript statement, which is akin to a complete sentence or command. A JavaScript program is actually a collection of statements.


TUTORIAL TAKEN FROM COURSE : JAVASCRIPT PROGRAMMING

FULL COURSE DETAILS

This training course aims to teach the reader the fundamentals of JavaScript. This course covers topics such as - variables and operators, control statements, functions, objects, frames, forms, dates, math and an introduction to cross-browser compatibility.

TO ACCESS THE FULL COURSE AND HUNDREDS OF OTHERS, CLICK HERE.


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?



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...
}


NEXT PAGE



5 RELATED COURSES AVAILABLE
JAVASCRIPT PROGRAMMING
This training course aims to teach the reader the fundamentals of JavaScript. This course covers topics such as -....
HTML 4.0 INTRODUCTION
To create, format and publish a small website using HTML 4.0. You will learn to create web pages incorporating fo....
INTRODUCTION TO JAVA PROGRAMMING
The aims of this Java training courses is to understand the role that Java plays on the Internet; describe the be....
MICROSOFT VISUAL BASIC V6 INTRODUCTION
To go from the fundamentals of Visual Basic programming to the threshold of Advanced level. Gaining in depth prog....
MICROSOFT VISUAL BASIC 5.0 CLIENT SERVER DEVELOPMENT
This course teaches the skills required to develop client server applications using MS Visual Basic 5.0 Enterpris....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Saturday 22nd November 2008  © COPYRIGHT 2008 - VISUALSOFT