Comparison Operators | Switch | If-Then-Else | Ternary Operator (condition? value1 : value2 ) | Loop - While | Loop - Do While | For Loop | Break | Continue

If-Then-Else

As with most other programming languages, JavaScript has a method of altering the course of execution. The most common method is testing a particular condition and then executing code based on the result of the test. The If-Then-Else construct is one of the most useful. Example:
JavaScript Result
<SCRIPT>
A=12
B=8
if (A>B)
  {
   document.write("A is larger than B")
  }
else
  {
  document.write("B is greater than or equal to A")
  }
</SCRIPT>
Table - If-Then-Else
Top

Comparison Operators

The following operators are used to compare variables:
OperatorDescription
= =Equal
!=NOT Equal
>Greater than
<Less than
<=Less than or Equal
>=Greater than or Equal
These operators will work with any variable type. If the two variables being compared are of different type, these operators will convert them and then do the comparison. If it is necessary to compare variables of exactly the same type. You can use the = = = or != = to verify that the variables are of the same type and value.
Top

Ternary Operator ?

(condition) ? value1 : value2

The ternary operator is often used in programming languages to "speed" writing the code. It should be used sparingly because it is easy to obfuscate the real purpose of the statement. Essentially, the ternary is short hand for an If-Then-Else construct.

The first item is the conditional statement which is to be analyzed. Put it in parentheses. Immediately following the conditional statement is a ?. This tells the script that if the condition is TRUE, execute whatever comes next. If the condition is FALSE, then the script executes whatever follows the colon (:)

The previous If-Then-Else script could be re-written as follows:

JavaScript Result
<SCRIPT>
A=12
B=8
document.write("let's try<BR/>")
document.write((A>B)?A:B)
(A>B)? document.write("A is greater than B"):
document.write("B is greater than or equal to A")
</SCRIPT>
Table - Ternary Operation

Note that the ternary operation does not allow you to do alternate printing. If you need to have some variable writing to the screen, use the form... document.write( (A>B)?A:B )

Top

Loop - While

Straightforward, similar to C++. The condition is tested at the beginning of executing each loop. Simply cite the condition which must remain true during the loop and then enclose the statements to loop within brackets.
JavaScript Result
<SCRIPT>
i = 1
while (i<6)
{
 document.write("The value of i=" + i++ + "<BR/>")
}
</SCRIPT>
Loop - While

Don't forget to find a way for the loop to stop. In this case it will be when i=>6. Also, don't forget to increment i, otherwise you will have an infinite loop. Fortunately infinite loops are detected by the browser and it will ask you if you want to continue but this is very bad form.

Top

Loop - Do While

Here the loop conditions are checked at the end of the statements so that the code is executed at least once regardless of the condition.
JavaScript Result
<SCRIPT>
i = 6
do
{
 document.write("The value of i=" + i++ + "<BR/>")
}
while (i<6);
</SCRIPT>
Loop - Do While
Note that the document.write statement had both string and variable info which was concatenated with the + Also, note that we used C++ feature of adding one to the variable i in both cases by specifying i as i++ Remember that this means use the current value of i and then increment it by one.
Top

For Loop

For loop is pretty standard. You must define the variable to be used to control, the condition which must be met to continue and, the increment which will take place on the variable at the end of each loop. The loop will continue so long as the condition is true. The condition is checked at the beginning of each cycle. The only way to breakout of the cycle is to use the break. If you wish to skip a part of the loop use the continue statement.
JavaScript Result
<SCRIPT>
total=0
for (i=1; i<=10; i++)
 {
  document.write("The value of i =" + i + "<BR/>")
  total += i
 }
document.write("Total =" + total)
</SCRIPT >
FOR LOOP

Make sure that you define all other variables outside the loop.

Top

The Break statement

Now let's look at the break. We can stop executing the loop at any time by using a condition to break the loop. For instance, using the above example we will stop the loop when the total >= 25:

JavaScript Result
<SCRIPT>
total=0
for (i=1; i<=10; i++)
 {
  document.write("The value of i =" + i + "<BR/>")
  total += i
  if (total >= 25)
   { break }
 }
document.write("Total = " + total)
</SCRIPT >
Using Break
Top

Continue Statement

The continue statement tells the program to skip the rest of the statements but continue the loop. Here is an example:
JavaScript Result
<SCRIPT>
total=0
for (i=1; i<=50; i++)
{
  total += i
  if (i> 5 && i< 46)
   { continue }
 document.write("i = " + i + "<BR/>")
}
document.write("The sum of all numbers = " + total)
</SCRIPT>
Continue statement

Switch and Case statements

The Switch works exactly like the Switch statements in C++ and Java (not quite the same as Visual Basic). It gives us the capablity of writing more elegant code than having multiple if-then-else statements nested or otherwise.
There are three parts to the statements and an optional fourth. First, you must start with switch at this point you identify the condition that you are testing. Next, for each possible condition you preface the condition with the case and follow it immediately with a colon. Then you can write whatever statements you expect to be performed for that particular condition. At the end you should put a break so that the program skips all the other cases and continues to the next operation after the end of the switch.
The only optional item is that you should have a default statement at the end to round up any unexpected conditions. Let's take a look:
JavaScript Result
<SCRIPT>
for (i=1; i<= 10; ++i)
{
   switch (i)
   {
    case 2:
    case 3:
     document.write(i + "small <BR/>")
    break

    case 4+1:
     document.write(i + "Bingo <BR/>")
    break

    case 8: case 9:
     document.write(i + "large <BR/>")
    break

    default:
     document.write(i + "<BR/>")
   }
}
</SCRIPT>
Switch and case statements

Note that the case condition must be reducible to an individual value. Thus, you can not use a range. However, you can specify each value on separate lines or on the same line (as in cases 2 & 3 and 8 & 9). You can even have an expression as in case 4+1.

Top
Next