In Java, we found two type of decision controlling instructions – if-else statements and switch statements. Here we will discuss about if-else part of Java.

We put conditions into if/else-if statements. Condition gets Boolean expression which returns either true or false.

if statement

In Java, for decisions control we can use if statements.

Syntax –

if (condition){
statements;
statements;
}
 

Let’s see some coding examples…

public class code1 {
public static void main(String[] args) {
int a=10;
// a==10 it’s a condition,which statisfies
if (a==10) {
System.out.println(“a is 10”);
}
}
}
output

Let’s see some another coding examples…

public class code2 {
public static void main(String[] args) {
int a=10;
a++;
int b=a;
 
System.out.println(“a is “+a);
System.out.println(“b is “+b);
 
if (b!=10) {
System.out.println(“statement executed!!”);
}
}
}
output

nested if

It’s simply means if statement inside another If statement. When we have multiple conditions then sometime we can use nested if block. Let’s understand through an example…

Make a code using nested-if statements, which will response on those persons which age between 18-30.

import java.util.Scanner;
import java.util.Scanner;
 
public class code3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int age;
System.out.println(“Enter person age: “);
age=sc.nextInt();
if (age>18) {
// nested if block
if (age<30) {
System.out.println(“person valid!!”);
}
}
}
}
output

else statement

When if block’s condition not satisfies then else block get executed.

Syntax –

if (condition){
statements;
}
else{
statements;
}

Let’s understand through a coding example…

public class code4 {
public static void main(String[] args) {
int a=2;
if (a==1) {
System.out.println(“a is 1”);
} else {
System.out.println(“a is 2”);
}
}
}
output

Let’s understand this with another example…

public class code5 {
public static void main(String[] args) {
float amnt=12.3F;
int b=30;
System.out.println(“amnt is “+amnt);
if (b==31) {
amnt=15.6F;
} else {
amnt=21.3F;
}
System.out.println(“after change, amnt now “+amnt);
}
}
output

else if statement

When we want to handle multiple condition, then we can use else-if statements. Let’s understand use case of else-if statements.

Syntax –

if (condition){
statements;
}
else if (condition){
statements;

Make a code, where it takes user’s phone number country code. There has three countries – India (91), Pakistan (92), and Bangladesh (93). According to user’s input it will print user’s country.

import java.util.Scanner;
 
public class code6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int counCode=0;
System.out.println(“enter country code: “);
counCode=sc.nextInt();
if (counCode==91) {
System.out.println(“india”);
}
else if(counCode==92){
System.out.println(“pakistan”);
}
else if(counCode==93){
System.out.println(“bangladesh”);
}
}
}
output

nested if-else statement

when we want to handle multiple conditions then we can use nested if-else statements.

Let’s understand through below code example…

public class code7 {
public static void main(String[] args) {
int a=10;
int b=0;
System.out.println(“b is “+b);
if (a==10) {
if (a>30) {
b=2;
} else {
b=4;
}
}
System.out.println(“b is “+b);
}
}
output

Till now, we have seen if, else if, else statements using relational operators like ><>= != etc.

Now we will perform logical operations over if-else statements.

Logical AND [ && ] – if both inputs same then output same.

public class logiCalAnd {
public static void main(String[] args) {
int a=10;
// here, a==10 and a<20 both conditions true.
if (a==10 && a<20) {
System.out.println(“a is 10 and less than 20”);
}
}
}
output

Let’s understand logical AND through another example…

public class logiCalAnd2 {
public static void main(String[] args) {
float amnt=32.2f;
// 1st condition amnt==32.2f true but 2nd condition amnt<12.3f not true
if (amnt==32.2f && amnt<12.3f) {
System.out.println(“if block executed!!”);
}
else{
System.out.println(“else block executed!!”);
}
}
}
output

Logical OR [ || ] – if at-least one input gets true then output same.

public class logicalOR {
public static void main(String[] args) {
int a=20;
// here, a==10 not true but a==20 true. means 0+1=1
if (a==10 || a==20) {
System.out.println(“logical OR”);
System.out.println(“a is 20”);
}
}
}
output

Let’s understand logical OR through another example…

public class logicalOR2 {
public static void main(String[] args) {
float amnt=44.5f;
// amnt==32.4f and amnt<12.3f both not true
if (amnt==32.2f || amnt<12.3f) {
System.out.println(“logical OR”);
System.out.println(“if block executed!!”);
}
else{
System.out.println(“logical OR”);
System.out.println(“else block executed!!”);
}
}
}
output

Note – in Java, if-else statements we have working on integer, float values. But where does String? Let’s say, i have two strings called – ‘cat’ and ‘cat’ both are same? Can we checks this using if statement ? We can’t directly check two string using relational operator(==), for this we need to use equals() method.

Let’s understand through below code example

public class strCheck {
public static void main(String[] args) {
String a=”cat”;
String b=”cat”;
if (a.equals(b)) {
System.out.println(“string matched!!”);
}
else{
System.out.println(“string not matched!!”);
}
}
}
output

let’s take with different strings –

public class strCheck2 {
public static void main(String[] args) {
String a=”java”;
String b=”Java”;
if (a.equals(b)) {
System.out.println(“string matched!!”);
}
else{
System.out.println(“string not matched!!”);
}
}
}
output

for second case, look string variable ‘a’ first letter start with ‘j’ but another string  variable ‘b’ first letter start with ‘J’. that’s why strings are not get matched.