PGDEL/DECP03/Unit3/19

From WikiEducator
< PGDEL‎ | DECP03‎ | Unit3
Jump to: navigation, search



Unit 3.3 Web application development tools & Technologies
Conditional statement

Conditional Statement
Syntax
Example

If statement:

The condition checking statement in JavaScript is if-else construct, used to construct the flow of program.

if(condition){
//JavaScipt statements
}
var book =”JavaScript”
if ( book = = ”JavaScript”) {
document.writeln(“ JavaScript book.”);
}

Conditional Statement:
If evaluates either true or false depending on the evaluation of the condition.

If the condition evaluated to true then the result will be Value 1 otherwise Value2

(Condition) ? Value 1 : Value 2
var book =”JavaScript”
( book = = ”JavaScript”) ? “JavaScript book.” : “Other Book”

Loop Statement

Loop is the ability to repeat some block of code, for the purpose JavaScript has two types of loop statements:

Loop
Syntax
Example

For loop

This is used to repeat certain block of code (statements) in specified number of times.

for (expression 1; expression 2; expression 3){
// JavaScript statements
}

Expression 1 sets up the counter variable.
Expression 2 is the condition, the loop will execute till condition evaluates to true.
Expression 3 is to update the counter variable specified in expression 1.

for( var count =1; count<=10; count++){
document.writeln(count);
}
The following code prints numbers from 1 to 10.

While loop

This is used to repeat certain block of code (statements), which until a condition met.

while(expression){
// JavaScript statements
}

Expression is a condition that evaluates to a Boolean value .The statements will execute as long as the condition is true.

var count =1;
while(count<=10){
document.writeln(count);
count++;
}
The following code prints numbers from 1 to 10.

Road Works.svg Work in progress, expect frequent changes. Help and feedback is welcome. See discussion page. Road Works.svg