Unit3.3-JS-ConditionalStatement

From WikiEducator
Jump to: navigation, search
Uou-logo-100px.png
Home  |  About UOU  |  About CEMCA  |  Contact Us    

Unit 3.3 Web application development tools & Technologies


3.3.5 JavaScript
  3.3.5.1 Data Types
  3.3.5.2 Variables
  3.3.5.3 Operators
  3.3.5.4 Conditional statement
  3.3.5.5 Dialog Boxes
  3.3.5.6 Functions


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

                                                                                                 Next

Suggested Reading
  • Web Applications


 Suggested Videos
  • Video 01 Title
  • Video 02 Title
  • Video 03 Title