Java Control Statements, Decision making statements are utilized to test the program condition, following are the some statements.
If statement is used to return the true value, following is the conceptual figure which can guide simply. In the following figure if condition is true then it goes for action otherwise it checks for the alternative action finally code will be closed.
Following is an example to understand the
if statement.
leapyear.java
To know the logic behind leap year program two rules need to be remember as follows.
Rule 1: A year is called leap year if it is divisible by 400 such as 1600, 2000 etc.
Rule 2: If year is not divisible by 400 as well as 100 but it is divisible by 4 then that year are also leap year such as 2004, 2008, 1012 etc.
[java]import java.util.Scanner;
public class leapyear {
private static Scanner input;
public static void main(String args[])
{
int year;
System.out.println("Enter the year");
Scanner s=new Scanner(System.in);
year = s.nextInt();
if(year%400==0 || year%100 ==0 || year%4==0)//this is the if condition{
System.out.println("Entered number is leap year");
}
}
}
[/java]
Output
[java]Enter the year
2016
Entered number is leap year[/java]
Now Splessons will explain the above example with
if else statement. The following is the syntax for the
if else condition.
[java]
if(Boolean_expression) {
// Executes when the Boolean expression is true
}else {
// Executes when the Boolean expression is false
}
[/java]
leapyear.java
[java]
import java.util.Scanner;
public class leapyear {
private static Scanner input;
public static void main(String args[])
{
int year;
System.out.println("Enter the year");
Scanner s=new Scanner(System.in);
year = s.nextInt();
//if statement results true
if(year%400==0 || year%100 ==0 || year%4==0){
System.out.println("Entered number is leap year");
}
//else statement results false
else{
System.out.println("it is not a leap year");
}
}
}
[/java]
Output
[java]Enter the year
2006
it is not a leap year[/java]
The following are the some differences between if and else if.
- The if condition is mandatory for a conditional construct. An else if condition cannot exist without a preceding if block.
- User can have only one “if” block but multiple “else if” blocks.
- Both “if” and “else if” are conditional expressions that help in the decision-making
process.
The
switch statement is executes one statement from multiple conditions. Following is the conceptual figure to under stand the switch concept. In the following figure compiler checks the every condition if it is true prints the statement otherwise checks the case until find the solution.
Following is an example.
[java]class Switch{
public static void main(String[] args) {
int number=2;
switch(number){
case 1: System.out.println("100");break;
case 2: System.out.println("200");break;
case 3: System.out.println("300");break;
default:System.out.println("Not in 100, 200 or 300");
}
}
} [/java]
Output
When compile the code following is an example.
[java]200[/java]
The following are the features of
switch case.
- It assesses the value of an expression and a piece of code is chosen on the premise of that assessed expression.
- Each case alludes back to the first expression.
- The data type that can be utilized as a part of switch expression is integer only.