- Forward


Conditions and Decisions
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Logical Operators:
    • Have Boolean operands and evaluate to a Boolean
  • Relational Operators:
    • Typically have numeric operands and evaluate to a Boolean
Motivation
Back SMYC Forward
  • An Observations:
    • Some algorithms require a decision to be made before steps are performed (in other words, the steps that are performed depend on the value of one or more variables)
  • Operationalizing this Idea in a Program:
    • Step 1: Evaluate a Boolean expression
    • Step 2: Base what happens next on the evaluation
Two Conditional Statements in Java
Back SMYC Forward
  • The switch Statement
    • Used to "choose" from multiple alternatives
  • The if Statement
    • Used for almost everything else
The switch Statement
Back SMYC Forward

    The switch statement has the following syntax Click here for information. :

    switch (variable) { [case value: statement... [break;]]... [default: statement... [break;]] }

The switch Statement (cont.)
Back SMYC Forward

An Example

javaexamples/basics/SwitchExample.java (Fragment: 0)
 
The if Statement
Back SMYC Forward

    The if statement has the following syntax Click here for information. :

    if (boolean_expression) statement [else statement]

The if Statement (cont.)
Back SMYC Forward
  • The statement:
    • Can be a simple statement terminating with a ;
    • Can be a block statement (i.e., 0 or more simple statements enclosed in {})
  • White Space:
    • Indenting and white space are not "relevant" syntactically
    • Indenting and white space improve readability stylistically
Best Practices Involving if Statements
Back SMYC Forward
  • Do Not Use "Empty" if Clauses:
    • Change the condition instead (using rules of inference)
  • Do Not Use "Empty" else Clauses:
    • Omit the clause
    • Include a comment if it clarifies the situation
  • Remember to Have a Default:
    • Either initialize to the default or have an else clause
Eliminating Empty if Clauses
Back SMYC Forward
  • An Example of an Empty if Clause:
    • if (value >= 0) { } else { System.out.print("-"); }
  • The Fix:
    • if (value < 0) { System.out.print("-"); }
Defaults in if Statements
Back SMYC Forward
  • Initializing to the Default:
    • result = "Pass"; if (grade < 60) result = "Fail";
  • Including an else Clause:
    • if (grade < 60) result = "Fail"; else result = "Pass";
Styles of if Statements
Back SMYC Forward
  • A Condensed Style:
    • javaexamples/basics/IfExample1.java (Fragment: 0)
       
  • An Expanded Style:
    • javaexamples/basics/IfExample1.java (Fragment: 1)
       
Nested if Statements
Back SMYC Forward

The statement Can Be Another if

javaexamples/basics/IfExample2.java (Fragment: 0)
 
Nested if Statements (cont.)
Back SMYC Forward

A Condensed Style for "Nesting"

javaexamples/basics/IfExample2.java (Fragment: 1)
 
Nested if Statements (cont.)
Back SMYC Forward

Multiple Nested if Statements

javaexamples/basics/IfExample3.java (Fragment: 1)
 
Nested if Statements (cont.)
Back SMYC Forward

A Condensed Style for Multiple Nested ifs

javaexamples/basics/IfExample3.java (Fragment: 0)
 
Nested if Statements (cont.)
Back SMYC Forward

A switch-Like Style

javaexamples/basics/IfExample4.java (Fragment: 0)
 
Java vs. Python - Important Differences
Back SMYC Forward
  • Parentheses:
    • In Java, the boolean expression must be in parentheses
  • Colons:
    • In Python, the Boolean expression (and the else) are followed by a colon
  • Indenting:
    • In Python, indenting is syntactically significant
Java vs. Python - Important Differences (cont.)
Back SMYC Forward
  • elif:
    • In Python, there is an elif clause (that requires a subsequent else clause)
  • switch/match:
    • Python after v3.10 has a match statement that is similar to the switch statement in Java (without explicit break statements and case _ used in place of default)
There's Always More to Learn
Back -