Java - Conditional Statements

Example 1: Compare two numbers using If

							  
public class IfExs {
	public static void main(String[] args) {

	        int marks = 76;

	        if (marks >= 40) {
	            System.out.println("Grade = Pass");
	        }

	        if (marks < 40) {
	        	System.out.println("Grade = Fail");
	        }

	}
}

							  

RESULT

Grade = Pass


Example 2: Compare two numbers using If

							  
public class IfElseExs {
	public static void main(String[] args) {
		
		int marks = 76;
	    
	        if (marks >= 40) 
	        {
	            System.out.println("Grade = Pass");
	        }
	        else
	        {
	        	System.out.println("Grade = Fail");
	        }
	}
}				  

							  

RESULT

Grade = Pass


Example 3: Compare two numbers using Else-If

							  
public class ElseIfExs {
	public static void main(String[] args) {
		
	int marks = 76;

	if (marks >= 90)
	{
	  System.out.println("Grade = A");
	} 
	else if (marks >= 80) 
	{
	  System.out.println("Grade = B");
	} 
	else if (marks >= 70) 
	{
	  System.out.println("Grade = C");
	} 
	else if (marks >= 60) 
	{
	  System.out.println("Grade = D");
	} 
	else 
	{
	  System.out.println("Grade = F");
	}

	}
}

							  

RESULT

Grade = C


Example 4: Compare two numbers using Else-If and Logical operators

							  
public class IfElseCondExs {
	public static void main(String[] args) {

		int age = 18;
		if(age>15&&age<=20)
		{
			System.out.println("Age is between 15 and 20");
		}
		else if(age>20&&age<=35){
			System.out.println("Age is between 21 and 35");
		}
		else if(age>35&&age<=55){
			System.out.println("Age is between 36 and 55");
		}
		else{
			System.out.println("Age is greater than 55");
		}

	}
}
							  
							  

RESULT

Age is between 15 and 20


Example 5: Compare two numbers using Else-If and Logical operators

							  
public class ElseIfLadderExs {
	public static void main(String[] args) {

        int test=2;

        if(test==1)
        {
            //This block will be executed only if "test" is equal to 1
            System.out.println("Hello");
        }
        else if(test==2)
        {
            //This block will be executed only if "test" is equal to 2
            System.out.println("Hi");

        }
        else if(test==3)
        {
            //This block will be executed only if "test" is equal to 3
            System.out.println("Good");
        }
        else
        {
            System.out.println("No Match Found");
        }
	}
}
 
							  

RESULT

Hi


Example 6: Compare two numbers using Switch-Case

							  
public class SwitchCaseExs {
	public static void main(String[] args) {

		int test=2;

		switch(test){
		case 1:
			System.out.println("Hello");
			break;
		case 2:
			System.out.println("Hi");
			break;
		case 3:
			System.out.println("Good");
			break;
		default:
			System.out.println("No Match Found");
			break;	
		}
	}
}
 
							  

RESULT

Hi


Example 7: Compare two numbers using Switch-Case

							  
public class ArraysExs {
	public static void main(String[] args) {
		int month = 8;
		switch (month) {
		case 1:  
			System.out.println("January"); 
			break;
		case 2:  
			System.out.println("February"); 
			break;
		case 3:  
			System.out.println("March"); 
			break;
		case 4:  
			System.out.println("April"); 
			break;
		case 5:  
			System.out.println("May"); 
			break;
		case 6:  
			System.out.println("June"); 
			break;
		case 7:  
			System.out.println("July"); 
			break;
		case 8:  
			System.out.println("August"); 
			break;
		case 9:  
			System.out.println("September"); 
			break;
		case 10: 
			System.out.println("October"); 
			break;
		case 11: 
			System.out.println("November"); 
			break;
		case 12: 
			System.out.println("December"); 
			break;
		default: 
			System.out.println("Invalid month.");
			break;
		}
	}
}
 
							  

RESULT

August


Example 8: Compare two numbers using Switch-Case

							  
public class ArraysExs {
	public static void main(String[] args) {
	int month = 2;
        int year = 2000;
        int numDays = 0;

        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                numDays = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                numDays = 30;
                break;
            case 2:
                if (year % 400 == 0)
                    numDays = 29;
                else
                    numDays = 28;
                break;
            default:
                System.out.println("Invalid month.");
                break;
        }
        System.out.println("Number of Days = " + numDays);
		}
}
 
							  

RESULT

Number of Days = 29


Example 9: Printing number using jumping statements (break)

							  
public class JumpBreakExs {
	public static void main(String[] args) {
		for(int i =0; i < 10 ; i++)
		{
			
			if(i == 3)
			{
				break;
			}
			System.out.println("num is : " + i);
		}
	}
}
 
							  

RESULT

num is : 0
num is : 1
num is : 2


Example 10: Printing number using jumping statements (continue)

							  
public class ArraysExs {
	public static void main(String[] args) {
		for(int i =0; i < 10 ; i++)
		{
			
			if(i == 3)
			{
				continue;
			}
			System.out.println("num is : " + i);
		}
	}
}
							   
							  

RESULT

num is : 0
num is : 1
num is : 2
num is : 4
num is : 5
num is : 6
num is : 7
num is : 8
num is : 9


BytecodeTechnosolutions.com | Raju