If statement in Java

Java uses conditional statements to support decision-making. If condition is one of the most commonly used conditional statements. If the condition returns true, then it executes the true block. if the condition returns false, then the else block will be executed.



Types of If Statements

There are different types of if statement supported in java

  • If statement
  • If-Else statement
  • If-Else-If statement
  • Nested If statement


If statement

The Java If statement checks the condition. if condition is true, it executes the code block. Otherwise, the code block will be ignored from execution.

Syntax

if(condition) {
    // block of statements
}

Example 1

package com.yawintutor;

public class IfStatement {
	public static void main(String[] args) {
		int a = 6;
		int b = 3;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);

		if (a > b) {
			System.out.println("a is greater than b");
		}

		System.out.println("End of program");
	}
}

Output

a value is 6
b value is 3
a is greater than b
End of program

Example 2

package com.yawintutor;

public class IfStatement2 {
	public static void main(String[] args) {
		int a = 6;
		int b = 9;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);

		if (a > b) {
			System.out.println("a is greater than b");
		}

		System.out.println("End of program");
	}
}

Output

a value is 6
b value is 9
End of program


If-Else Statement

The Java If-Else statement has two blocks, if block and else block. If-Else statement checks the condition. If the condition is true, it will executes statement in if block. If the condition returns false, it will executes statements in Else block.

Syntax

if(condition) {
    //block of statements executed if the condition returns true
} else {
    //block of statements executed if the condition returns false
}

Example 1

package com.yawintutor;

public class IfElseStatement {
	public static void main(String[] args) {
		int a = 6;
		int b = 3;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);

		if (a > b) {
			System.out.println("a is greater than b");
		} else {
			System.out.println("b is greater than a");
		}

		System.out.println("End of program");
	}
}

output

a value is 6
b value is 3
a is greater than b
End of program

Example 2

package com.yawintutor;

public class IfElseStatement2 {
	public static void main(String[] args) {
		int a = 6;
		int b = 9;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);

		if (a > b) {
			System.out.println("a is greater than b");
		} else {
			System.out.println("b is greater than a");
		}

		System.out.println("End of program");
	}
}

Output

a value is 6
b value is 9
b is greater than a
End of program


If-Else-If Statement

The Java If-Else-If statement has multiple conditions and blocks. If the condition is true, the first block of the statement is executed. if not, checks the next condition. if the condition returns true, it will execute next block of statement. otherwise it will executes else block of statement

Syntax

if(condition1) {
    //block of statements executed if the condition1 returns true
} else if(condition2 ) {
    //block of statements executed if the condition2 returns true
} else {
    //block of statements executed if the condition2 returns false
}

Example

package com.yawintutor;

public class IfElseIfStatement {
	public static void main(String[] args) {
		int a = 6;
		int b = 6;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);

		if (a > b) {
			System.out.println("a is greater than b");
		} else if (b > a) {
			System.out.println("b is greater than a");
		} else {
			System.out.println("a is equal to b");
		}

		System.out.println("End of program");
	}
}

Output

a value is 6
b value is 6
a is equal to b
End of program


Nested-If Statements

Nested If statement contains if statements within another if statements.

package com.yawintutor;

public class NestedIfStatement {
	public static void main(String[] args) {
		int a = 1;
		int b = 2;
		int c = 3;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		System.out.println("c value is " + c);

		if (a > b) {
			if (a > c) {
				System.out.println("a is biggest number");
			} else {
				System.out.println("c is biggest number");
			}
		} else {
			if (b > c) {
				System.out.println("n is biggest number");
			} else {
				System.out.println("c is biggest number");
			}
		}

		System.out.println("End of program");
	}
}

Output

a value is 1
b value is 2
c value is 3
c is biggest number
End of program



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *