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;
		}
	}

 

Java if else statement
Java if else statement

 
 

if statement

Java if statement
Java 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

nested-if
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

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");
	      }

 

By coderss

Leave a Reply

Your email address will not be published. Required fields are marked *