Flow Control in Java - if, if-else-if, if-else
Using Flow control in Java to control the flow of execution of the program based on certain conditions. It we can select a number of statements to be executed after a condition is met.
In Java we use the following to control the flow of the program:
- if
- if-else
- if-else-if
- Nested if etc.
1. if statement:
It is used to decide whether a certain statement or block of code should be executed or not based on a condition. If the condition is true then the block of statement should be executed otherwise not.
Syntax:
if(condition){
//statements will execute only if condition is true
}
For Example:
int date = 1;
int month = 1;
if(date==1 && month ==1){
System.out.println("Happy New Year");
}
Output: Happy New Year
2. if-else statement:
It is used to decide whether a certain statement or block of code should be executed or not based on a condition. But what if the condition is false and we need to execute some statements in that case. We can use else part to execute a block of statements when the condition is false.
Syntax:
if(condition){
//statements will execute only if condition is true
}else{
//statements will execute only if codition is false
}
For Example: To check whether a given number is even or odd
int num = 13;
if(num%2==0){
System.out.println("Number is even");
}
else{
System.out.println("Number is odd");
}
Output: Number is odd
3. if-else-if statement:
When a user is given multiple conditions and based on those conditions they have to execute a certain block of code.
Here if statements will execute from top to bottom until a condition is met. Once a condition is met it will execute the statements and bypass rest of if -else blocks.
- In case if none of the condition is true then the final else block will be executed.
Syntax:
if(condition-one){
//statements will execute only if condition-one is true
}else if(condition-two){
//statements will execute only if codition-two is true
}else{
//statements will execute if none of the above conditions are true
}
Note: There can be any number of else-if block
For Example:
int percentage = 65;
if(precentage>90 && percentage<=100){
System.out.println("Grade A");
}
else if(precentage>80 && percentage<=90){
System.out.println("Grade B");
}
else if(precentage>70 && percentage<=80){
System.out.println("Grade C");
}
else if(precentage>60 && percentage<=70){
System.out.println("Grade D");
}else{
System.out.println("Fail");
}
Output: Grade D
4. Nested if statement:
When if statement is used inside another if statement then it is called nesting of if statements.
Syntax:
if(condition-one){
//statements will execute only if condition-one is true
if(condition-two){
//statements will execute only if condition is true
}
}
For Example: Check whether a given year is Leap Year or not
int year = 1900;
boolean leapYear = false;
// if the year is divided by 4
if (year % 4 == 0) {
// if the year is century
if (year % 100 == 0) {
// if year is divided by 400
// then it is a leap year
if (year % 400 == 0)
leapYear = true;
else
leapYear = false;
}
// if the year is not century
else
leapYear = true;
}
else
leapYear = false;
if (leapYear)
System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");
}
}
Output: 1900 is not a leap year.
Continue learning: break and continue statements
Comments
Post a Comment