if-else statement
The if-else statement is basic control flow statement for all programming languages, if you want to execute block of code based on the evaluation of certain condition is true then you can use if-else statement
The below code takes title as a method parameter if the title equals to hardcoded value which is “keysandstrokes” then the method will return true otherwise it returns false
In simple terminology, if else is a conditional statement if the condition is true to execute set of code or else execute another set of code
public boolean isEqual(String title) { if (title.equalsIgnoreCase("keysandstrokes")) { return true; } else { return false; } }
if statement
In the if statement, if the condition evaluates to true then execute the code which is in the curly braces
String title = "Keysandstrokes"; if (title.equalsIgnoreCase("keysandstrokes")) { System.out.println("The title matches"); }
Nested If Statement
You can call if statement inside another if statement something like below
int age = 30; String role = "tester"; if (age > 15 ){ System.out.println("Age is greater than 20"); if(role.equalsIgnoreCase("Tester")) { System.out.println("Tester..."); } }
if-else-if-ladder
int age = 20; if (age < 10) { System.out.println("Age is less than 10"); } else if (age > 10 && age <= 20) { System.out.println("Age is between 10 and 20"); } else { System.out.println("Age is greater than 20"); }