if statements
Objective #1: Use if statements.
if (number == 3)
{
System.out.println("The
value
of
number is 3");
System.out.println("Goodbye");
}
if (num > 0)
System.out.println("num is positive");
System.out.println("num is not zero");
the body of the if statement is only considered to be the first System.out.println statement according to the Java compiler. The second statement would execute even if num is equal to zero. The indentation of the second statement has no effect on the way the compiler interprets this code.
Always use the "double equals" symbol (i.e. comparison operator) rather than the assignment operator in control expressions. For example,
int num = 5;
if (num == 5)
{
System.out.println("hello world");
}
would display "hello world". However, the code segment
int num = 5;
if (num = 5)
{
System.out.println("hello world");
}
would cause a compile error.
Avoid using an unnecessary semicolon after a control expression:
if (num > 0);
{
System.out.println("num is positive");
}
which would cause the phrase "num is positive" to be displayed even if num is negative since the body of the if statement would be considered empty.
Objective #2: Be able to use the if else statement.
if
(number < 0)
{
System.out.println("The number
is negative.");
}
else
{
System.out.println("The number
is zero or positive.");
}
Objective #3: Be able to use and interpret nested if statements.
Objective #4: Avoid usage problems and errors with if statements.
Objective #5: Use Boolean expressions with relational (comparison) and logical (Boolean) operators.
== equal to NOTE: this is two equals symbols next to each other, not to be confused with the assignment operator, =
> greater than
< less than
>= greater than or equal to
<= less than or equal to
!= not equal to
&& is
the logical AND operator
|| is
the logical OR operator
! is
the logical NOT operator
A |
B |
A && B |
A || B |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
1 |
1 |
1 |
1 |
1 |
Example:
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the number of people sharing the pizza: ");
int num = keyboard.nextInt();if (num > 0 && 8 / num >= 2)
{
System.out.println("Each person will get 2 or more pieces of pizza.");
}if (8 / num >= 2 && num > 0) // causes run-time ArithmeticException error if num is zero
{
System.out.println("Each person will get 2 or more pieces of pizza.");
}
If zero is inputted for num, the first if statement above works fine since the compiler works from left to right through the control expression and num > 0 evaluates to false making the whole if control expression false. In other words, the compiler never bothers to evalute the 8 / num >= 2 part of the control expression. This is called short-circuit evaluation because the whole control expression is "short-circuited" and never complete evaluated.
If zero is inputted for num, the control expression in second if statement again is evaluated from left to right and a division-by-zero runtime ArithmeticException error immediately occurs.Since Java supports short-circuit evaluation, it is wise for a Java coder to take advantage of it and write if statement control expressions like the first example to avoid potential runtime errors.
A |
B |
B || A |
!(B || A) |
(!(B || A) || A) |
A && (!(B || A) || A) |
A && B |
A || B && !A |
A |
0 | 0 | |||||||
0 | 1 | |||||||
1 | 0 | |||||||
1 | 1 |
Objective #6: Use an if statement to parse and identify characteristics of integers.
Objective #7: Compare floating-point values and double variables correctly.
double a = 2.0; double b = Math.sqrt(2) * Math.sqrt(2); // 2.0000000000000004 if (a == b) { System.out.println("equal"); } else { System.out.println("NOT EQUAL when using the == operator with double variables"); } if (a - b < 1E-15) { System.out.println("EQUAL when subtracting them and checking that the difference is negligible"); } else { System.out.println("not equal"); }
NOT EQUAL when using the == operator with double variables EQUAL when subtracting them and checking that the difference is negligible