Conditional operation

Logic is used to make decisions in code; choosing to run one piece of code or another depending on the comparisons made. This requires use of something called a conditional. There are a few different conditionals that you might want to use, but we’ll just focus the one used most commonly: if.

If:
It’s very simple: if some logic (the condition) is true, run a block of code. You can also supply more code to be run if the condition is not true, and supply additional conditions and blocks of code to optionally be run. These forms are called if-else, as you’ll see below.

The most simple if statement looks something like this:

Syntax:
if (condition)
{
code to be executed if condition is true
}

Example:
<script type="text/javascript">
//Write "Lunch-time!" if the time is 11
var d=new Date()
var time=d.getHours()  
if (time==11)
{  document.write("<em>Lunch-time!</em>")  }
</script>


If-else:
The if-else form of an if statement is used to run an alternative piece of code if the conditional is not true. The code in the if block below will be ignored, for example - only the code in the else block will be run.

Syntax:
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}

Example:
<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>

If...else if...else Statement


You should use the if....else if...else statement if you 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
<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>

Switch

You should use the switch statement if you 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
<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.
var d=new Date()
theDay=d.getDay()
switch (theDay)
{
case 5:  
document.write("Finally Friday")  
break
case 6:  
document.write("Super Saturday")  
break
case 0:  
document.write("Sleepy Sunday")  
break
default:  
document.write("I'm looking forward to this weekend!")
}
</script>



No comments:

Post a Comment