All IT Courses 50% Off
QA Tutorials

Java Selection Statements

Basically, Java will support two selection statements which include: IF statement and switch statement, these statements will allow to control the flow of our program execution which is based upon conditions which are known only during run time.

IF statement:

IF statement is basically the Java’s conditional branch statement which can be used to route the program execution through two different paths.

Syntax for if statement is:





  1. Each statement will be a single statement or may be compound statement enclosed in the curly braces.
  2. Condition will be any expression which will return Boolean value 
  3. Else clause is optional

IF statement will work like this: suppose IF condition is true then statement1 will be executed else statement2 will be executed, in the no case will both statements will be executed.

Let us consider an example:

Int a, b;

  // …..





  • if a is less than b then a is set to zero else
  • b is set to zero
  • in no case both a & b will be set to zero.

2. Nested IF statement: 

Nested IF statements are very common in the programming. When we nest IF, main thing to remember is to that else statement will always refer to the nearest or latest IF statement, that is within same block as the else and that is not associated with else.

Let us consider an example:
If ( i == 10)
If (j < 20 )   
a = b;
If(k > 100)   
c = d;
a = c;

This indicates final else is not associated with if(j < 20) because its not in the same block, final else will be associated with if(k>100) because its closest to IF within the same block.

SWITCH statement: 

SWITCH is the multiway branch statement which provides easy way to dispatch execution to the different parts of our code which is based on the value of an expression.

Syntax of SWITCH statement is:

Case value1:
    // statement sequence
Case value2:
    //statement sequence
Case valueN.
    // statement sequence
   // default statement sequence 

Expression must be type byte, short, int or char each of the value will be specified in the case statements must be type compatible with the expression, here each case will be unique.

Let us consider an example:

Class simpleswitch
Public static void main (string args [])
Case 0:
    System.out.println(“I is zero”);
Case 1:
  System.out.println(“I is one”);
  System.out.println(“I is greater than 2”);


I is zero

I is one

I is greater than 2

As we can see each time through the loop the statements will be associated with the case constant which will match I are executed rest all are bypassed.After I is greater than 2 no case statement I are executed 


  1. What is selection statement?
  2. Explain IF statement with program
  3. Explain SWITCH statement with program
Facebook Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button

Get Python Course
worth 499$ for FREE!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys Blog will use the information you provide on this form to be in touch with you and to provide updates and marketing.