Conditional & Decision Making Statements in Java
Conditional Control Structure(Decision Making Statements) in Java
As we have already discussed the features of the conditional Control Structures in this article, let us proceed with details of each example. These are also referred to as Decision Making Statements in Java.
Types of Decision Making Statements in Java:
if statements:
- The if statements help control the program flow based on some condition.
- It executes some code block if the condition is true, otherwise, it will get skipped.
Syntax:
if (test_expression) { //statements to be executed }
If and only if the test_expression
is true, the statement block will get executed or it will get skipped. Here is a working example.
public class NumberGreater { public static void main(String[] args) { int x = 20; int y = 15; if ( x > y ) { System.out.println("x is greater than y"); } } }
Output:
if..else statement:
- We use if-else statements as well, to control the program flow based on some condition. But it is has a difference
- It executes some statement code block if the condition is true and if the condition is false, it executes else statement code block.
Syntax:
if (test_expression) { //statements for if block } else { //statements for else block }
If and only if the test_expression
is true, the Statements for if block will get executed otherwise statements for else block will get executed. Here is a working example.
import java.util.*; public class Vote { /** Enter the age of the person * .it will show if the person is eligible to vote */ public static void main(String[] args) { int age ; System.out.println("Enter the age : ") ; // code to enter data from the keyboard. Scanner sc = new Scanner(System.in); age = sc.nextInt(); if (age >= 18) { System.out.println("\n The person is eligible to vote."); } else { System.out.println("\n The person is not eligible to vote."); } } }
Output:
Nested if..else statements:
- These statements allow us to put conditional statements inside another conditional statement.
- First the outer if block is evaluated. If the condition is true, then it goes to the inner if block to check its validity.
Syntax:
if (test_expression_one) { if (test_expression_two) { // statements to be executed if test_expression two is true. } } else { //else statement block }
First, it evaluates test_expression_one
, if it is true then it goes for evaluating test_expression_two
.
If test_expression_two is true, then it executes the statements inside the block otherwise executes the else statement block.
If anyone of the if conditions are false, then the else statement block code will be executed.
import java.util.Scanner; public class ScienceEligible { /** Enter the classname and marks * it will show if the student is eligible for Science stream */ public static void main(String[] args) { int classname ; float marks ; // code to enter data from the keyboaard. System.out.println("Enter the classname and marks"); Scanner sc = new Scanner(System.in); classname = sc.nextInt(); marks = sc.nextFloat(); if (classname == 11) { if ( marks >= 85) { System.out.println("You are eligible for Science stream !!"); } else { System.out.println("Sorry ! You are not eligible for Science stream1 !"); } } else { System.out.println("Sorry ! You are not eligible for Science stream !"); } } }
Output:
Multiple if ( if..else ladder):
- We use this conditional statement when we need to take multiple decisions, based on multiple conditions and their results.
- It will contain multiple blocks which depend on the truth value of its respective condition.
Syntax:
if (test_expression 1) { // statements } else if (test_expression 2) { // statements } else if (test_expression n) { // statements } else { // statements }
The below programs will help you understand how to use multiple if-else statements.
import java.util.Scanner; public class AverageGrade { /** Enter the marks of 3 subjects and find the average and the grade . * .If avg>=80, grade A * If avg>=60, grade B * .If avg>=40, grade C * .If avg<40, failed in the exam */ public static void main(String[] args) { int s1, s2, s3 , sum , avg; System.out.println("Enter your marks for the 3 subjects"); Scanner sc = new Scanner(System.in); s1 = sc.nextInt(); s2 = sc.nextInt(); s3 = sc.nextInt(); sum = s1 + s2 + s3; avg = sum/3; System.out.println("your avg is "+avg); if (avg >= 80) { System.out.println("You got A grade"); } else if ( avg >= 60) { System.out.println("You got B grade"); } else if ( avg >= 40) { System.out.println("You got C grade"); } else { System.out.println("You Failed in this exam"); } } }
Output:
import java.util.*; public class StudentMarks { /** Enter the marks of 3 subjects and find the average and the grade . * If avg>=80, grade A * If avg>=60, grade B * If avg>=40, grade C * If avg<40, failed in the exam . */ public static void main(String[] args) { int s1, s2, s3 , sum , avg ; System.out.println("Enter your marks for the 3 subjects"); Scanner sc = new Scanner(System.in); s1 = sc.nextInt(); s2 = sc.nextInt(); s3 = sc.nextInt(); sum= s1 + s2 + s3; avg= sum/3; System.out.println("your avg is "+avg); if (avg >= 80) { System.out.println("You got A grade"); } if ( avg >=60 && avg < 80) { System.out.println("You got B grade"); } if ( avg >=40 && avg < 60) { System.out.println("You got C grade"); } if (avg < 40) { System.out.println("You Failed in this exam"); } } }
Output:
In the next article, we will discuss the details of looping or iteration control structure statements available in Java.