Chapter 4- Conditionals: If-else Statement in Java



Sometimes we want to watch comedy videos on YouTube If the day is Sunday.

Sometimes, we order junk food if it is our friend’s birthday in the hostel.

You might want to buy an umbrella if it’s raining and you have the money.

You order the meal if aloo or your favorite bhindi is listed on the menu.

All these are decisions which depends on a certain condition being met. In java, we can execute instructions on a conditional being met.


Decision-making instructions in Java

  • If-Else Statement
  • Switch Statement


  • If-Else Statement

The syntax of an if-else statement in C looks like that of C++ and JavaScript. Java has a similar syntax too. It looks like:



/* if (condition-to-be-checked) {
statements-if-condition-true;
}
else {
statements-if-condition-false;
} */

Code Example :


int a = 29;
if (a>18) {
System.out.println(“You can drive”);
}
Note that : Else block is Optional

Aryadeep

Welcome to CodeWithARYA. It's My Personal Reference Website. Where You Can Get Amazing Codes. Have a Look.

Post a Comment

Previous Post Next Post