Ch. 3. Selections
Find the error? if (radius > 0); { area = radius*radius*PI; System.out.println("The area for the circle of radius " + radius + " is " + area); }
- Adding a semicolon at the end of an if clause. - This mistake is hard to find, because it is not a compilation error or a runtime error, it is a logic error.
Binary Operator Associativity:
- All binary operators except assignment operators are left-associative. a - b + c - d is equivalent to ((a - b) + c) - d - Assignment operators are right-associative. a = b += c = 5 is equivalent to a = (b += (c = 5))
Debugger is a program that facilitates debugging. You can use a debugger to
- Execute a single statement at a time. - Trace into or stepping over a method. - Set breakpoints. - Display variables. - Display call stack. - Modify variables.
The boolean Type and Operators
- Java provides six comparison operators (also known as relational operators) that can be used to compare two values. - The result of the comparison is a Boolean value: true or false. > boolean b = (1 > 2);
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. - If operators with the same precedence are next to each other, their associativity determines the order of evaluation. All binary operators except assignment operators are left-associative.
What is Debugging
- The process of finding and correcting errors - 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.
switch Statement Rules: (1/2) ----------------------------------- switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; default: statement(s)-for-default; }
- The switch-expression must yield a value of char, byte, short, or int type and must always be enclosed in parentheses. - The value1, ..., and valueN must have the same data type as the value of the switch-expression. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch-expression. - The value1, ..., and valueN are constant expressions, meaning that they cannot contain variables in the expression, such as 1 + x.
switch Statement Rules: (2/2) ----------------------------------- switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; default: statement(s)-for-default; }
- 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. - The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed. - The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression.
Operator Associativity:
- When two operators with the same precedence are evaluated, the associativity of the operators determines the order of evaluation.
Types of Loops:
1. if Statements 2.
Is this format correct? if i > 0 { System.out.println("i is positive"); }
No, correct format: if (i > 0) { System.out.println("i is positive"); }
Is this format correct? if (i > 0) System.out.println("i is positive");
Yes, but not recommended
Logical errors are called ____________
bugs
Two-way if Statement
if (boolean-expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; }
One-way if Statements
if (boolean-expression) { statement(s); }
The else clause matches the ___________ if clause in the same block.
most recent
switch Statements
switch (status) { case 0: statement; break; case 1: statement; break; case 2: statement; break; case 3: statement; break; default: System.out.println("Errors: invalid status"); System.exit(1); }
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)