A programming language uses control statements to control the flow of execution of a program. In Java programming, we can control the flow of execution of a program based on some conditions. Java control statements can be put into the following three categories: selection, iteration, and jump.
Output:
you may also like these related posts:
8. Encapsulation in Java with example.
9. Inheritance in Java with example.
10. Polymorphism in Java with example.
Java if-else Statement:
Java if statement is used to decide whether a certain statement or block of the statement will be executed or not. If a certain condition is true, then the block of statement is executed, otherwise not. But, if you want to do something else if the condition is false then we should use else statement. We can use else statement with if statement to execute the block of code when the condition is false.
Syntax:
if(condition){
//code for if statement
}
else{
//code for else statement.
}
Flowchart:
Java if-else statement example:
class ifElseExample{
public static void main(String args[]){
int a = 10;
if( a%2 == 0){
System.out.println("Number is even.");
}
else{
System.out.println("Number is odd.");
}
}
}
Output:
Number is even
you may also like these related posts:
1. Types of Operators in Java with example.
2. Basic data types in Java with example.
3. How to set path in java.
4. Types of variables in Java with example.
5. Java Hello World program | java first program.
6. Top 100 java interview question and answer.
7. Java Collection framework complete tutorial.2. Basic data types in Java with example.
3. How to set path in java.
4. Types of variables in Java with example.
5. Java Hello World program | java first program.
6. Top 100 java interview question and answer.
8. Encapsulation in Java with example.
9. Inheritance in Java with example.
10. Polymorphism in Java with example.
Java if-else Statement with Example
Reviewed by Prashant Srivastava
on
December 16, 2019
Rating:
No comments: