Home
<<Table of Contents
<<Arrays
Loops>>

MAKING DECISIONS

To be useful, a program must be able to react to changes. The concepts presented here allow you to do just that. They are essential to any serious programming language. If you are familiar with C/C++ or similar languages, this will all seem very fimiliar.

  1. The If Statement
  2. The Else and Else if Statements
  3. Switch Statements


Top

The If Statement

My Father always told me, "If is the biggest word in the english language." This also holds true for programming languages. The "If" statement allows your program to make a decision. Let's say the person using your applet can not seem to remember what qualifies as an "A," but he wants to know if he got one. Well, it is a safe bet this person fell a little short of the ninety mark, but I will show you how its done.

if (grade > 90) {
This code will execute only if the value stored
in the varible "grade" is greater then ninety.

}

Allow me to go into greater depth about the structure of an "if statement." First, comes the word "if" (I hope you have figured that out by now), then inside the parenthesis is the condition. If the condition is true, all code inside the braces is executed, if it is not, the code inside the braces is ignored. Now, if this person's grade is exactly sixty-six, you might want to tell him that he just barely passed (surprisingly).

if (grade == 66) {
This code only executes if "grade"
equals sixty-six exactly.

}

Notice there are two equal signs, that is not a typo (you thought you had me there). This is one of the most common mistakes for people who are switching from "BASIC" to Java or C/C++. If you only use one equal sign, your program will not work. There are several operators you may use in your conditions.

==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

I hope you found my table informative. Basic programmers should find all of these operators familiar except one. In the interest of saving BASIC programmers much frustration, you always use the "!=" operator if you want to check for a value being unequal to another value. You do not use a greater-than and less-than sign next to each other.
There are two more operators I have yet to mention - the logical-and (&&) and the logical-or (||) operators. The "|" character is to the right of the "]" button on most keyboards and usually under the "Back Space" button. You must hold "Shift" to type it. These operators are used to test for multiple conditions. For example:

if (grade <= 15 && IQ <= 20) {
This code will only execute if "grade"
is equal to or less-than fifteen
and IQ is equal to or less-than twenty.

}

You may also use parenthesis inside of the condition.

if (grade <= 15 && (IQ <= 20 || SAT <= 100)) {
If "grade" is below or equal to fifteen
and either "IQ" is less-than or equal twenty
or "SAT" is less-than or equal to 100, this
code will be executed.

}



Top


The Else and Else if Statements

Now, you can execute code if a statement is true, but what if you want to execute different code if your condition is false. Now, you can use the "else" statement. Allow me to provide an example.

if (grade >= 90) {
This code executes if the grade is an "A."
}
else {
This code executes if the grade was not an "A."
}

Else statements are simple enough. If the condition provided in the "if statement" is true, the following code is executed but not the code in the "else statement." If the code is false, the "if statement's" code is ignored and the "else statements" code is executed.
You can also have several "if's" togethor with an "else if" statement. For example:

if (grade >= 90) {
	The grade is an "A."
}
else if (grade >= 80 && grade < 90) {
	The grade is a "B."
}
else if (grade >= 70 && grade < 80) {
	The grade is a "C."
}
else if (grade >= 65 && grade < 70) {
	The grade is a "D."
}
else {
	The grade is an "F."
}

Very simply, the computer checks each condition, if none of the conditions are true, the code in the "else statement" is executed. If more then one of the conditions are true, both of them will be executed, and if you modify a variable that makes a following condition true (e.g. you change the value of "grade" inside one of the "else if statements"), the statement will execute.



Top


Switch Statements

A "Switch Statement is very similar to a group of "else if statements." It is used to test for several conditions of one variable. Here is another example.

switch (grade) {
	case 100:
		This code executes if
		"grade" is one hundred.
		break;
	case 95:
		This code executes if
		"grade" is ninety-five.
		break;
	case 65:
		This code executes if
		"grade" is sixty-five.
		break;
	case 20:
		This code executes if
		"grade" is twenty.
		break;
	defualt:
		"grade" is not equal to
		any of the values listed.
}

In a "switch statement," you must put a variable of type char, byte, short, or int (see "
Variables in Java") in the parenthesis. Every "case block" checks for a certain value of the variable; if it matches, the code is executed. If there are no matches, the default case block is executed. In Java and C/C++, code in the case blocks "falls through;" when a "case block" matches, the code in all of the following "case blocks" is executed along with the one that mathces. To stop this from happenning, you must put a "break;" statement at the end of all your "case blocks." The "break" statement exits the the "switch statement." It is not needed for the last "case block" because there is no code after it in the "switch statement."



Home
<<Table of Contents
<<Arrays
Loops>>

Now, you can make decisions. Yes, you are truly decisive. Our next chapter is about loops. So, you can become truly loopy - or not. Well, maybe you should read the next chapter before you start to wonder about who is loopy.