In this problem, you will learn about control flow statements using Java if and if...else statements with the help of examples.
In computer programming, we use the if statement to control the flow of the program. For example, if a certain condition is met, then run a specific block of code. Otherwise, run another code.
An if-else statement has the following logical flow:
For example:
class Main { public static void main(String[] args) { int number = 0; // checks if number is greater than 0 if (number > 0) { System.out.println("The number is positive."); } // checks if number is less than 0 else if (number < 0) { System.out.println("The number is negative."); } // if both condition is false else { System.out.println("The number is 0."); } } } Output The number is 0.
In the above example, we are checking whether the number is positive, negative, or zero.
Here, we have two condition expressions:
Here, the value of the number is 0. So both the conditions evaluate to false. Hence the statement inside the body of else is executed.
Task:
Given an integer M perform the following conditional actions: