Condition Checking
If
We can use this statement if we want to execute a set of code in case the given condition is true. Let us say if today is Sunday go to market.
syntax (How it is written):
if (condition){
job to be done if condition is found true
}
[Top]
If - - else
We can use this statement if we want to execute a set of code in case the given condition is true and other code in case of given condition is false. For example:
if today is Sunday
go to market
else go to office
Syntax
if (condition){
job to be done if condition is found true
}
else{job to be done if condition is found false
}
[Top]
if - - else if – else
We can use this statement if we want to execute a set of code out of many sets of codes. Fox example
if today is Sunday
go to market
else if time is 9 ‘O' Clock or more
go to office
else
go to yoga class
similar code(syntax)…
[Top]
Switch
We can use this statement if we want to execute a block of code for a situation, out of many blocks of codes for different situations. For example
syntax (How to write) :
switch(variablename){
case 1 :
code to be executed
breakcase 2 :
code to be executed
breakcase 3 :
code to be executedbreak
default :
code to be executed if non of the above give cases are found}
[Top]
Example
sample code
<html>
<head></head>
<body>
<script type="text/javascript">var a
a=parseInt(prompt("Enter value for a",""))
document.write("<br>Value of a "+a);
if (a<=5){document.write("<br>a is lesser than or equal to 5 ");
}
else if (a>5 && a<10){document.write("<br>a is greater than 5 but lesser than 10");
}
else{
document.write("<br> a is greater than or equal to 10");
}
var day
a=parseInt(prompt("Enter Weekday please",""))
switch(a){case 1:
document.write("<br> It is Sunday ");
document.write("<br> Enjoy…");break
case 2:
document.write("<br> It is Monday ");
document.write("<br> go to College ");break
case 3:
document.write("<br> It is Tuesday ");
document.write("<br> go to College ");break
case 4:
document.write("<br> It is Wednesday ");
document.write("<br> go to College ");break
case 5:
document.write("<br> It is Thrusday ");
document.write("<br> go to College ");break
case 6:
document.write("<br> It is Friday ");
document.write("<br> go to College ");
breakcase 7:
document.write("<br> It is Saturday ");
document.write("<br> go to College ");break
default :
document.write("<br> Invalid weekday enter value from 1-7")
}</script>
</body>
</html>
Understanding program :
In the above program we have used prompt method which is used to accept input from user , we learnt this in dialog box section, parseInt function will convert the accepted value to number type because the value returned by the prompt method will be of string type and we can not compare or perform any arithmetic calculation with a string value, so we used parseInt method.
The output of the above program will depend on the value entered by the user, for different values different block of statements will be executed.U can c the result f above code by the link given blow.I hope more explaination is nt necessary.
C the implementation of above code ..click condition
[Top]
«<Javascript4|Javascript6»>