JS Decision Control Statements
Sometimes, we may want to perform different actions for different decisions. We can use decision control statements in our code to do this. They are also called conditional statements.
In JS we have the following decision control statements:
- if statement – use this statement if we want to execute some code only if a specified condition is true
- if…else statement – use this statement if we want to execute some code if the condition is true and another code if the condition is false
- if…else if….else statement – use this statement if we want to select one of many blocks of code to be executed
- switch statement – use this statement if we want to select one of many blocks of code to be executed
1. if Statement
We should use the if statement if we want to execute some code only if a specified condition is true.
Syntax
if (condition)
{
code to be executed if condition is true
}
N.B. that if is written in lowercase letters. Using uppercase letters (IF) will generate a JS error!
Example.
<html>
<body>
<script type="text/javascript">
//Write a "Good morning" greeting if
//the time is less than 10
var d = new Date();
var time = d.getHours();
if (time < 10) {
document.write("<b>Good morning</b>");
}
</script>
<p>This example demonstrates the if statement.</p>
</body>
</html>
2. if…else Statement
If we want to execute some code if a condition is true and another code if the condition is not true, use the if….else statement.
Syntax
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
Example.
<html>
<body>
<script type="text/javascript">
//If the time is less than 10, you will get a "Good morning" greeting.
//Otherwise you will get a "Good day" greeting.
var d = new Date();
var time = d.getHours();
if (time < 10) {
document.write("Good morning!");
}
else {
document.write("Good day!");
}
</script>
</body>
</html>
3. if…else nesting
We should use the if….else nesting if we want to select one of many sets of lines to execute.
Syntax
if (condition1) {
code to be executed if condition1 is true
}
else if (condition2) {
code to be executed if condition2 is true
}
else {
code to be executed if condition1 and condition2 are not true
}
Example.
<html>
<body>
<script type="text/javascript">
var d = new Date();
var time = d.getHours();
if (time<10){
document.write("<b>Good morning</b>");
}
else if (time>10 && time<16) {
document.write("<b>Good day</b>");
}
else {
document.write("<b>Hello World!</b>");
}
</script>
</body>
</html>
4. JS switch Statement
We should use the switch statement if we want to select one of many blocks of code to be executed.
Syntax
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}
This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically.
Example.
<html>
<body>
<script type="text/javascript">
var d = new Date();
theDay = d.getDay();
switch (theDay) {
case 5:
document.write("<b>Finally Friday</b>");
break;
case 6:
document.write("<b>Super Saturday</b>");
break;
case 0:
document.write("<b>Sleepy Sunday</b>");
break;
default:
document.write("<b>I'm really looking forward to this weekend!</b>");
}
</script>
<p>
This JS code will generate a different greeting based on what day it is. Note that Sunday=0, Monday=1, Tuesday=2, etc.
</p>
</body>
</html>