Introduction to Computing Using Java - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Computing Using Java

Description:

Introduction to Computing Using Java Branching: if and if/else – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 15
Provided by: Michael4071
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Computing Using Java


1
Introduction to Computing Using Java
  • Branching if and if/else

2
Branching/Selection
  • Apart from sequential execution, branching is
    another important programming concept.
  • It makes our programs go through different
    statements and produce different results.
  • Two categories in Java
  • if-statement
  • switch-statement (not discussed here)

3
How Does it Work?
  • For example
  • ??????,????? ??????
  • Java equivalent
  • boolean raining cloudThickness gt 0.8
  • if (raining)
  • System.out.println(Bring umbrella)
  • else
  • System.out.println(Hands free)

4
if-(then)-else
  • Dont invent the keyword then!
  • if (condition)
  • true_statement
  • else
  • false_statement
  • next_statement
  • condition is a boolean expression.
  • true_statement and false_statement may be
    compound statements.

5
if-(then)-else
  • Dont invent the keyword then!
  • if (condition)
  • true_statement
  • else
  • false_statement
  • next_statement
  • If the condition is true, true_statement part is
    executed, false_statement otherwise.
  • In both cases, the program continues (joins) at
    the next_statement.

6
Optional else-part
  • The following is also valid
  • if (condition)
  • true_statement
  • next_statement
  • In this case, if the condition is true, the
    true_statement part will be executed.Otherwise,
    nothing happens.
  • The program always continues at the
    next_statement.

7
Dice Example Again
  • int diceOne 5, diceTwo 6, diceThree 6
  • if ((diceOne diceTwo) (diceTwo
    diceThree))
  • System.out.println(Eat all!)
  • else
  • if (diceOne diceTwo diceThree lt 10)
  • System.out.println(Small!)
  • else
  • System.out.println(Big!)
  • Nested if-statements.

One statement
8
Successive else-ifs
  • int marks
  • char grade
  • if (marks gt 80)
  • grade A
  • else
  • if (marks gt 70)
  • grade B
  • else
  • if (marks gt 60)
  • grade C
  • else
  • if (marks gt 50)
  • grade D
  • else
  • grade E
  • int marks
  • char grade
  • if (marks gt 80) grade A
  • else if (marks gt 70) grade B
  • else if (marks gt 60) grade C
  • else if (marks gt 50) grade D
  • else grade E
  • Just a matter of style.

9
Whose else?
  • double GPA
  • if (GPA gt 3.5)
  • if (GPA gt 4.0)
  • System.out.println(Distinction)
  • else
  • System.out.println(Credit)
  • Which if does this else belong to?

10
Whose else?
  • double GPA
  • if (GPA gt 3.5)
  • if (GPA gt 4.0)
  • System.out.println(Distinction)
  • else
  • System.out.println(Credit)
  • Which if does this else belong to?

11
Whose else?
  • double GPA
  • if (GPA gt 3.5)
  • if (GPA gt 4.0)
  • System.out.println(Distinction)
  • else
  • System.out.println(Credit)
  • Which if does this else belong to?
  • Correct!

12
Solution to Dangling else Issue
  • Rule of thumb
  • An else-part attaches to the nearest available
    if, which has not already been matched.
  • double GPA
  • if (GPA gt 3.5)
  • if (GPA gt 4.0)
  • System.out.println(First Hon)
  • else
  • if (GPA gt 3.7)
  • System.out.println(Second Upper)
  • else
  • System.out.println(Second Lower)

This pair of braces is a must!
13
Omitting then-statement
  • How can we omit the then-part and keep the
    else-part?
  • Empty then-statement
  • if (condition)
  • else
  • System.out.println(Is it OK?)
  • Use of boolean negation (better approach).
  • Left to you as an exercise.

14
End Note
  • Readings and References
  • Section 2.6 The Scanner class from Java
  • Section 3.6 Formatting output
  • Sections 5.1, 5.2 Conditionals (if/ else)
  • Exercise
  • 5.1, 5.2, 5.3, 5.4, 5.5, 5.10
  • Programming Projects
  • 5.10
Write a Comment
User Comments (0)
About PowerShow.com