The function construct, itself, forms another way to affect flow of control through a whole program. This will be discussed later in the course.
x == y // x is equal to y x != y // x is not equal to y x < y // x is less than y x <= y // x is less than or equal to y x > y // x is greater than y x >= y // x is greater than or equal to y
We also have Boolean operators for combining expressions. Again, these operators return true or false
!x // the NOT operator (negation) -- true if x is false x && y // the AND operator -- true if both x and y are true x || y // the OR operator -- true if either x or y (or both) are true x ^ y // the exclusive or operator -- true if exactly one operand is true and one is false
These operators will be commonly used as test expressions in selection
statements or repetition statements (loops).
(x > 0 && y > 0 && z > 0) // all three of (x, y, z) are positive
(x < 0 || y < 0 || z < 0) // at least one of the three variables is negative
( numStudents >= 20 && !(classAvg < 70))
// there are at least 20 students and the class average is at least 70
( numStudents >= 20 && classAvg >= 70)
// means the same thing as the previous expression
(d != 0 && n / d > 0) // notice that the short circuit is crucial in this one. If d is 0, // then evaluating (n / d) would result in division by 0 (illegal). But // the "short-circuit" prevents it in this case. If d is 0, the first // operand (d != 0) is false. So the whole && is false.
if (boolean expression)
{
statement(s)
}
if (boolean expression)
{
statement(s)
}
else
{
statement(s)
}
if (grade >= 68)
System.out.print("Passing");
// Notice that there is no else clause. If the grade is below 68, we move on.
if (x == 0)
System.out.println("Nothing here");
else
System.out.println("There is a value");
// This example contains an else clause. The bodies are single statements.
if (y != 4)
{
System.out.println("Wrong number");
y = y * 2;
counter++;
}
else
{
System.out.println("That\'s it!");
success = true;
}
Multiple statements are to be executed as a result of the condition being true or false. In this case, note that the blocks are needed.
// What output will it produce if val = 2? Does the "too bad" statement really go with the "else" here?
if (val < 5)
System.out.println("True");
else
System.out.println("False");
System.out.println("Too bad!");
* Indentation is only for people! It improves readability, but means nothing to the compiler.
if (x == 1 || 2 || 3)
System.out.print("x is a number in the range 1-3");
if (x > 5) && (y < 10)
System.out.print("Yahoo!");
if (response != 'Y' || response != 'N')
System.out.print("You must type Y or N (for yes or no)");
switch (expression)
{
case constant:
statements
case constant:
statements
... (as many case labels as needed)
default: // optional label
statements
}
boolean_expression ? true_expression : false_expression
System.out.print( (x > y) ? "x is greater than y" : "x is less than or equal to y");
// Note that this expression gives the same result as the following
if (x > y)
System.out.print("x is greater than y");
else
System.out.print("x is less than or equal to y");
(x < 0 ? value = 10 : value = 20);
// this gives the same result as:
value = (x < 0 ? 10 : 20);
// and also gives the same result as:
if (x < 0)
value = 10;
else
value = 20;