Java If Loop

  1. Syntax
  2. if statement
  3. if...else statement
  4. if..elseif...else statement
  5. if...elseif...elseif statement
  6. if...if...if statement

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Syntax


if(condition) {
 
	// Statement if the condition is true..
}

if(condition) {
 
	// Statement-1 if the condition is true..
}else {
 
	// Statement-2 if the condition is false..
}

if(condition-1) {
 
	// Statement-1 if the condition-1 is true..
}else if(condition-2) {
 
	// Statement-2 if the condition-2 is true..
}else {
 
	// Statement-3 if both conditions are false..
}

if(condition-1) {
 
	// Statement-1 if the condition-1 is true..
}else if(condition-2) {
 
	// Statement-2 if the condition-2 is true..
}else if(condition-3) {
 
	// Statement-3 if the condition-3 is true..
}

if(condition-1) {
 
	// Statement-1 if condition-1 is true..}
if(condition-2) {
 
	// Statement-2 if condition-2 is true..}
if(condition-3) {
 
	// Statement-3 if condition-3 is true..}




B. if statement


If_Ex1.java

public class If_Ex1 {
	
	int a = 10;
	int b = 8;
	int c;
		
	void add() {
		
		c = a + b;
	}
	
	void check() {
		
		if (c >= a) {
			
			System.out.println("The Addition is : " + c);
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		If_Ex1 obj = new If_Ex1();
		obj.add();
		obj.check();
	}
}

Sample Output

The Addition is : 18




C. if...else statement


If_Ex2.java

public class If_Ex2 {
	
	boolean ans = true;
	
	void check() {
		
		if(ans == false) {
			
			System.out.println("The Answer is false");
		}
		else {
			
			System.out.println("The Answer is true");
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		If_Ex2 obj = new If_Ex2();
		obj.check();
	}
}

Sample Output

The Answer is true




D. if..elseif...else statement


If_Ex3.java

public class If_Ex3 {
 
	int a = 11;
	int b = 20;
	int c = 18;
	
	void check() {
		
		if(a>b && a>c) {
			
			System.out.println("A is Greater !!!");
		}
		else if(b>a && b>c) {
			
			System.out.println("B is Greater !!!");
		}
		else {
			
			System.out.println("C is Greater !!!");
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		If_Ex3 obj = new If_Ex3();
		obj.check();
	}
}

Sample Output

B is Greater !!!




E. if...elseif...elseif statement


If_Ex5.java

public class If_Ex5 {
	
	int a = 3;
			
	void check() {
		
		if(a == 1) {
			System.out.print("'A' is : 1");
		}else if(a == 2) {
			System.out.print("'A' is : 2");
		}else if(a == 3) {
			System.out.print("'A' is : 3");
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		If_Ex5 obj = new If_Ex5();
		obj.check();
	}
}

Sample Output

'A' is : 3




F. if...if...if statement


If_Ex4.java

public class If_Ex4 {
	
	int a = 11;
	int b = 20;
	int c = 18;
	
	void check() {
		
		if(a>b && a>c) {
			
			System.out.println("A is Greater !!!");
		}
		if(b>a && b>c) {
			
			System.out.println("B is Greater !!!");
		}
		if(c>a && c>b) {
			
			System.out.println("C is Greater !!!");
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		If_Ex4 obj = new If_Ex4();
		obj.check();
	}
}

Sample Output

B is Greater !!!






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com