Java Conditionals
Java conditionals statements execute a particular section of code for a specific condition. If a specific condition is specified then only the particular code under its segment will be executed. Java has the following conditional statements:
- Use if to specify a block of code to be executed, if a specified condition is true.
- Use else to specify a block of code to be executed, if the same condition is false.
- Use else if to specify a new condition to test, if the previous condition is false.
- Use switch to specify many alternative blocks of code to be executed.
If-else Statement
- The if-else statement is used to perform two operations for a single condition.
- The if-else statement is an extension to the statement using which we can perform two different operations.
- One operation is for the correctness of that condition and other is for the incorrectness of that condition.
Control flow for if-else statement
import java.util.Scanner;
class IfElse { public static void main(String[] arr) { System.out.print("Enter your age: "); Scanner input = new Scanner(System.in); int age = input.nextInt(); if (age > 18) { System.out.println("Yooo!!! You can drive."); } else { System.out.println("Nooo!!! You can't drive yet."); } }}
Outputs :
Enter your age: 20
Yooo!!! You can drive.
Nooo!!! You can't drive yet.
If-else Ladder
- Instead of using multiple if statements, we can also use else if along with if thus forming an if else-if else ladder.
- The else-if ladder statement is an extension of the if-else statement.
- It is used in the scenario where there are multiple cases to be performed for different conditions.
- Last else is executed only if all the previous conditions fails.
- The last else block is optional.
- Using such kind of logic reduces indents.
Control flow for if else-if ladder
import java.util.Scanner;
class IfElseLadder { public static void main(String[] arr) { System.out.print("Enter your percentage: "); Scanner input = new Scanner(System.in); double marks = input.nextDouble(); if (marks >= 90) { System.out.println("You got grade A."); } else if(marks >= 80){ System.out.println("You got grade B."); } else if(marks >= 65){ System.out.println("You got grade C."); } else if(marks >= 50){ System.out.println("You got grade D."); }else{ System.out.println("You are fail."); } input.close(); }}
Outputs :
Enter your percentage : 95.6
You got grade A.
Enter your percentage : 87.4
You got grade B.
Enter your percentage : 70
You got grade C.
Enter your percentage : 57
You got grade D.
Enter your percentage : 45.9
You are fail.
Switch-Case
- The switch-case statement is an alternate to if else-if ladder statement.
- Switch-case statement is allows us to execute multiple operations for the different values of a single variable called switch variable.
- We can define various statements in the multiple cases for the different values of a single variable.
- The switch variable can be an integer, character or string type in Java.
- The case value can be used only inside the switch statement.
- The break statement in switch case is not necessary. It is optional.
- If there is no break statement found in the matched case, all the cases will be executed present after the matched case.
- The default case is also optional. The default case is executed when all the other cases are false.
Outputs :
Choose a base among 2, 8, 10, 16 : 2
The number system is BINARY.
Thanks for viewing my Java Code!!!
Choose a base among 2, 8, 10, 16 : 8
The number system is OCTADECIMAL.
Thanks for viewing my Java Code!!!
Choose a base among 2, 8, 10, 16 : 10
The number system is DECIMAL.
Thanks for viewing my Java Code!!!
Choose a base among 2, 8, 10, 16 : 16
The number system is HEXADECIMAL.
Thanks for viewing my Java Code!!!
Choose a base among 2, 8, 10, 16 : 20
The choice is not valid.
Thanks for viewing my Java Code!!!
Comments
Post a Comment