Page 142 - Introduction to Programming with Java: A Problem Solving Approach
P. 142

                108 Chapter 4 Control Statements
Whatever is in the places marked by <condition> always evaluates to either true or false.
Typically, each condition involves some type of comparison. With pseudocode, you can use words to describe the comparisons, but in real Java code, you must use special comparison operators. Comparison operators (also called equality and relational operators) are like mathematical operators in that they link adjacent operands, but instead of combining the operands in some way, they compare them. When a math- ematical operator combines two numbers, the combination is a number like the operands being combined. But when a comparison operator compares two numbers, the result is a different type. It is not a number like
the operands being compared. It is a Boolean truth value—either true or false. Here are Java’s comparison operators:
==, !=, <, >, <=, >=
The == operator tests whether two values are equal. Notice that this symbol uses two equals signs! This is different from the single equals sign that we all use instinctively to represent equality. Why does Java use two equals signs for equality in a comparison? It’s because Java already uses the single equals sign for as- signment, and context is not enough to distinguish assignment from equality. Do not try to use a single = for comparison! The Java compiler will not like it.
The != operator tests whether two values are unequal. As you’d expect, the < operator tests whether a value on the left is less than a value on the right. The > operator tests whether a value on the left is greater than a value on the right. The <= operator tests whether a value on the left is less than or equal to a value on the right. The >= operator tests whether a value on the left is greater than or equal to a value on the right. The result of any one of these tests is always either true or false.
4.3 if Statements
Apago PDF Enhancer
 Now, let’s look at a simple example of the condition in an if statement. Here’s a simple if statement that checks a car’s temperature gauge value and prints a warning if the temperature is above 215 degrees:
     condition
  if (temperature > 215)
   {
}
System.out.println("Warning! Engine coolant is too hot.");
System.out.println("Stop driving and allow engine to cool.");
The condition uses the > operator to generate a true value if the temperature is above 215 degrees or a false value if it is not. The subordinate statements execute only if the condition generates a true value.
Syntax
In the above example, note the parentheses around the condition. Parentheses are required whenever you have a condition, regardless of whether it’s for an if statement, a while loop, or some other control struc- ture. Note the braces around the two subordinate print statements. Use braces to surround statements that are logically inside something else. For example, braces are required below the main method’s heading and at the bottom of the main method because the statements inside the braces are logically inside the main method. Likewise, you should use braces to surround the statements that are logically inside an
















































































   140   141   142   143   144