Wednesday 18 December 2013

Javascript :Conditional Statements Part -1

Javascript(JS):

While writing a program, there may be a situation when you need to
choose one way out of the given two ways. Here we need conditional statements
that allow your program to make correct decisions and perform right actions.

JavaScript supports conditional statements which are used to perform different
actions based on different conditions

JavaScript supports following forms of if..else statement:

-> if statement

-> if...else statement

-> if...else if... statement.

if statement :

Syntax:

if (expression){
   if true execute the statement .....
}
If the resulting value is true,given statement will be executed.
If expression is false then no statement would be not executed.

Example:

<script type="text/javascript">

var age = 20;
if( age > 18 ){
   document.write("this is my world");
}
</script>


if...else if... statement:


Syntax:

if (expression 1){
   execute if expression 1 is true
}
else if (expression 2){
   execute if expression 2 is true
}
else if (expression 3){
   execute if expression 3 is true
}
else{
   execute if no expression is true
}
each if is part of the else clause of the previous statement.
Statement(s) are executed based on the true condition, if non of the condition
is true then else block is executed.


if...else statement:


Syntax:

if (expression){
   execute if expression is true
}
else{
   execute if expression is false
}
If the expression is true, given statement(s) in the if block, are executed.
If expression is false then given statement(s) in the else block, are executed.

No comments:

Post a Comment