Lesson Six - Decision making

Decision making is the most important part of making any programming or scripting language interactive. When the user types in some text in a text box, there may be certain criteria that the text has to meet. The most simlpest form of decision making is the if statement. For example, say the user types in their password. The password would be stored in an array. If the password is in the array, and it is typed correctly, then the user will be allowed into the Web site. Consider the lines below:

var Password = new Array() {templton};

var User;

if(User == Password[])
document.write("Welcome");

Notice that this if statement has only one line to process. If there were other lines before or after document.write("Welcome");, you would have to enclose them in { } curly braces. This is what is called a command block.

Now, say an if statement wasn't enough. You have two conditions that you want to test against and only one statement. This is where the if...else statement is very useful. With the if...else statement, the if value is returned as true and the else value is returned as false. If you have multiple variables that you need to test, you can put if...else statements within if...else statements. These are called nested if...else statements.

This seems to be making your program more open to a wider array of variables. However, this also seems like a more tedious way of testing your data. You can make data testing simpler by using switch statements. With a switch statement, instead of testing every expression, you can test the ones you want to be tested. Consider this, you have a program that requires the user to enter their astrological sign to find out what their horoscope is. Instead of the program going from sign to sign to see if this is the one they chose, the switch statement searches for the sign the user entered and then executes the program to print the horoscope. For example:

if(sign == Aquarius)
 {this is your horoscope}
else if(sign == Pisces)
 {this is your horoscope}
else if(sign == Aries)
 {this is your horoscope}
else if(sign == Taurus)
 {this is your horoscope}
else if(sign == Gemini)
 {this is your horoscope}
etc...

You could write:

switch (sign) {
 case Aquarius:
  [this is your hororscope]
break;
 case Pisces:
  [this is your hororscope]
break;
 case Aries:
  [this is your hororscope]
break;
 case Taurus:
  [this is your hororscope]
break;
 case Gemini:
  [this is your hororscope]
break;
etc...}

The line break; "kicks out" of the program as soon as the right sign is found. This creates efficientcy in programming and makes programs work faster, especially with slower modems and smaller amounts of RAM or slow processors. The point of programming in ANY language is to make the program efficient. If the user has to wait for their computer to catch up to yours, chances are you won't have too many users, only surfers.

NOTE: In actual programming, you would leave out the "[" and "{"

Question: WOW!! I had no idea that there are different reasons for using if...else statements and switch statements. Speaking of "efficiency", are there any other statements that I can use to test the data I'm using?

Answer: There are several. Next, we will look at repetition statements that include the while, do...while, for, for...in, and with statements.

Quick Links:
Have Java Problem
Do you have a Java Question?

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips