08 May Java Decision Making Statements
Decision making in Java comprises one or more conditions to be evaluated by the program. Java decision making statements, include the following,
- if statement
- if-else statement
- If-else if-else statement
- Nested Ifs
- Switch Statement
- Nested Switch
Let us discuss them one by one,
if
Under if, the statement executes if the condition is true. It is the most commonly used decision-making statement in C, C++ and Java.
Syntax
1 2 3 4 5 |
if(condition) { // statements execute if the condition is true } |
Example
Let us see an example, wherein we will learn about the usage of if statement in Java,
1 2 3 4 5 6 7 8 9 10 11 12 |
public class StudyopediaDemo { public static void main(String args[]) { int i = 10; // if statement if(i>5) System.out.println("The value is more than 5"); } } |
Output
The following is the output,
if-else
Under the if-else statement, the first statement executes if the condition is true, else statement two executes.
Syntax
1 2 3 4 5 6 7 8 |
if(condition) { // statement1 execute if the condition is true } else { // statement2 execute if the condition is false } |
Example
Let us see an example, wherein we will learn about the usage of if-else statement in Java,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class StudyopediaDemo { public static void main(String args[]) { int i = 2; System.out.println("if-else statement in Java - Studyopedia"); // if-else statement if(i>5) { System.out.println("The value is more than 5"); } else { System.out.println("The value is less than 5"); } } } |
Output
The following is the output,
if-else if-else
The if-else if-else executes and whichever condition is true, the associated statement executes. If none of them is true, then else executes.
Syntax
1 2 3 4 5 6 7 8 9 10 11 12 13 |
if(condition) { // statement executes when this condition is true }else if(expression) { // statement executes when this condition is true }else if(condition) { // statement executes when this condition is true }else if(condition) { // statement executes when this condition is true }else { // this statement executes when none of the condition is true. } |
Example
Let us see an example, wherein we will learn about the usage of if-elseif-else statement in Java,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class StudyopediaDemo { public static void main(String args[]) { int marks = 90; if( marks == 90 ) { System.out.print("Marks is 90"); }else if(marks == 75 ) { System.out.print("Marks is 75"); }else if( marks == 80 ) { System.out.print("Marks is 80"); }else { System.out.print("else statement"); } } } |
Output
The following is the output,
Nested ifs
Under nested ifs, as the name suggests, ifs are nested. If the condition under the topmost if statement is true, then the nested if execute.
Syntax
1 2 3 4 5 6 7 8 |
if(condition) { // statement executes if this expression is true if(condition) { // statement executes if this expression is true } } |
Example
Let us see an example, wherein we will learn about the usage of nested ifs in Java,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class StudyopediaDemo { public static void main(String args[]) { int i = 8; System.out.println("'Nested if' in Java - Studyopedia"); // nested if if(i>5) { if(i==8) { System.out.println("The value is more than 5"); } } } } |
Output
The following is the output,
Switch
Under switch statement, the comparison is between the expression and the list of values (case). A match would allow the case statement specific to that value to execute. A break statement is useful inside a switch to terminate a statement sequence.
If none of the value matches with the expression, the default statement executes. However, remember that the default statement is optional.
Syntax
Here, the expression is a Boolean expression,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
switch(expression) { case value1 : break; case value2 : break; case value3 : break; . . . default : } |
Example
Let us see an example, wherein we learn about the usage of switch statement in Java,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class StudyopediaDemo { public static void main(String args[]) { System.out.println("'Switch' in Java - Studyopedia"); int marks = 75 ; switch(marks) { case 90: System.out.println("Grade A"); break; case 75: System.out.println("Grade B"); break; case 60: System.out.println("Grade C"); break; } } } |
Output
The following is the output,
Nested switch
A nested switch occurs when a switch statement is inside another switch. It allows you to add multiple options to a case value.
For example, do you like, “Desktop”, “Laptop”, or “iPad”. If you like laptop, further options can be added i.e. Business Laptops, Gaming Laptops, etc. The flow would be like,
Let us see the syntax,
Syntax
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
switch(expression) { case value1 : break; case value2 : switch(expression) { case value2.1 : break; case value2.2 : break; case value2.3 : break; default : case value3 : break; . . . default : } |
Example
Let us see an example, wherein we learn about the usage of nested switch,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class StudyopediaDemo { public static void main(String args[]) { System.out.println("'Switch' in Java - Studyopedia"); int marks = 75 ; int a = 80; switch(marks) { case 90: System.out.println("Grade A+"); break; case 75: switch(a) { case 80: System.out.println("Grade B+"); break; case 85: System.out.println("Grade A"); break; } } } } |
Output
The following is the output,
In this lesson, we learned how to work with Java Decision Making Statement, such ifs, if-else, switch, etc. In the next lesson, we will learn about loops in Java.
No Comments