Categories: Java

Selection Statement

The selection statement allows choosing the set of instructions for execution depending upon an expression’s truth value.

  • Java provides two types of selection statements – if and switch. In addition, the’:?’ (ternary operator) is also used.
  • The selection statements are also called conditional statements or decision statements.

The if statement is used to test particular conditions. If the c condition evaluates to true, a course-of-action is followed, i.e, a statement or a set of statements is executed. There are four different types of If statements:

In Java, there are four different types of if statements:

  • if statement
  • if-else statement
  • if-else-if ladder
  • nested if statement
if statement:

The if statement is a single conditional statement that is only executed if the given condition is true.

Syntax:

 
if(condition)
{  
 //if true code executed
}  
If Block data flow diagram
Example:
public class IfDemo1 {  
public static void main(String[] args) 
 {  
 int age=20;  
 if(age > 18)
  {  
  System.out.print("Not a minor");  
  }  
   System.out.print(" minor");  
 }  
}  
Output:
Not a minor

Similarly, if the age is 15 then,

minor
if-else Statement

For condition testing, the if-else statement is used. If the condition is true, the if block is executed; if the condition is false, the else block is executed.

It comes in handy when we need to perform an operation based on a false result.

When the condition is false, the else block is executed.

Syntax:

if(condition)
{  
 //code for true  
}
else
{  
 //code for false  
}  
Example:

public class IfDemo2 {  
public static void main(String[] args) 
 {  
 int age=20;  
 if(age > 18)
  {  
  System.out.print("Not a minor \nCondition true");  
  
  }  
  else{
       System.out.print(" minor \nCondition false");  
  }
  
 }  
}  
 
Output:
Not a minor
Condition true

Similarly, if the age is 15 then,

minor
Condition false
If-else Block data flow diagram

We shall see about the if-else ladder and nested if in the next tutorial.

Note: also read about the Type casting in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago