Table of Contents
Decision Making Statements in Java
Decision making in Java can be paralleled with making decisions in real life. A decision-making statement is required in programming when you want to execute a certain part of the code if a condition is satisfied and execute a different part of your code when the condition is not satisfied. The control statements are used to determine the flow of the program in such cases. These statements either help the code progress, terminate, or branch in smaller parts.
Java’s Decision-Making Statements
- if
- if-else
- nested-if
- if-else-if
- switch-case
- jump – break, continue, return
IF
The if statement is a fundamental decision-making statement that is used in almost every Java program. The ‘if’ statement determined whether a certain block will be executed if a certain condition is met. Suppose the condition is true, the lines inside the if block will be executed. Otherwise, the lines of code will be ignored, and the flow of the program will carry on.
Syntax:
if (condition) { //Statements to be executed //if the condition is true. }
Example:
class Demo { public static void main (String args []) { int k = 50; if (k > 100) System.out.println(“Hello”); System.out.println(“Hello World”); } }
OUTPUT
Hello World
If – Else
The If-Else block is an extension of the If Block. Here, if a particular condition is met, a certain execution will take place. If the condition is not met, the program will direct its flow to another set of statements.
Syntax:
if (condition) { //Execution of certain statements } else { //Execution of another set of statements }
Example:
class Demo { public static void main (String args []) { int k = 10; if(k > 5) System.out.println(“Hello”); else System.out.println(“Hello World”); } }
OUTPUT
Hello
Nested-If
The nested-if statement contains more than one block inside each other. It is acceptable in Java to nest more than one if statements inside each other.
Syntax:
if (condition 1) { // Execution a certain block of statements if (condition 2) { // Execute a certain block of statements } }
Example:
class Demo { public static void main (String args []) { int k = 10; if (k == 10); { if (k < 15) System.out.println(“k is smaller than 15 ”); if (k < 12) System.out.println (“k is smaller than 12 too”); else System.out.println(“k is greater than 15”); } } }
OUTPUT
k is smaller than 15
k is smaller than 12 too
If-Else-If
If-Else-If contains a series of nested if-else blocks inside each other. In this type of decision statement, you can add an if block inside an else block.
Syntax:
if (condition) statement; else if (condition) statement; . . else statement;
Example:
class Demo { public static void main (String args []) { int k = 20; if (k == 10); k++; else if (k == 20) k--; else System.out.println(“No choice”); System.out.println(k); } }
OUTPUT
19
Switch Case
Switch Case allows users to choose from a series of case statements that give the value of the switch expression.
Example:
class Demo { public static void main (String args []) { int k = 10; switch (k) { case 0: System.out.println(“0”); break; case 1: System.out.println(“1”); break; case 10: System.out.println(“10”); break; default: System.out.println(“Hello”); } } }
OUTPUT
10
Jump
Jump in Java is comprised of the following three statements:
- Break – To forcefully terminate the current decision-making block.
- Continue – To move on to the next iteration of a loop.
- Return – This statement is used to return a particular value from a method to the method call.
0 Comments