Chapter 3 - Java for Beginners Course

If-Then-Else Statement

A second common case is when a given condition is true, we want to execute a certain block of code (same as before), but also want to execute some other block of code when the condition is false. The if-then-else statement allows us to do this and its syntax looks as follows:

if (boolean condition is true) {
	// then execute this block of code
} else {
	// execute this block of code if the condition is false

For example:

int age = 19;
double discount = 1d;

if (age < 18) {
    discount = 0.5d;
    System.out.println("The age is under 18");
} else {
    discount = 0d;
    System.out.println("The age is 18 or over");

System.out.println("The discount to use is: " + discount);


The age is 18 or over
The discount to use is: 0.0

Analysing the if-else statement example

In this case, the age variable has a value of 19, hence the condition in the if statement (age < 18) is false. This means that the first block of code won’t be executed.

However, because we have an else statement, the block of code after it will be executed, and as a result the message The age is 18 or over is printed out and the discount variable is given a value of 0.

Code in GitHub

Get the code for this tutorial using the links below.

Project Repo
Download code for this step
Main class for this step

This is a list of recommended tutorials or courses that might be useful before starting this one.

Welcome to the Course!
Course Introduction
Chapter 1 - Building Blocks
Quick introduction to Java Variables Classes And Objects Class Example - Defining a class Object Examples - Creating instances Java Application Example - Running our first app Accessing class members - The dot operator Packages - Organizing the code
Chapter 2 - Primitives and Operators
Primitives Arithmetic Operators Assignment Operator Unary Operators Equality and Relational Operators Conditional Operators
Chapter 3 - Statements and Control Flow
Expressions Statements If-Then Statement If-Then-Else Statement More If Statements Switch Statement While and Do-While Statements For Statement Branching Statements Exception Handling
Chapter 4 - Code Example
Example Project - A Simple Vending Machine Adding money Delivering Items Giving Change
Chapter 5 - Classes and Interfaces
Introduction Access Level Modifiers Class Declaration - Class, Methods and Fields Class Declaration - Constructors Inheritance Basics Inheritance - Constructors Inheritance - Methods and Fields Polymorphism Abstract Classes and Methods Interfaces Static Class Members Class Composition Final Classes and Class Members Generic Classes
Chapter 6 - Base Object Behaviors
Introduction Type Comparison Type Casting Object Equality - The Contract Object Equality - Common Pitfalls Object String Representation Garbage Collection Object Comparison Primitive Wrappers and Autoboxing
Chapter 7 - Data Structures
Introduction Arrays - Declaration and Creation Arrays - Basic Operations Core Collection Interfaces List and ArrayList - Basic Operations ArrayList Internals Introduction to Hash Tables Map and HashMap - Basic Operations Set and HashSet - Basic Operations
Chapter 8 - Anonymous classes and lambdas
Introduction Filtering a List Anonymous Classes Lambdas Built-in Functional Interfaces
Chapter 9 - Streams
Introduction Creating Streams Intermediate Operations Terminal Operations