Introduction
Lets recall the ComputeArea program. What if the user enters a negative value? How to prevent that?
We can perform comparison in java. The result of the comparison is a boolean value:
Boolean Variables
boolean variableName;
Example:
if Statements:
One-Way if Statement
A one-way if statement executes an action if and only if the condition is true. Example:
Example:
Write a program that prompts the user to enter an integer. If the number is a multiple of 5, print HiFive. If the number is divisible by 2, print HiEven.
Two-Way if statement
The actions that a two-way if statement specifies differ based on whether the condition is true or false.
Example:
Nested if Statement
The statement in an if or if ... else statement can be any legal Java statement, including another if or if ... else statement. The inner if statement is said to be nested inside the outer if statement.
The nested if statement can contain another if statement; in fact, there is no limit to the depth of the nesting.
10
Example:
11
12
Common Error 4: Dangling else Ambiguity (The else clause always matches the most recent unmatched if clause in the same block).
13
Common Error 5: Assignment operator (=); if the value of even was false, this will assign true to even and prints It is even. to the console.
14
switch Statements
To replace nested if statements to handle multiple conditions efficiently. The syntax is as follows:
15
The switch-expression must yield a value of char, byte, short, or int type The value1, and valueN must have the same data type as the value of the switch-expression. The keyword break is optional: It ends the switch statement. The default case is optional: It can be used to perform actions when none of the specified cases matches the switch-expression.
16
switch Example
A simple program which prints on the console a different message when the user enters an integer from 1 to 4.
17
Conditional Expressions
boolean-expression ? expression1 : expression2; The result of this conditional expression is expression1 if boolean-expression is true; otherwise the result is expression2.
18
19
20
21
22
23
Write a program that checks whether a number is divisible by 2 and 3, by 2 or 3, and by 2 or 3 but not both:
24
Write a the program that lets the user enter a year and checks it is leap.
25
They work exactly the same as the && and || operators with one exception: the & and | operators always evaluate both operands.
When evaluating p1 && p2, Java first evaluates p1 and then evaluates p2 if p1 is true; if p1 is false, it does not evaluate p2.
26
If you wish to display only two digits after the decimal point in a floating-point value, you may write the code like this:
where format is a string that may consist of substrings and format specifiers. A format specifier specifies how an item should be displayed.
27
28
Formatting Output
29
Example:
30
Remark
By default, the output is right justified. You can put the minus sign (-) in the specifier to specify that the item is left justified in the output within the specified field.
31
Evaluation of Expressions
The expressions in parentheses are evaluated first. When evaluating an expression without parentheses, Java operators are evaluated according to:
Precedence (check table 3.10) Associativity: If operators with the same precedence are next to each other, their associativity determines the order of evaluation.
32
Operator Precedence
33
Example
Applying the operator precedence and associativity rule, the expression 3 + 4 * 4 > 5 * (4 + 3) - 1 is evaluated as follows:
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
34 Fatima Kanj, Spring 2012 - 2013