IWATS - JavaScript

Conditional Statements

These are used in JavaScript to perform various different actions based on different conditions. Below this test will explain how to write a conditional statements such as if, and if else, as well as how to use them in your code.

When you are writing code you will sometimes want to various different actions based on decisions. Such as turning a page blue if that is your favorite color. Conditional statements within the code can help a user do this. There are actually three different types of conditional statements, one of which is used seldom in my experience.

If Statement

This type of statement can be used if you want to do something, only if a condition is true. Such as, going swimming if it is warm and sunny out. Or baking cookies if you have the right set of ingredients on hand. The syntax for an if statement can be seen below in Figure 5.1.

Figure 5.1
<script language="JavaScript" type="text/javascript">
if (rank == captain)
{
document.write("Welcome Captain")
}
</script>

Note that in the figure above that the code only executes if the condition is true, so nothing happens if it is false. It isn't necessary to say that you otherwise do nothing, it is implied, just as with the integer 5, it is assumed that you mean 5 / 1. Though you could say to otherwise do nothing, but that would make it the next type of conditional statement.

If...else Statement

This type of statement is used if you want to select one of two different options to execute. Such as, go swimming if it is warm and sunny out else go to the bowling ally. Or, bake yourself some cookies if you have all the ingredients else go to the store and purchase more ingredients. The syntax for an if...else statement can be seen below in Figure 5.2.

Figure 5.2
<script language="JavaScript" type="text/javascript">
if (rank == captain)
{
document.write("Welcome Captain")
}
else
{
document.write("You aren't a Captain!")
}
</script>

Note that in the figure above you give the switch an alternative action to perform in the case of a false. If the variable in question does not equal captain, than an alternative is given. This is a very commonly used and very powerful tool of JavaScript.

Switch Statement

This is the option to use if you want to use one of many different sets of instructions. An example being that you call up certain clients on certain days of the week. By using a condition statement to determine the day of the week say, Monday, then the switch would display what is supposed to happen on Monday, such as call client from the ISD Intrepid. The syntax for a switch statement can be seen below in Figure 5.3.

Figure 5.3
<script language="JavaScript" type="text/javascript">
switch (dayofweek)
{
case 1:
  document.write("It is Monday, contact the ISD Intrepid")
  break    
case 2:
  document.write("It's Tuesday, contact the ISD Vanguard")
  break
default:
  document.write("It is unnecessary to contact anybody today.")
}
</script>

In the switch above, dayofweek is a number (Monday = 1, Tuesday = 2, etc.). First there is a single expression given in this example as switch (dayofweek), which is evaluated once. This means that only one value will be extracted from the expression. In this case a number to designate day of the week. The value obtained is then checked against each case in the switch, in this example case 1, and then case 2. If it matches the value up with one of the cases than the block of code associated with that case is executed, followed by a break to keep the rest of the cases codes to be executed. After the break the script exits the switch. If the value does not match up with any of the cases then it will run the block of code present in the default section of the switch. No break is necessary here as default is at the end of the list of cases available in the switch.