Chapter 3 Programming
Chapter 3 Programming
Chapter 3 Programming
and Conditionals
1
The boolean Type and Operators
The result of the comparison is a Boolean value: true or false.
2
Relational Operators
3
One-way if Statements
if (radius >= 0) {
area = radius * radius * PI;
if (boolean-expression) { System.out.println("The area"
statement(s); + " for the circle of radius "
}
+ radius + " is " + area);
}
4
Note
if i > 0 { if (i > 0) {
System.out.println("i is positive"); System.out.println("i is positive");
} }
(a) Wrong (b) Correct
if (i > 0) { if (i > 0)
System.out.println("i is positive"); Equivalent System.out.println("i is positive");
}
(a) (b)
5
The Two-way if Statement
if (boolean-expression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
6
if-else Example
if (radius >= 0) {
area = radius * radius * 3.14159;
7
Multiple Alternative if Statements
(a) (b)
8
Multi-Way if-else Statements
9
Trace if-else statement
Suppose score is 70.0 The condition is false
10
animation
Trace if-else statement
Suppose score is 70.0 The condition is false
11
animation
Trace if-else statement
Suppose score is 70.0 The condition is true
12
animation
Trace if-else statement
Suppose score is 70.0 grade is C
13
animation
Trace if-else statement
Suppose score is 70.0 Exit the if statement
14
Note
The else clause matches the most recent if clause
in the same block.
15
Note, cont.
Nothing is printed from the preceding statement. To
force the else clause to match the first if clause, you
must add a pair of braces:
int i = 1;
int j = 2;
int k = 3;
if (i > j) {
if (i > k)
System.out.println("A");
}
else
System.out.println("B");
This statement prints B.
16
Common Errors
Adding a semicolon at the end of an if clause is a common
mistake.
if (radius >= 0);
Wrong
{
area = radius*radius*PI;
System.out.println(
"The area for the circle of radius " +
radius + " is " + area);
}
This mistake is hard to find, because it is not a compilation error
or a runtime error, it is a logic error.
This error often occurs when you use the next-line block style.
17
TIP
if (number % 2 == 0) Equivalent
even = true; boolean even
else = number % 2 == 0;
even = false;
(a) (b)
18
CAUTION
Equivalent if (even)
if (even == true)
System.out.println( System.out.println(
"It is even."); "It is even.");
(a) (b)
19
Problem: Computing Taxes
Write a program that calculates income taxes. Use the below tax rate:
Up to ETB 600 0%
20
Logical Operators
|| or logical disjunction
true false !(age > 18) is false, because (age > 18) is true.
22
Truth Table for Operator &&
false false false (age <= 18) && (weight < 140) is false, because both
false
true false (age > 18) && (weight > 140) is false, because (weight
false true true (age > 34) || (weight <= 140) is true, because (age > 34)
true
true false (age > 14) || (weight >= 150) is false, because
true 24
true true
Truth Table for Operator ^
false false false (age > 34) ^ (weight > 140) is true, because (age > 34) is false
false true true (age > 34) ^ (weight >= 140) is true, because (age > 34) is false
true
true false (age > 14) ^ (weight > 140) is true, because (age > 14) is
false 25
true true
The & and | Operators
If x is 1, what is x after this
expression?
(x > 1) & (x++ < 10)
28
switch Statement Rules
The switch-expression must
yield a value of char, byte,
switch (switch-expression) {
short, or int type and must
always be enclosed in case value1: statement(s)1;
parentheses. break;
case value2: statement(s)2;
The value1, ..., and valueN must break;
have the same data type as the …
value of the switch-expression.
The resulting statements in the case valueN: statement(s)N;
case statement are executed when break;
the value in the case statement default: statement(s)-for-default;
matches the value of the switch-
}
expression. Note that value1, ...,
and valueN are constant
expressions, meaning that they
cannot contain variables in the
expression, such as 1 + x.
29
switch Statement Rules
The keyword break is optional,
switch (switch-expression) {
but it should be used at the end
of each case in order to case value1: statement(s)1;
terminate the remainder of the
switch statement. If the break break;
statement is not present, the
next case statement will be
case value2: statement(s)2;
executed. break;
…
case valueN: statement(s)N;
The default case, which is break;
optional, can be used to perform default: statement(s)-for-default;
actions when none of the
specified cases matches the }
switch-expression.
When the value in a case statement matches the value
of the switch-expression, the statements starting from
this case are executed until either a break statement or
the end of the switch statement is reached.
30
Trace switch statement
Suppose day is 2:
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
31
Trace switch statement
Match case 2
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
32
Trace switch statement
Fall through case 3
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
33
Trace switch statement
Fall through case 4
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
34
Trace switch statement
Fall through case 5
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
35
Trace switch statement
Encounter break
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
36
Trace switch statement
Exit the statement
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5: System.out.println("Weekday"); break;
case 0:
case 6: System.out.println("Weekend");
}
37
Problem: Chinese Zodiac
Write a program that prompts the user to enter a year and displays the
animal for the year.
38
Conditional Expressions
if (x > 0)
y=1
else
y = -1;
is equivalent to
y = (x > 0) ? 1 : -1;
(boolean-expression) ? expression1 : expression2
Ternary operator
Binary operator
Unary operator
39
Conditional Operator
if (num % 2 == 0)
System.out.println(num + “is even”);
else
System.out.println(num + “is odd”);
System.out.println(
(num % 2 == 0)? num + “is even” :
num + “is odd”);
40
Conditional Operator, cont.
41
Operator Precedence
()
var++, var--
+, - (Unary plus and minus), ++var,--var
(type) Casting
! (Not)
*, /, % (Multiplication, division, and remainder)
+, - (Binary addition and subtraction)
<, <=, >, >= (Relational operators)
==, !=; (Equality)
^ (Exclusive OR)
&& (Conditional AND) Short-circuit AND
|| (Conditional OR) Short-circuit OR
=, +=, -=, *=, /=, %= (Assignment operator)
42
Operator Precedence and Associativity
The expression in the parentheses is evaluated first.
(Parentheses can be nested, in which case the
expression in the inner parentheses is executed
first.) When evaluating an expression without
parentheses, the operators are applied according to
the precedence rule and the associativity rule.
43
Operator Associativity
3 + 4 * 4 > 5 * (4 + 3) - 1
(1) inside parentheses first
3 + 4 * 4 > 5 * 7 – 1
(2) multiplication
3 + 16 > 5 * 7 – 1
(3) multiplication
3 + 16 > 35 – 1
(4) addition
19 > 35 – 1
(5) subtraction
19 > 34
(6) greater than
false 45
Debugging
Logic errors are called bugs. The process of finding
and correcting errors is called debugging. A common
approach to debugging is to use a combination of
methods to narrow down to the part of the program
where the bug is located. You can hand-trace the
program (i.e., catch errors by reading the program),
or you can insert print statements in order to show
the values of the variables or the execution flow of
the program. This approach might work for a short,
simple program. But for a large, complex program,
the most effective approach for debugging is to use
a debugger utility.
46
Debugger
Debugger is a program that facilitates debugging. You can use a debugger to
47