PGDEL/DECP03/Unit3/19
Unit 3.3 Web application development tools & Technologies
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 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 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){ Expression 1 sets up the counter variable. |
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){ 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. |
Work in progress, expect frequent changes. Help and feedback is welcome. See discussion page. |