M

Introduction to Java Programming and Data Structures - Chapter 3 Selections

Selections in Java Programming

The boolean Type and Relational Operators

  • Purpose: Often, programs require comparing two values (e.g., checking if integer i is greater than integer j).

  • Relational Operators: Java provides six comparison operators, also known as relational operators, used to compare two values.

  • Result: The outcome of a comparison operation is a boolean value, which can be either true or false.

    • Example: boolean b = (1 > 2); would result in b being false.

  • List of Relational Operators:

    • < (less than)

      • Mathematics Symbol: <

      • Example (radius is 5): radius < 0

      • Result: false

    • <= (less than or equal to)

      • Mathematics Symbol: \le

      • Example (radius is 5): radius <= 0

      • Result: false

    • > (greater than)

      • Mathematics Symbol: >

      • Example (radius is 5): radius > 0

      • Result: true

    • >= (greater than or equal to)

      • Mathematics Symbol: \ge

      • Example (radius is 5): radius >= 0

      • Result: true

    • == (equal to)

      • Mathematics Symbol: =

      • Example (radius is 5): radius == 0

      • Result: false

    • != (not equal to)

      • Mathematics Symbol: \ne

      • Example (radius is 5): radius != 0

      • Result: true

One-Way if Statements

  • Syntax:
    java if (boolean-expression) { statement(s); }

  • Execution Flow: If the boolean-expression evaluates to true, the statement(s) inside the if block are executed. If it evaluates to false, the statement(s) are skipped.

  • Flowchart: A diamond shape represents the boolean-expression. If true, flow goes to statements; if false, it bypasses them.

  • Example: java if (radius >= 0) { area = radius * radius * PI; // PI represents a constant like 3.14159 System.out.println("The area for the circle of radius " + radius + " is " + area); }

    • Here, if radius is greater than or equal to 0, the area is calculated and printed.

  • Notes on Syntax: While braces {} are generally recommended for readability and to avoid potential errors, for a single statement, they are optional.

    • Correct with braces: if (i > 0) { System.out.println("i is positive"); }

    • Equivalent without braces for a single statement: if (i > 0) System.out.println("i is positive");

    • Incorrect (semicolon after if): if (i > 0); { System.out.println("i is positive"); } (This creates an empty statement for the if, and the print statement is always executed, which is a logic error).

  • Demo Example (SimpleIfDemo): A program that prompts the user for an integer and prints

Selections in Java Programming
The boolean Type and Relational Operators
  • Purpose: Compare two values.

  • Relational Operators: Six operators used for comparison.

  • Result: A boolean value (true or false).

    • Example: boolean b = (1 > 2); results in b being false.

  • List of Relational Operators:

    • &lt; (less than) - Math: &lt;

    • &lt;= (less than or equal to) - Math: \le

    • &gt; (greater than) - Math: &gt;

    • &gt;= (greater than or equal to) - Math: \ge

    • == (equal to) - Math: =

    • != (not equal to) - Math: \ne

One-Way if Statements
  • Syntax:

    java if (boolean-expression) { statement(s); }

  • Execution Flow:

    • If boolean-expression is true, statement(s) execute.

    • If boolean-expression is false, statement(s) are skipped.

  • Key Points on Syntax:

    • Braces {} are recommended for readability, required for multiple statements.

    • Optional for a single statement:

      • if (i > 0) System.out.println("i is positive");

    • Common Error: Do not place a semicolon after the if condition, e.g., if (i > 0); will lead to logic errors.