Skip to toolbar

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.");
			}
		}		
	}
}

3655total visits,3visits today

Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 2
  • Page views today : 3
  • Total visitors : 48,110
  • Total page view: 66,245

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.