Java Decision Making Statements

Java Control Flow, Java Decision Making Statements, Java if statement, Java else statement, Java nested conditions, and Java Switch case statement.

Control Flow Statements in Java

We have three types of conditional statements in computer programming.

1. Decision-making/Conditional statements (if, switch)
2. Loop Statements (for, while, do while, and enhanced for)
3. Branching statements (break, continue, and return)

Decision Making Statements

Two types of Conditional Statements

1. if statement
2. switch statement

Three types of Conditions

1. Single Condition (Positive and Negative Conditions)

Syntax:

if (condition){
.
.
}

Positive condition

if (a>b) {
.
.
}

Negative Condition

if (!(b>a) {
.
.
}

2. Compound Condition (Positive and Negative Conditions)

Positive Condition

if ((condition1) && Or ||(condition2) {
.
.
}

Negative Condition

if (!(condition1) && Or || (condition2){
.
.
}

3. Nested Condition (Positive and Negative Conditions)

if (condition1){
if (condition2){
if (condition3){
.
.
}
}
}


Usage of Conditional Statements

In Java, we have the following decision-making statements –

1. Java if Statements
2. Java If else Statements
3. Java if else if Statements
4. Java Nested If Statements
5. Java Switch Case Statement

1. Execute a block of statements when a condition is true

Syntax:

if (condition){
Statements
.
.
.
}

Example:

int a=100, b=50;

if (a>b){
System.out.println(“A is Big Number”);
}
System.out.println(“Hello”);
}

2. Execute a block of statements when a condition is true, otherwise execute another block of statements

Syntax:

if (condition){
.
.
}
else
{
.
.
}

Example:

int x=100, y=50;

if (a>b){
System.out.println(“X is Big Number”);
}
else
{
System.out.println(“Y is Big Number”);
}

3. Execute a block of statements when a compound condition is true

Syntax:

if ((condition1) && or || (condition2) && or || (condition3)){
.
.
}

Example:

int a=100, b=90, c=700;

if ((a>b) || (a>c)){
System.out.println(“A is Big Number”);
}

4. Execute a block of statements when a compound condition is true, othewise execute another block of statements,

Syntax:

if ((condition1) && or || (condition2) && or || (condition3)){
.
.
}
else {
statements
.
.
}

Example:

int a=100, b=90, c=700;

if ((a>b) && (a>c)){
System.out.println(“A is Big Number”);
}
else
{
System.out.println(“A is Not Big Number”);
}

5. Decide among several alternates (else if)

Syntax:

if (condition) {
.
.
}
else if (condition){
.
.
}
else if (condition){
.
.
}
else if (condition){
.
.
}
else {
.
.
}
———————
In Software Testing
Requirement – Test Case/Test Script

In Software Development
Problem – Solution

Problem: Initialize an Integer variable and verify the range

if the number is in between 1 and 100 then display “Number is Small Number”
if the number is in between 101 and 1000 then display “Number is Medium Number”
if the number is in between 1001 and 10000 then display “Number is Big Number”
if the Number is more than10000 then display “Number is High Number”
Otherwise, Display “Number is either Zero or Negative Value”

Solution:

int val=-100;

if ((val > 0) && (val <=100)) {
System.out.println(“Val is Small Number”);
}
else if ((val >100) && (val<=1000)) {
System.out.println(“Val is Medium Number”);
}
else if ((val >1000) && (val<=10000)) {
System.out.println(“Val is Big Number”);
}

else if (val>10000) {
System.out.println(“Val is High Number”);
}

else {
System.out.println(“Val is either Zero or Negative Number”);
}

6. Run / Execute a block of Statements/steps Instructions when more than one condition is true (Nested if)

Syntax:

if (condition1){
if (condition2{
if (condition3) {
.
.
}
}
}

Example:

Check whether a variable (a) is having a big value number among four variables, using the Nested condition

int a=100, b=90, c=80, d=70;

if (a>b) {
if (a>c) {
if (a>d) {
System.out.println(“A is Big Number”);
}
}
}

Using Compound Condition

int a=100, b=90, c=80, d=70;

if ((a>b)&&(a>c)&&(a>d)) {
System.out.println(“A is Big Number”);
}

Nested Condition vs. Compound Condition

In Nested Condition we can write multiple else parts then we can locate the exact problem
In Compound Condition we can write a single else part only, so we can’t locate the exact problem

if ((a>b)&&(a>c)&&(a>d)) {
System.out.println(“A is Big Number”);
}
else {
System.out.println(“A is Not Big Number”);
}

Using Nested if structure we can write multiple “else” parts so that we locate the exact problem.

Program:

int a=100, b=900, c=80, d=70;

if (a>b) {
if(a>c) {
if(a>d) {
System.out.println(“A ia Big Number”);
}
else {
System.out.println(“A is Not Big Number – 3rd Condition is False”);
}
}
else {
System.out.println(“A is Not Big Number – 2nd Condition is False”);
}
}

else {
System.out.println(“A is Not Big Number – 1st Condition is False”);
}

Problem: Find the biggest number among four numbers

Hint: Use else if and compound conditions…

Solution:

int a=100, b=10, c=807, d=807;

if ((a>b) && (a>c)&& (a>d)){
System.out.println(“A is Big Number”);
}

else if ((b>a) && (b>c)&& (b>d)){
System.out.println(“B is Big Number”);
}

else if ((c>a) && (c>b)&& (c>d)){
System.out.println(“C is Big Number”);
}

else if ((d>a) && (d>b)&& (d>c)){
System.out.println(“D is Big Number”);
}

else {
System.out.println(“Two or more values are Big”);
}

7. Decide among several alternates (switch statement)

Syntax:

switch (expression) {

case1 value:
statements
.
.
break;

case2 value:
statements
.
.
break;

case3 value:
statements
.
.
break;

case4 value:
statements
.
.
break;

default:
statements
.
.
}

Example:

If the grade is A then print “Excellent”, B – “Good”, C – “OK”, Others – Invalid Grade

a. Solution using else if structure

char grade =’X’;

if (grade == ‘A’) {
System.out.println(“Excellent”);
}
else if (grade == ‘B’) {
System.out.println(“Good”);
}

else if (grade == ‘C’) {
System.out.println(“OK”);
}

else {
System.out.println(“Invalid Grade”);
}

b. Solution using switch structure

public static void main(String[] args) {
char grade =’D’;

switch (grade) {

case ‘A’:
System.out.println(“Excellent”);
break;

case ‘B’:
System.out.println(“Good”);
break;

case ‘C’:
System.out.println(“OK”);
break;

default:
System.out.println(“Invalid Grade”);
}

switch structure Example 2:

int a=10, b=5;
String operation =”mul”;

switch (operation) {//Condition/Expression/Operand

case “add”:
System.out.println(a+b);
break;

case “sub”:
System.out.println(a-b);
break;

case “mul”:
System.out.println(a*b);
break;

default:
System.out.println(“Invalid Operation”);
}


Java Programming Language Syllabus

Java Step by Step Videos

Follow me on social media: