Nested If Statements - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Nested If Statements

Description:

Act as documentation of your intent. Make code easier to read ... Else If Idiom. Using the else-if idiom the second if statement immediately follows the else ... – PowerPoint PPT presentation

Number of Views:1210
Avg rating:3.0/5.0
Slides: 11
Provided by: swea9
Category:

less

Transcript and Presenter's Notes

Title: Nested If Statements


1
Nested If Statements
  • Computer Science
  • Chapter 5

2
Nested if Statements
  • Nested if statements
  • allow you to combine multiple if statements
  • allow more complex decision making in your program

3
(No Transcript)
4
Braces In Nested Statements
  • Braces can
  • Act as documentation of your intent
  • Make code easier to read
  • Help you group if else statement as you intended

5
Deeply Nested Statements
  • Deeply nested statements become very confusing.
  • JOptionPane provides an easy way to get input
    from the user
  • Use import statement
  • Import javax.swing.JOptionPane

6
Else If Idiom
  • Using the else-if idiom the second if statement
    immediately follows the else statement
  • If (choice 1)
  • System.out.println(Running..)
  • Else if (choice 2 )
  • system.out.println(Hopping)

7
The Switch Statement
  • The switch statement is specifically designed for
    choosing among multiple options.
  • Advantages
  • Additional cases can be added easily to expand
    number of choices
  • Easier to read than nested statements
  • Easier to run and maintain

8
Ternary Operators
  • Ternary Operator is a concise alternative to if
    statement
  • Use when you want to assign a value based on a
    test that evaluates to a Boolean value

9
Ternary Syntax
  • Syntax is
  • Variable condition ? expression1 expression2
  • Condition tested
  • If condition evaluates to true, statement
    evaluates to the first expression
  • If condition evaluates to false, statement
    evaluates to the second expression

10
Default Choice
  • Default tag optional
  • Good programming practice to include a default
  • Impossible to get to defaultput in an error
    handling code just in case
Write a Comment
User Comments (0)
About PowerShow.com