Java Conditional Statement
Table of Contents
In Java Conditional Statements, Java uses Boolean variables to evaluate conditions. Boolean value is returned when true and false when an expression is compared or evaluated.
Following Java Conditional Statements:-
Sr.No. | Statement | Description |
---|---|---|
1. | if statement | An if statement contains a boolean expression followed by one or more statements. |
2. | else statement | This statement executes when the boolean expression is false. |
3. | nested if statement | You can use one or more if or else if statement inside another if or else if statement(s). |
4. | switch case statement | A switch case statement allows a variable to be tested for equality against a list of values. |
1. if statement
If a condition is true, use the if statement to specify a block of Java code to execute.
Syntax
if (condition1) {
// if the condition1 is true, then execute this block.
}
Example
if (10 > 8) {
System.out.println("10 is greater than 8.");
}
2. else statement
Syntax
if (condition2) {
// if the condition2 is true, then execute this block.
} else {
// if the condition2 is false then execute this block.
}
Example
int a = 10;
if (a < 7) {
System.out.println("in if = " + a);
} else {
System.out.println("in else = " + a);
}
ternary (? : ) Operator
Ternary operator same as if else statement.
Syntax
variable = (condition) ? expressionTrue : expressionFalse;
Example
int a1 = 10;
String result = (a1 < 8) ? "in if block value= " + a1 : "in else block value= " + a1;
System.out.println(result);
3. nested if statement
When there is an one or more if statement inside a if statement, it is called the nested if statement.
Syntax
if(first condition) {
if(second condition) {
}
}
Example
int number = 70;
if(number < 100 ){
System.out.println("number is less than 100.");
if(number > 50){
System.out.println("number is greater than 50.");
}
}
// Output
number is less than 100
number is greater than 50
4. switch case statement
Use the switch case statement to select one of several code blocks to be executed.
Syntax
switch(expression) {
case a:
// code block
break;
case b:
// code block
break;
case c:
// code block
break;
default:
// code block
}
- The switch expression is evaluated once.
- The value of the expression is match with the values of each case.
- If a match occurs, the corresponding block of code is executed.
- The break and default keywords are optional.
Example
int gender = 2;
switch (gender) {
case 1:
System.out.println("Male");
break;
case 2:
System.out.println("Female");
break;
default:
// This 'default' keyword specifies some code to run if there is no case match
System.out.println("Other");
}
// Outputs
Female
Question :-
- What is “if statement” in Java?
- How to work switch case in Java?
- How many Java Conditional Statements available in java?
- What is ternary (? : ) Operator in Java?
- What is use of break keyword in Java?
- How to work switch statement in Java with examples?
- If Ternary operator same as if else statement In Java?
0 Comments