Java if statement is checking a condition, then do the action accordingly. Where In Java if-else statement, it makes more correctly and broadway to get logic.
Every programming language or application without logic can’t be complete. The if-else condition statements provide logic in the program.
If conditions statement decision has to states: True or False, Boolean data type. It checks conditions in runtime.
This tutorial you will learn about If, if-else and some important condition with these statements with examples.
Syntax
Here is the simple syntax, pass the expression which is an evaluate state is true or false if true then go inside the code statements.
if (expression) { // statements }
Java if statement Example
A simple example number is positive or not. Using an int variable data type and print results in the console.
class Hello { public static void main(String[] args) { int a = 99; if (a > 0) { System.out.println("Positive number"); } System.out.println("I am always executed."); } }
Output: Positive number
I am always executed.
How if the statement condition works?
Here is an image of exploration, how’s it works.
Java if-else statement Example
Using else block with if condition gives you cover the broad scenario of logic, For example, examination passing mark is 40 out of 100, so you can give a logic statement if less than 40 then always failed.
Let’s see the example and code of the same.
class Hello { public static void main(String[] args) { int marks = 78; if (marks > 40) { System.out.println("You are passed The exam! Congratulation"); } else { System.out.println("Sorry! you are failed in exam."); } } }
Output: You are passed The exam! Congratulation
How does the if-else statement condition work?
Java if else shorthand
How you can write a Java if conditions statement in shorted form?
Using the ternary operator, you can do it, see below code.
class Hello { public static void main(String[] args) { int marks = 78; String msg = ((marks > 40)? "Passed" : "Failed"); System.out.println(msg); } }
Output: Passed
Java if statement multiple conditions
Here are many ways to do multiple if the conditions., choose as per your requirements.
#First Ways example
if(ConditionOne && ConditionTwo && ConditionThree) { //Code to execute }
#Second Wayt to do it
if(ConditionOne) { if(ConditionTwo ) { if(ConditionThree) { //Code to execute } } }
Java if statement strings
The above example all using int (Integer), so how you will do if condition with strings? Check out this example for java if statement with Strings.
class Hello { public static void main(String[] args) { String s = "Hello"; if(s.equals("Hello")) { System.out.println("String is the same"); } } }
Output: String is the same
This not only where if-else finished there is more statement related to it –
- if statement
- if-else statement
- if-else-if ladder
- nested if statement
We will cover the remaining topic in later tutorials.
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1Java version 11
All Java if statement and Java if-else statement Examples are in Java 11, so it may change its different from Java 9 or 10 or upgraded versions.