Anda di halaman 1dari 34

Chapter 3: Selections Introduction to Java Programming, Daniel Liang, 8th Edition

Spring 2012 - 2013

Introduction

Lets recall the ComputeArea program. What if the user enters a negative value? How to prevent that?

Using Selection Statements

Fatima Kanj, Spring 2012 - 2013

Comparisons and boolean Expressions


We can perform comparison in java. The result of the comparison is a boolean value:

true or false Example: System.out.println(1 < 2); //Output: false

Comparison Operators (Relational Operators)

Fatima Kanj, Spring 2012 - 2013

Boolean Variables

Declaration of boolean variables:

boolean variableName;

Range of values of boolean type:

Two values: true or false

Example:

boolean isEquilateral = true; boolean flag = false;

Fatima Kanj, Spring 2012 - 2013

Types of Selection Statements

Java has several types of selection statements:

if Statements:

One-way if Statements Two-way if Statements Nested if Statements

switch Statements Conditional Expressions

Fatima Kanj, Spring 2012 - 2013

One-Way if Statement

A one-way if statement executes an action if and only if the condition is true. Example:

Fatima Kanj, Spring 2012 - 2013

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.

Fatima Kanj, Spring 2012 - 2013

Two-Way if statement

The actions that a two-way if statement specifies differ based on whether the condition is true or false.

Fatima Kanj, Spring 2012 - 2013

Example:

To check whether an integer is even or odd:

Fatima Kanj, Spring 2012 - 2013

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.

The nested if statement can be used to implement multiple alternatives.

10

Fatima Kanj, Spring 2012 - 2013

Example:

Finding the letter grade of a given score:

11

Fatima Kanj, Spring 2012 - 2013

Common Errors in Selection Statements

Common Error 1: Forgetting Necessary Braces

Common Error 2: Semicolon at the if Line

12

Fatima Kanj, Spring 2012 - 2013

Common Errors in Selection Statements

Common Error 3: Redundant Testing of Boolean Values

Common Error 4: Dangling else Ambiguity (The else clause always matches the most recent unmatched if clause in the same block).

13

Fatima Kanj, Spring 2012 - 2013

Common Errors in Selection Statements

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

Fatima Kanj, Spring 2012 - 2013

switch Statements

To replace nested if statements to handle multiple conditions efficiently. The syntax is as follows:

15

Fatima Kanj, Spring 2012 - 2013

Rules of the switch Statement


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

Fatima Kanj, Spring 2012 - 2013

switch Example

A simple program which prints on the console a different message when the user enters an integer from 1 to 4.

17

Fatima Kanj, Spring 2012 - 2013

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

Fatima Kanj, Spring 2012 - 2013

Logical Operators or Boolean Operators

Used to combine several conditions.

19

Fatima Kanj, Spring 2012 - 2013

Truth Table for Operator !

20

Fatima Kanj, Spring 2012 - 2013

Truth Table for Operator &&

21

Fatima Kanj, Spring 2012 - 2013

Truth Table for Operator ||

22

Fatima Kanj, Spring 2012 - 2013

Truth Table for Operator ^

23

Fatima Kanj, Spring 2012 - 2013

Boolean Operators: Example 1

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

Fatima Kanj, Spring 2012 - 2013

Boolean Operators: Example 2

Write a the program that lets the user enter a year and checks it is leap.

25

Fatima Kanj, Spring 2012 - 2013

Unconditional vs. Conditional Boolean Operators

Unconditional Operators (& , | )

They work exactly the same as the && and || operators with one exception: the & and | operators always evaluate both operands.

Conditional (&&, ||)

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

Fatima Kanj, Spring 2012 - 2013

Formatting Console Output

If you wish to display only two digits after the decimal point in a floating-point value, you may write the code like this:

A better way: Use the printf method.

where format is a string that may consist of substrings and format specifiers. A format specifier specifies how an item should be displayed.

27

Fatima Kanj, Spring 2012 - 2013

Frequently Used Specifiers

28

Fatima Kanj, Spring 2012 - 2013

Formatting Output

29

Fatima Kanj, Spring 2012 - 2013

Example:

30

Fatima Kanj, Spring 2012 - 2013

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

Fatima Kanj, Spring 2012 - 2013

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.

Binary operators are left associative

Assignment operators are right associative

32

Fatima Kanj, Spring 2012 - 2013

Operator Precedence

33

Fatima Kanj, Spring 2012 - 2013

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

Anda mungkin juga menyukai