If statement in java 7

Ads
 

If statement in java 7

This tutorial describes the if statement in java 7. This is one kind of decision making statement.

This tutorial describes the if statement in java 7. This is one kind of decision making statement.

If statement in java 7

This tutorial describes the if statement in java 7. This is one kind of decision making statement.

There are various way to use if statement with else -

If Statement :

 If statement contains one boolean condition and if it satisfied then it executes the if block statements otherwise it jumps out the if block.

Syntax :

if(condition) {
......
......//statements
}

If -Else Statement :

In if-else statement, if statement contains boolean condition and if it the condition is true the if block is executed otherwise the else block is executed.

Syntax :

if(condition) {
......
......//statements
}
else{
........
......//statements
}

Nested Ifs  statements :

In java, you can use nested if statements that means you can write if statement inside the if block.

Syntax :

if(condition1) {
if(condition2) {
....//statements
}
......
......//statements
}

Multiple Else-If statements :

In java, you can use multiple if-else statements as well as Else-If statements that means if you want to write if statement inside the else block then you can use else-if statement.

if(condition1) {
......
......//statements
}
else if(condition2){
........
......//statements
}
else{
........
......//statements
}

Example :

package decisionMaking;

class IfElseStatement {
	public static void main(String[] args) {
		int x = 20, y = 200;
		int max = 100;
		
		if (max > x) { // if statement
			System.out.println("max is greater than x.");
		} 
		else if (max > y) { // else if statement
			System.out.println("max is greater than x and y");
		} 
		else { // else statement
			System.out.println("max is less than x and y");
		}
	}
}

Output :

max is greater than x.

Ads

Ads