Home > Tutorial > Java If Else Sample Code

Java If Else Sample Code

The if else statement is the foundation of all the control flow statements in the Java programming language. These statements tell your Java application to execute a section of code or not to execute a section of code based on some evaluation.

Java If-Else Example

The simple Java program below shows the flow when the condition is true. Since we set the condition to be true explicitly, the output below is expected.

	//Method exampleA to show simple control flow
	public static void exampleA()
	{
		//Set my conditional to true
		boolean myConditional = true;
		
		
		if(myConditional)
		{
			System.out.println("Conditional Was True. Execute Some Code In This Section!");
		}
		else
		{
			System.out.println("Conditional Was NOT True. Execute Some Code In This Section!");
		}	
	}



Java If-Else Example Output:

Conditional Was True. Execute Some Code In This Section!


Java If-Else If-Else Example

This next sample Java application method shows how to use if-else if-else statements to control the flow of the program. If the number was less than 100, it will fall into the first block of code. If the number was greater than 100, the second block of code will be executed. Since we set our number to 100, we fall into the else, which is really the catch-all or default block.

//Method exampleB to show different control flow
	public static void exampleB()
	{
		//Set my conditional to true
		int randomNumber = 100;
		
		
		if(randomNumber < 100)
		{
			System.out.println("Random Number is Less Than 100");
		}
		else if(randomNumber > 100)
		{
			System.out.println("Random Number Must be Greater Than 100");
		}	
		else
		{
			System.out.println("Random Number: "+randomNumber);
		}
	}



Java If-Else If-Else Example Output:

Random Number: 100


Java Nested If Else Example

Our last sample code shows how Java allows nested “else if” statements to be included together. If none of the “if” or “else if” conditions are met, the code will by default drop into the “else” statement at the bottom. On ther other hand, if any of the previous conditions are met, the code will not drop into the “else”. You can see how this example mimics the switch and case statements. If case is not a concern, use of the String toLower() or toUpper() could have been included here.

//Method exampleC to show nested if-else if-else
	public static void exampleC()
	{
		String myString = "JAVACODEDEPOT.COM";
		
		if(myString.equals("JavaCodeDepot.com"))
		{
			System.out.println("Found A Match If: "+myString);
		}
		else if(myString.equals("JavaCodeDepot.COM"))
		{
			System.out.println("Found A Match First Else If: "+myString);
		}
		else if(myString.equals("JAVACodeDepot.com"))
		{
			System.out.println("Found A Match Second Else If: "+myString);
		}
		else if(myString.equals("JAVACODEDEPOT.COM"))
		{
			System.out.println("Found A Match Third Else If: "+myString);
		}
		else
		{
			System.out.println("Hit the Default!");
		}
	}



Java Nested If Else Output:

Found A Match Third Else If: JAVACODEDEPOT.COM


Conclusion

You have a couple of options in Java when coding conditional statements or program control flow. An earlier tutorial showed the guidelines on how to use the Java Switch Statement. The if-else is ideal for a simple condition, but the switch and case may be better if you have multiple conditions. I suppose it comes down to coding preferences.

Seymour
Posted by

Seymour

I have been a software developer for twenty years now focusing on writing high quality Java applications. If you are in a programming field, you know that technology is constantly changing. You have to keep current or you will get left behind!

You may also like...

Leave a Reply

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

Copyright © 2018 JavaCodeDepot