Working with Statements

Statements are similar to sentences in the English language.

A statement tells Java to perform an action. It can include one or more expressions.

There are two types of statements in Java: * Simple Statement * Compound Statement

Understanding Simple Statements

Simple statements are the actions your program performs.

  • Assertion Statement
  • Break Statement
  • Continue Statement
  • Empty Statement
  • Expression Statement
  • Return Statement and
  • Throw Statement

    • A simple statement always ends with a semicolon.
    • The example shown here is an expression statement. Therefore, it is terminated with a semicolon.

      System.out.println("Hello, world!");
      

Understanding Compound Statements

Compound statements help you group simple statements and control them.

  • Block Statement
  • Do-While Statement
  • For Statement
  • If Statement
  • Switch Statement
  • Synchronized Statement
  • Try Statement and
  • While Statement

    • A compound statement does not require a semicolon.
    • An exception to this is the Do-While Statement.
    • Compound statements are usually associted with blocks.