Quick Summary of Conditional Statements in Java

By John Paul Mueller

Using a simple if statement allows your Java application to decide whether to take a particular action. Relying on the if…else statement lets your application choose between two actions. Here are the key points you should remember about condition statements in Java:

  • A conditional statement consists of a condition and a task. When the condition is true, the application performs the task.

  • The condition portion of a conditional statement is also called an expression.

  • Every conditional task could be performed by using a simple if statement, but using other conditional statement forms makes your application less prone to errors and also reduces the amount of typing you perform.

  • The if…else statement lets your application choose between two courses of action depending on whether the expression you provide is true or false.

  • Use the switch statement to allow the user to choose from a list of items.

  • Rely on the default clause to provide a default action for your application when the user doesn’t provide useful input.

And here is the tech talk you should know for working with conditional statements in Java:

  • bug: An error in the application code that causes your application to behave in a way that you didn’t anticipate.

  • conditional statement: A programming construct that consists of a condition (expression) and a task. When the condition is true, the application performs the task.

  • expression: A mathematical description of the condition that you want the application to check. An expression normally includes a logical operator and always provides a Boolean output.

  • nesting: The act of combining multiple levels of statement into a single super statement. The application executes upper levels of the statement first and then drills down into lower levels. Use nesting to make complex decisions where one decision affects several others.