DEV Community

Indumathy
Indumathy

Posted on

Decision Making Statement

Decision making statements are used to execute different blocks of code based on conditions (true or false).

  • if()
  • else()
  • else if()

if Statement:

if statement execute the block, when if condition is true.

Example:

int mark = 92;
if(mark>=90)
{
System.out.println("Grade A");
}

output: Grade A

else Statement:

else statement execute the block, when the if condition is false.

Example:

int mark = 80;
if(mark>=90)
{
System.out.println("Grade A");
}
else(mark<90)
{
System.out.println("Grade B");
}

output: Grade B

else if statement:

If we want to check multiple condition we go for else if statement which is use between if and else satement.
Example:

int mark = 75;
if(mark>=90)
{
System.out.println("Grade A");
}
else if(mark>=80)
{
System.out.println("Grade B");
}
else if(mark>=70)
{
System.out.println("Grade c");
}
else if(mark>=60)
{
System.out.println("Grade D");
}
else
{
System.out.println("Fail");
}

Output: Grade c

Switch Statement:

Switch statement is use when there are multiple fixed choices like days in week, food items in hotel menu.

Example:

int order = 3;
switch(order)
{
case 1: System.out.println("Ice Cream");
break;
case 2: System.out.println("Juice");
break;
case 3: System.out.println("Briyani");
break;
default:
System.out.println("Out of Menu");
}

Data types in Switch case:

  • Swithch case only allow byte, short, int, char from primitive data type and String from non primitive data type.

  • Switch case don't allow long, float, double and Boolean data type.

Note: String data type was allowed in Switch case at java 8 version.

Top comments (0)