Java If-else Statement is used to test condition.It boolean true is returned,the code in between the if statement is executed. Java If-el...
Java If-else Statement is used to test condition.It boolean true is returned,the code in between the if statement is executed.
Example:
[
Java If-else Statement Syntax.
[if(condition)
{
//code to execute if boolean true is returned
}]
Types of if statements
- if statement
- if-else statement
- nested if statement
- if-else-ladder
Java if Statement example
[
public class JavaIfStatement { public static void main(String args[]) { int age=30; if(age>=18) { System.out.println("Your Illegiable to be a voter"); } } }
]
Java if-else statement
Java if-else statement executes code if boolean true is returned.If false is returned, else code is executed.
Syntax
[
if(condition to test)
{ //execute this code } else { //execute this code
}
]
Example:
[
public class JavaIfStatement { public static void main(String args[]) { int age=13; if(age>=18) { System.out.println("Your Illegiable to be a voter"); } else { System.out.println("You cannot vote..too young"); } } }]
Java if-else-if ladder Statement
In the if-else-if ladder, one condition is executed from multiple statements.
Syntax:
[
if(condition 1) { //code to execute } else if(condition 2) { //code to execute } else if(condition 3) { //code to execute } . . . else { //code to execute }
]
Example:
[
public class JavaIfStatement { public static void main(String args[]) { int score=30; if(score<50) { System.out.println("Your grade is D"); } else if(score>50&&score<=60) { System.out.println("Your grade is C"); } else if(score>60&&score<=70) { System.out.println("Your grade is B"); } else if(score>70&&score<=80) { System.out.println("Your grade is A-"); } else { System.out.println("Your grade is A"); } } }
]