No announcement
 

Conditional Statements

Conditional statements are executed when a certain condition is met.

if statement

An if statement consists of a boolean expression followed by one or more statements.

The structure of if statement is:

if (condition) statement

the above statement can also be written as:

if (condition)
 {
     statement;
 }

if you want to execute multiple statements then:
if (condition)
 {
     statement 1;
     statement 2;
 }

The statements will get executed when the boolean expression evaluates to true.

Example:

IfStatementDemo.java

package com.codingnous.tutorial;

public class IfStatementDemo {

	public static void main(String[] args) {

		// Declare and initialize test variable
		int percentage = 40;

		// Check the test variable against a value
		if (percentage > 33) {
			System.out.println("Result is Pass");
			System.out.println("You obtained " + percentage + " %");
		}
	}

}

Output
Result is Pass You obtained 40 %

if..else statement

An if..else statement consists of optional else statement which gets executed when the boolean expression is false.

The structure of if statement is:

if (condition)
 {
     statement(s) when condition is true;
 }
else
 {
     statement(s) when condition is false;
 }

Example:

IfElseStatementDemo.java

package com.codingnous.tutorial;

public class IfElseStatementDemo {

	public static void main(String[] args) {

		// Declare and initialize test variable
		int percentage = 32;

		// Check the test variable against a value
		if (percentage > 33) {
			System.out.println("Result is Pass");
		} else {
			System.out.println("Result is Fail");
		}
		System.out.println("You obtained " + percentage + " %");
	}

}

Output
Result is Fail You obtained 32 %

switch statement

The if/else construct is suitable when we have few conditions to test. If we have to deal with multiple selections then switch statement can be useful.

The structure of switch statement is:

switch (variableName)
 {
     case value1: statement(s)
                         break;
    case value2: statement(s)
                         break;
    case value3: statement(s)
                         break;
    case value4: statement(s)
                         break;
    default: statement(s)
}

The variable defined within the switch parenthesis, i.e., variableName will be tested for matching value defined after case keyword, i.e., value1, value2, value3, value4. If the value of variableName does not matches with any case then default is executed. Break and Default is optional. If you want your code to fall below, you can skip break.

Switch variable (variableName) can only be of type integers, convertable integers (byte, short, char), strings and enums.

Example:

SwitchStatementDemo.java

package com.codingnous.tutorial;

public class SwitchStatementDemo {

	public static void main(String[] args) {

		// Declare and initialize test variable
		int rating = 3;

		// Switch case
		switch (rating) {
		case 1:
			System.out.println("Rating is extremely poor");
			break;
		case 2:
			System.out.println("Rating is poor");
			break;
		case 3:
			System.out.println("Rating is average");
			break;
		case 4:
			System.out.println("Rating is good");
			break;
		case 5:
			System.out.println("Rating is excellent");
			break;
		default:
			System.out.println("Rating is NA");
		}
	}

}

Output
Rating is average