Category: conditional statements in java
There are 2 conditional statements in java :
* IF statement
*Switch- case
Java If-else Statement
==================
The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in Java.
if statement
if-else statement
if-else-if ladder
nested if statement
Java if Statement
The Java if statement tests the condition. It executes the if block if condition is true.
Syntax:
if(condition){
//code to be executed
}
Syntax2:
if(condition){
//code if condition is true
}else{
//code if condition is false
}
Leap Year Example:
============
A year is leap, if it is divisible by 4 and 400. But, not by 100.
public class LeapYearExample {
public static void main(String[] args) {
int year=2020;
if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){
System.out.println(“LEAP YEAR”);
}
else{
System.out.println(“COMMON YEAR”);
}
}
}
Java Switch Statement
==================
The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. The switch statement works with byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long. Since Java 7, you can use strings in the switch statement.
In other words, the switch statement tests the equality of a variable against multiple values.
Syntax :
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
……
default:
code to be executed if all cases are not matched;
}
Posts Not Found
Maybe try a search?