Java if-else Statement with Example

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.



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:

7. Java Collection framework complete tutorial.
8. Encapsulation in Java with example.
9. Inheritance in Java with example.
10. Polymorphism in Java with example.

Java if-else Statement with Example Java if-else Statement with Example Reviewed by Prashant Srivastava on December 16, 2019 Rating: 5

No comments:

Powered by Blogger.