What is Java If Else ? How it Works ?

What is Java If Else ? How it Works ?


When you’re programming, it’s common to write code that should only be executed when a certain condition is met.

For instance, you may want an order to be placed on your e-commerce site only if a user has submitted their address. Or you may operate a coffee store and want to give a user a discount on their coffee if they have ordered more than five coffees in the last week.

In programming, we use conditional statements for this purpose. Conditional statements execute a specific block of code based on whether a condition evaluates to true or false. In Java, the if...else statement is a control flow statement that allows you to execute a block of code only if a certain condition is met.

This tutorial will explore how to use the if...else statement in Java, and explore a few examples of the if...else statement being used in a Java program.

Java If Statement

The most basic conditional statement is the if statement. if statements evaluate whether a statement is equal to true or false, and will only execute if the statement is equal to true. If the statement evaluates to false, the program will skip over the if statement and continue running the rest of the program.