Basic Statements: Sequential and Conditional Structures

Basic Statements: Sequential and Conditional Structures

目录

  • Basic Statements: Sequential and Conditional Structures
  • Sequential Statements
  • Conditional Statements
    • if Statements
      • Single-Branch if Statement (Not Recommended, Generally if Statements Should Include an else Clause)
      • Dual-Branch if Statement
      • Multi-Branch if Statement
      • Nested if Statements

Sequential Statements

  • Sequential statements are the most fundamental in Java (and also the simplest control structure in all programming languages). Unless otherwise specified, they execute one after another.

  • The simplest algorithm structure is the sequential structure.

  • When no other statements interfere, sequential statements execute from top to bottom. Example:

  • The example executes commands in a top-down order, outputting each statement sequentially.

Conditional Statements

  • Conditional statements include if and switch statements.

if Statements

Single-Branch if Statement (Not Recommended, General if Statements Should Include an else Clause)

Syntax:
    if (Boolean Expression) {
      // Execute when Boolean Expression is true
}

Example:

  • When strings are equal, the Boolean expression is true, so the code inside the if block (System.out.println("Equal")) is executed. It outputs "Equal" first, then proceeds to print "End".
  • When strings are not equal, the Boolean expression is false, so the code inside the if block is skipped, and it directly proceeds to print "End".

Dual-Branch if Statement

The dual-branch if statement has two paths: if and else. If the Boolean expression is true, the code inside the if block is executed; otherwise, the code inside the else block is executed.

Syntax:
    if (Boolean Expression) {
      // Execute when Boolean Expression is true
} else {
      // Execute when Boolean Expression is false
}

Example:

  • When strings are not equal, the Boolean expression is false, so the code inside the if block is skipped, and the code inside the else block is executed, printing "Not Equal", then proceeding to print "End".

Multi-Branch if Statement

Sometimes two branches are not enough to express what we need, so the multi-branch if statement comes into play. If Boolean Expression 1 is true, the code in side the if block is executed. If Boolean Expression 2 is true, the code inside the first else if block is executed, and so on. Finally, an else block can act as a fallback (optional based on context). Once any if or else if condition is true, the remaining else if and else blocks are skipped.

Syntax:
    if (Boolean Expression 1) {
      // Execute when Boolean Expression 1 is true
} else if (Boolean Expression 2) {
      // Execute when Boolean Expression 2 is true
} else if (Boolean Expression 3) {
      // Execute when Boolean Expression 3 is true
}
...
else {
      // Execute when none of the above Boolean Expressions are true
}

Example:

  • This example is a simple grade evaluation. When the score is between 100 and 90, the grade is A. When the score is between 89 and 80, the grade is B, and so on. Finally, an else block ensures that any value out side these ranges outputs "Invalid Score".

Nested if Statements

  • An if statement nested within another if statement.
if (Boolean Expression 1) {
      // Execute when Boolean Expression 1 is true
            if (Boolean Expression 2) {
      // Execute when Boolean Expression 2 is true
    }
}
  • Essentially, both Boolean Expression 1 and Boolean Expression 2 must be true for the code inside the inner if block to execute.

Tags: java programming Control Structures Conditional Statements Sequential Statements

Posted on Thu, 14 May 2026 19:23:27 +0000 by BrettCarr