Home Java Jdk7 If statement in java 7



If statement in java 7
Posted on: December 22, 2012 at 12:00 AM
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.

Related Tags for If statement in java 7:


More Tutorials from this section

Ask Questions?    Discuss: If statement in java 7  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.