Wyo Java Ch. 5 Lecture Notes
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 = 0;
if (num == 5)
{
System.out.println("hello world");
}
would display "hello world". However, the code segment
int num = 0;
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: 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
| AND | OR | NOT | |||||||
| A | B | A && B | A | B | A || B | A | !A | ||
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | ||
| 0 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | ||
| 1 | 0 | 0 | 1 | 0 | 1 | ||||
| 1 | 1 | 1 | 1 | 1 | 1 | ||||
Example:
int a = 0, b = 1, c = 2;
if (2 * b - c > 5 && Math.PI / Math.pow(2, 0) > 3 && c + 2 * c / a > 0 || a < b)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
The compiler evaluates the whole control expression to be true without even evaluating the left side of the || operator. Since a < b is true and since the || is the last operation according to the order of operations, the whole expression is considered to be true. Notice that the division by 0 (a = 0) would normally cause a run-time error but since that part of the expression is never evaluated, the error is avoided.Some compilers and languages support short-circuit evaluation and some do not. When short-circuit evaluation is supported, it allows efficient programmers to rearrange control expressions in an order that saves a little bit of time and thus speeds up the program. In this example, since Dumb Johnny and Lazy Joe are the weakest students, their expressions are placed in the control expression before Smart Sally's.
if (dumbJohnnysGrade > 60 && lazyJoesGrade > 60 && smartSallysGrade > 60)
{
System.out.println("Everyone in the class gets a candy bar"):
}
else
{
System.out.println("No one gets a candy bar");
}
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 #5: Compare strings correctly using the equals method.
Objective #6: 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
Objective #7: Be able to use the switch statement.
switch (characterEntered)
{
case 'A':
System.out.println("You entered
an
A");
break;
case 'B':
System.out.println("You entered a B");
break;
default:
System.out.println("Illegal entry");
break;
}