Java program of decision making by using if & if-else statement
ADVERTISEMENTS
Java program of decision making, There are you will learn how to make control decisions in the Java program.
Java language follows the same decision structure as the C language.
By using the if statement:
The syntax of the if the statement is:
if (test expression)
{
// statements to be executed if the test expression is true
}
{
// statements to be executed if the test expression is true
}
Let us understand this example through Java programming:
// Java program of decision making by using if statement
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer value::\n");
int n = in.nextInt();
// true if number is less than 6
if (n < 6) {
System.out.println("Condition is true.\n");
}
}
}
Output:
Enter an integer value::
5
Condition is true.
By using the if...else statement:
The syntax of the if...else statement is:
if (test expression) {
// statements to be executed if the test expression is true
} else {
// statements to be executed if the test expression is false
}
// statements to be executed if the test expression is true
} else {
// statements to be executed if the test expression is false
}
Example-1: In this example, you will understand how true a condition occurs.
// Java program of decision making by using if-else statement
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer value::\n");
int n = in.nextInt();
// true if number is less than 6 otherwise it will be false
if (n < 6) {
System.out.println("Condition is true.\n");
} else {
System.out.println("Condition is false.\n");
}
}
}
Output:
Enter an integer value::
5
Condition is true.
Example-2: In this example, you will understand how false a condition occurs.
// Java program of decision making by using if-else statement
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer value::\n");
int n = in.nextInt();
// true if number is less than 6 otherwise it will be false
if (n < 6) {
System.out.println("Condition is true.\n");
} else {
System.out.println("Condition is false.\n");
}
}
}
Output:
Enter an integer value::
7
Condition is false.
By using the if...else ladder:
The syntax of the if...else ladder is:
if (test expression1) {
// statement(s)
} else if(test expression2) {
// statement(s)
} else if (test expression3) {
// statement(s)
}
.
.
else {
// statement(s)
}
// statement(s)
} else if(test expression2) {
// statement(s)
} else if (test expression3) {
// statement(s)
}
.
.
else {
// statement(s)
}
Let us understand this example through the Java program:
// Java program of decision making by using the if-else ladder
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer value::\n");
int n = in.nextInt();
if (n == 0) {
System.out.println("Fisrt condition is true.\n");
} else if (n == 1) {
System.out.println("Second condition is true.\n");
} else if (n == 2) {
System.out.println("Third condition is true.\n");
} else {
System.out.println("Condition failed!\n");
}
}
}
Output:
Enter an integer value::
1
Second condition is true.
By using the nested if...else statement:
The syntax of the nested if...else statement is:
if (test expression 1) {
else if(test expression 2) {
// statement(s)
} else if (test expression 3) {
// statement(s)
}
.
.
else {
// statement(s)
}
} else if (test expression n) {
// statement(s)
}
.
.
else {
// statement(s)
}
else if(test expression 2) {
// statement(s)
} else if (test expression 3) {
// statement(s)
}
.
.
else {
// statement(s)
}
} else if (test expression n) {
// statement(s)
}
.
.
else {
// statement(s)
}
Example-1: In this example, you will understand the first input is less than the second input.
// Java program of decision making by using the nested if-else statement
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the two integer values::\n");
int n = in.nextInt(),
m = in.nextInt();
if (n >= m) {
if (n == m) {
System.out.println("Result: " + n + " = " + m + "\n");
} else {
System.out.println("Result: " + n + " > " + m + "\n");
}
} else {
System.out.println("Result: " + n + " < " + m + "\n");
}
}
}
Output:
Enter the two integer values::
5
7
Result: 5 < 7
Example-2: In this example, you will understand the first input is greater than the second input.
// Java program of decision making by using the nested if-else statement
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the two integer values::\n");
int n = in.nextInt(),
m = in.nextInt();
if (n >= m) {
if (n == m) {
System.out.println("Result: " + n + " = " + m + "\n");
} else {
System.out.println("Result: " + n + " > " + m + "\n");
}
} else {
System.out.println("Result: " + n + " < " + m + "\n");
}
}
}
Output:
Enter the two integer values::
7
5
Result: 7 > 5