If statements:
An if statement is a programming construct that allows a program to choose between different paths of execution based on a specified condition. In the previous program, a message like "6 + 2 = 7 is false" is displayed. If you want the message to be "6 + 2 = 7 is incorrect," you can use a selection statement to make this minor modification.
In Java, there are several types of selection statements available: one-way if statements, two-way if-else statements, nested if statements, multi-way if-else statements, switch statements, and conditional expressions.
A one-way if statement is used to execute a block of code only if a certain condition is true. The syntax for a one-way if statement is as follows:
if (boolean-expression) {
The flowchart in Figure-1a visually represents how Java executes the syntax of an if statement. A flowchart is a diagram that illustrates an algorithm or process, displaying the steps as boxes of different shapes and connecting them with arrows to indicate the flow of control. In a flowchart, a diamond-shaped box represents a Boolean condition, and a rectangular box represents statements or actions.
Fig-1
If the boolean expression evaluates to true, the statements inside the block are executed. Consider the following code as an example:
area = radius * radius * PI;
System.out.println("The area for the circle of radius " + radius + " is " + area);
The flowchart in Figure-1 b represents the execution flow of the above statement. If the value of `radius` is greater than or equal to 0, the area is calculated and the result is displayed. Otherwise, if the value is less than 0, the two statements inside the block will not be executed.
The boolean expression is enclosed in parentheses to clearly define its scope. It is important to ensure that the boolean expression is properly formatted. For example, the code in (a) is incorrect and needs to be corrected as shown in (b):
System.out.println("i is positive");
System.out.println("i is positive");
The curly braces that enclose the block of statements can be omitted if the block consists of a single statement. The following two examples are equivalent:
System.out.println("i is positive");
System.out.println("i is positive");
Both examples will execute the `System.out.println("i is positive")` statement if the condition `(i > 0)` is true.
The following program prompts the user to enter an integer. If the number is a multiple of 5, the program displays "HiFive". If the number is divisible by 2, it displays "HiEven".
- import java.util.Scanner;
- public class SimpleIfDemo {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.println("Enter an integer: ");
- int number = input.nextInt();
- if (number % 5 == 0) {
- System.out.println("HiFive");
- }
- if (number % 2 == 0) {
- System.out.println("HiEven");
- }
- }
- }
In the above program, the `Scanner` class is used to read input from the user. The entered integer is stored in the variable `number`. The program then checks if `number` is divisible by 5 using the condition `number % 5 == 0`. If true, it prints "HiFive". Similarly, it checks if `number` is divisible by 2 using the condition `number % 2 == 0`. If true, it prints "HiEven". If neither condition is true, no message is displayed.
This program demonstrates the use of multiple `if` statements to perform independent checks on the value of `number` and display different messages based on the results.
Comments
Post a Comment