"if-then" Statements

This section describes 'if-then' statement, which is a decision-making statement that executes contained sub-statements only when the given 'boolean' expression is 'true'.

"if" statements have 2 forms: "if-then" statements and "if-then-else" statements. Let's look at "if-then" statements in this tutorial first.

What Is "if-then" Statement? - An "if-then" statement is a decision-making statement that executes the contained sub-statement only when the given "boolean" expression is "true".

Here is the syntax for an "if-then" statement. Note that "then" is not used a keyword used in the statement at all.

if (condition_expression) contained_statement

The contained sub-statement can be a simple single statement, or a complex statement block. Here is a sample program that shows you how to use "if-then" statements:

/* IfThenStatementTest.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
class IfThenStatementTest {
   public static void main(String[] arg) {
      java.io.PrintStream out = System.out;

      out.println("\"if-then\" statement containing a single statement:");
      boolean isNewYear = true;
      if (isNewYear) out.println("   Happy New Year!");

      out.println("\"if-then\" statement containing a statement block:");
      boolean hasError = true;
      if (hasError) {
         out.println("   Open the log file.");
         out.println("   Write an error message.");
         out.println("   Close the log file.");
      }
   }
}

If you compile and run the above program, you will see:

herong> java IfElseStatementTest.java

"if-then" statement containing a single statement:
   Happy New Year!
"if-then" statement containing a statement block:
   Open the log file.
   Write an error message.
   Close the log file.

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

Control Flow Statements

 What Is Control Flow Statement

"if-then" Statements

 "if-then-else" Statements

 Nested "if-then-else" Statements

 "switch" Statements

 Fall-Through Behavior of "switch" Statements

 Basic "for" Statements

 Enhanced "for" Statements

 "while" Statements

 "do" Statements

 "break" Statements

 Labeled "break" Statements

 "continue" Statements

 Labeled "continue" Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB