Selection and Conditional statement in Java

In this part we are going to learn, what are statements in java? Different types of statements in java, how to execute conditional statements in java? and what are their syntax? which will help you to code your choice in a better way

Let’s start with the statement,

A statement in Java is a single command that is executed by the Java interpreter. By default, the java interpreter runs one statement after another, in the order, they are written and thus process continues.

java offers many statement types including simple, compound, program flow control statements.

Program control statement (or control flow statement) broadly belong to one of these categories:

1. Selection statements
2. Iteration statements
3. Jump statement

Selection statement:

The selection allows choosing the set-of-instruction for execution depending upon an expression’s truth value. java provides two types of selection: if and switch.
in addition to certain circumstances?  the operator can be used as an alternative to the if statement.

the selection statements also called conditional statements or decision statements.

The if statement :

An if statement tests a particular condition; if the condition evaluates to true, a course of action is followed i.e, a statement or set of a statement is executed. otherwise (if the condition evaluates to false), the course of the action is ignored.

The syntax of the if statement is as follows:


Where a statement may consist of a single statement, a compound statement, or nothing (in case of an empty statement).
for example,

class even
public static void main(String args[])
int i =8;
if( i%2==0)
System.out.println(" This number is even");

What about if we want a second option if the part in the if statement becomes false.? then java have if-else statements.

if-else statement:

You can also have an else part in if statement. the else part gets executed if the condition (or expression) of if statement evaluates to false. the syntax of using if-else is:

if (expression | condition)

statement a;


statement b;

where statement a and statement b may consist of a single statement, a compound statement, or an empty statement. for instance, consider the following code:


System.out.println(N+ "is even");


System.out.println(N+ "is odd");

the above code evaluates whether the remainder of N divided by 2 is zero or not if it is. i.e., when expression N%2==0 evaluates to true (say N is 8), it prints

8 is even

otherwise(i.e. when else part is executed say, when N =9) it prints

9 is odd

This is only applicable when you want to execute either of the choices what about multiple-choice .? java offers nested ifs for this lets should discuss what is nested Ifs.

Nested -if:

A nested if is and if t6hat has another if in its ifs body or in its else’s body. the nested if can  be written as:

if(expression 1)


if(expression 2)



statement 2;




if(expression 3)





if statement, either there can be if statement(s) in its body-of-if or in its body-of-else or in both. the inner ifs can themselves be nested if, but the inner if must terminate before an outer if.

Ternary operator:

This is the most interesting part of the conditional statement. Have you ever heard about it?

let’s learn what is the ternary operator, why should we learn it?

java has an operator that can be used s an alternative to if statement. This operator can be used to replace if-else statement of the general form:

if(expression 1)

expression 2;



The above form of if can be alternatively written using ?: as follows;

expression1? expression 2: expression3;

it works in the same way as the above-given form of if does i.e., expression1 is evaluated, if it is true, expression2 gets executed(i.e., its value becomes the value of entire expression) otherwise expression3 gets executed(i.e., its value now becomes the value of the entire expression). for instance, the following if statement.

int c;





We can alternatively written is as

int c=a>b ?a:b;

See how simple and compact your code has become.

Switch case:

Java provides a multiple- branch selection statement known as a switch. this selection statement successively tests the value of an expression against a list of integer or character constants.

when a match is found, the statement associated with that constant is executed. the syntax of the switch statement is as follows:



case constant1: statement sequence 1;


case constant2:

statement sequence 2;


case constant3:

statement sequence 3;


case constant n-1:

statement sequence n-1;



statement sequence n;


Now you must be wondering about the words break, switch and default.lets have a look in hem in detail

the expression is evaluated and its values are matched against the values of the constants specified in the case statements. when a match is found, the statement sequence associated with that case is executed until the break statement or the end of the switch statement is reached

.the default statement gets executed when no match is found,f the absence of break, this is called fall through.

the default statements are optional and, if it is missing, no action takes place if all matches fail.