Chapter 3 Selections

Embed Size (px)

Citation preview

  • 7/28/2019 Chapter 3 Selections

    1/34

    Chapter 3:Selections

    Introduction to J ava Programming, Daniel Liang, 8th Edition

    Spring 2012 - 2013

  • 7/28/2019 Chapter 3 Selections

    2/34

    Introduction

    Lets recall the ComputeAreaprogram.

    What if the user enters a negative value?

    How to prevent that? Using Selection Statements

    Fatima Kanj, Spring 2012 - 20132

  • 7/28/2019 Chapter 3 Selections

    3/34

    Comparisons and boolean Expressions We can perform comparison in java.

    The result of the comparison is a booleanvalue: trueor false Example: System.out.println(1 < 2); //Output: false

    Comparison Operators (Relational Operators)

    Fatima Kanj, Spring 2012 - 20133

  • 7/28/2019 Chapter 3 Selections

    4/34

    Boolean Variables

    Declaration of booleanvariables:

    booleanvariableName;

    Range of values of booleantype:

    Two values: true or false

    Example:

    booleanisEquilateral = true;

    booleanflag = false;

    Fatima Kanj, Spring 2012 - 20134

  • 7/28/2019 Chapter 3 Selections

    5/34

    Types of Selection Statements

    Java has several types of selection statements:

    if Statements:

    One-way ifStatements

    Two-way ifStatements

    Nested ifStatements switch Statements

    Conditional Expressions

    Fatima Kanj, Spring 2012 - 20135

  • 7/28/2019 Chapter 3 Selections

    6/34

    One-Way if Statement

    A one-way ifstatement executes an action if and only ifthe condition is true.

    Example:

    Fatima Kanj, Spring 2012 - 20136

  • 7/28/2019 Chapter 3 Selections

    7/34

    Example:

    Fatima Kanj, Spring 2012 - 20137

    Write a program that prompts the user to enter an integer. If the number is amultiple of5, printHiFive. If the number is divisible by2, printHiEven.

  • 7/28/2019 Chapter 3 Selections

    8/34

    Two-Way if statement

    The actions that a two-way ifstatement specifies differ basedon whether the condition istrueor false.

    Fatima Kanj, Spring 2012 - 20138

  • 7/28/2019 Chapter 3 Selections

    9/34

    Example:

    To check whether an integer is even or odd:

    Fatima Kanj, Spring 2012 - 20139

  • 7/28/2019 Chapter 3 Selections

    10/34

    Nested if Statement The statement in an ifor if ... elsestatement can be any legal Java

    statement, including another ifor if ... elsestatement.

    The inner ifstatement is said to benestedinside the outer ifstatement. The nested ifstatement can contain another ifstatement; in fact, there is no limit to the

    depth of the nesting.

    The nested ifstatement can be used to implement multiplealternatives.

    Fatima Kanj, Spring 2012 - 201310

  • 7/28/2019 Chapter 3 Selections

    11/34

    Example:

    Finding the letter grade of a given score:

    Fatima Kanj, Spring 2012 - 201311

  • 7/28/2019 Chapter 3 Selections

    12/34

    Common Errors in Selection Statements

    Common Error 1: Forgetting Necessary Braces

    Common Error 2: Semicolon at the if Line

    Fatima Kanj, Spring 2012 - 201312

  • 7/28/2019 Chapter 3 Selections

    13/34

    Common Errors in Selection Statements

    Common Error 3: Redundant Testing of Boolean Values

    Common Error 4: Dangling else Ambiguity (Theelseclause alwaysmatches the most recent unmatched ifclause in the same block).

    Fatima Kanj, Spring 2012 - 201313

  • 7/28/2019 Chapter 3 Selections

    14/34

    Common Errors in Selection Statements

    Common Error 5: Assignment operator (=); if the value of even wasfalse, this will assign true to even and prints It is even. to the console.

    Fatima Kanj, Spring 2012 - 201314

  • 7/28/2019 Chapter 3 Selections

    15/34

    switch Statements

    To replace nested if statements to handle multiple conditionsefficiently.

    The syntax is as follows:

    Fatima Kanj, Spring 2012 - 201315

  • 7/28/2019 Chapter 3 Selections

    16/34

    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.

    Fatima Kanj, Spring 2012 - 201316

  • 7/28/2019 Chapter 3 Selections

    17/34

    switch Example

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

    Fatima Kanj, Spring 2012 - 201317

  • 7/28/2019 Chapter 3 Selections

    18/34

    Conditional Expressions

    boolean-expression ? expression1 : expression2;

    The result of this conditional expression isexpression1 ifboolean-expression is true; otherwise the result isexpression2.

    Fatima Kanj, Spring 2012 - 201318

  • 7/28/2019 Chapter 3 Selections

    19/34

    Logical Operators or Boolean Operators

    Used to combine several conditions.

    Fatima Kanj, Spring 2012 - 201319

  • 7/28/2019 Chapter 3 Selections

    20/34

    Truth Table for Operator !

    Fatima Kanj, Spring 2012 - 201320

  • 7/28/2019 Chapter 3 Selections

    21/34

    Truth Table for Operator &&

    Fatima Kanj, Spring 2012 - 201321

  • 7/28/2019 Chapter 3 Selections

    22/34

    Truth Table for Operator ||

    Fatima Kanj, Spring 2012 - 201322

  • 7/28/2019 Chapter 3 Selections

    23/34

    Truth Table for Operator

    Fatima Kanj, Spring 2012 - 201323

  • 7/28/2019 Chapter 3 Selections

    24/34

    Boolean Operators: Example 1 Write a program that checks whether a number is divisible by2and3, by2

    or 3, and by 2or 3but not both:

    Fatima Kanj, Spring 2012 - 201324

  • 7/28/2019 Chapter 3 Selections

    25/34

    Boolean Operators: Example 2

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

    Fatima Kanj, Spring 2012 - 201325

  • 7/28/2019 Chapter 3 Selections

    26/34

    Unconditional vs. Conditional Boolean Operators

    Unconditional Operators (& , | ) They work exactly the same as the&&and || operators with

    one exception: the&and | operators always evaluate bothoperands.

    Conditional (&&, ||)

    When evaluatingp1 && p2, Java first evaluatesp1and thenevaluatesp2 ifp1 istrue; ifp1 is false, it does not evaluatep2.

    Fatima Kanj, Spring 2012 - 201326

  • 7/28/2019 Chapter 3 Selections

    27/34

    Formatting Console Output

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

    A better way: Use theprintfmethod.

    where format is a string that may consist of substrings and formatspecifiers. A format specifier specifies how an item should bedisplayed.

    Fatima Kanj, Spring 2012 - 201327

  • 7/28/2019 Chapter 3 Selections

    28/34

    Frequently Used Specifiers

    Fatima Kanj, Spring 2012 - 201328

  • 7/28/2019 Chapter 3 Selections

    29/34

    Formatting Output

    Fatima Kanj, Spring 2012 - 201329

  • 7/28/2019 Chapter 3 Selections

    30/34

    Example:

    Fatima Kanj, Spring 2012 - 201330

  • 7/28/2019 Chapter 3 Selections

    31/34

    Remark

    By default, the output is right justified. You can put theminus sign (-) in the specifier to specify that the item isleft justified in the output within the specified field.

    Fatima Kanj, Spring 2012 - 201331

  • 7/28/2019 Chapter 3 Selections

    32/34

    Evaluation of Expressions

    The expressions in parentheses are evaluated first.

    When evaluating an expression without parentheses, Javaoperators 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

    Fatima Kanj, Spring 2012 - 201332

  • 7/28/2019 Chapter 3 Selections

    33/34

    Operator Precedence

    Fatima Kanj, Spring 2012 - 201333

  • 7/28/2019 Chapter 3 Selections

    34/34

    Example

    Applying the operator precedence and associativity rule, theexpression 3 + 4 * 4 > 5 * (4 + 3) - 1 is evaluated as follows:

    3 + 4 * 4 > 5 * ( 4 + 3) - 1

    3 + 4 * 4 > 5 * 7 1

    3 + 16 > 5 * 7 1

    3 + 16 > 35 1

    19 > 35 1

    19 > 34

    f al se

    (1) inside arentheses firs

    (2) multiplication

    (3) multiplication

    (4) addition

    (5) subtraction

    (6) greater than

    Fatima Kanj, Spring 2012 - 201334