Basic Java – 10 || Conditions (If and Switch) in JAVA.

         To create a robust and error-free program we need to perform an action based on the certain conditions. In Java, this can be achieved using If and Switch statements. These statements are called conditional statements in JAVA.

If-else Statement

           Let’s understand first If statement. An if statement is used to take a decision based on the boolean values true and false. If a syntax or combination of syntax returns a true value, only then all the statement inside the if block gets executed. Otherwise Else block get executed.

if(condition)

{

       Statement(s)….

}

else

{

       Statement(s)….

}

if else statement
if else statement

Code Example:

public class IfElse {
	public static void main(String[] args) 
	{
		int result=1;
		if(result==1) // Executes when condition is True.
		{
			System.out.println("Value of result: "+result);
		}
		else          // Executes when condition is False.
		{
			System.out.println("Value of result not equal to 1.");
		}
	}
}
Related Links:

OOPs Concept:

Java Question And Answer:

If-else if-else ladder

Sometimes we have to take a decision between multiple conditions. In that case, we have to use if-else if-else ladder.

Code Example:

public class Ifelse {

	public static void main(String[] args) 
	{
		int result=1;
		if(result==1)
		{
			System.out.println("Value of result: "+result);
		}
		else if(result==2)
		{
			System.out.println("Value of result: "+result);
		}
		else if(result==3)
		{
			System.out.println("Value of result: "+result);
		}
		else
		{
			System.out.println("Invalid Value");
		}
	}
}

Switch-case

               Alternate of if-else statement is Switch-case. The functionality will remain same, the key difference is only that if-else statement works on the boolean value. The condition can be simple or complex.

                                   if(result > 0 && result <10)

             On the other hand switch-case statement used to take a decision between multiple options based on the variable value only like a numeric value, character value or string value.

Code Example using numeric variables:

public class SwitchCase {

	public static void main(String[] args) 
	{
		int result=3;
		
		switch(result)
		{
			case 1: // Executes if value of result is 1.
			{
				System.out.println("Value of result equal to 1.");
			}
			break;  // Break statement used to exit from swtich loop once any condition satisfied.
			case 2: // Executes if value of result is 2.
			{
				System.out.println("Value of result equal to 2.");
			}
			break;
			case 3: // Executes if value of result is 3.
			{
				System.out.println("Value of result equal to 3.");
			}
			break;
			default:
			{
				System.out.println("If all the above conditions get failed.");
			}
		}	
	}
}

Code Example using character variable:

public class SwitchCase2 {

	public static void main(String[] args) 
	{
		char result='b';
		
		switch(result)
		{
			case 'a':
			{
				System.out.println("Value of result equal to a.");
			}
			break;
			case 'b':
			{
				System.out.println("Value of result equal to b.");
			}
			break;
			case 'c':
			{
				System.out.println("Value of result equal to c.");
			}
			break;
			default:
			{
				System.out.println("If all the above conditions get failed.");
			}
		}		
	}
}

Code Example using string variable:

public class SwitchCase3 {

	public static void main(String[] args) 
	{
		String result="third";
		
		switch(result)
		{
			case "first":
			{
				System.out.println("Value of result equal to 1.");
			}
			break;
			case "second":
			{
				System.out.println("Value of result equal to 2.");
			}
			break;
			case "third":
			{
				System.out.println("Value of result equal to 3.");
			}
			break;
			default:
			{
				System.out.println("If all the above conditions get failed.");
			}
		}		
	}
}

1509total visits,1visits today

Leave a Reply

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