Ad Code

Responsive Advertisement

Basic Java Program - 03

Question 03: Write any program to check multiple conditions using if statement in java.
Download hole Program / Project code, by clicking following link:
List operators used in if conditional statement in java ?
In Java, if statements are used to control the flow of a program based on the evaluation of one or more conditions. To evaluate conditions, various operators are used within the if statement. Here are the common operators used in if conditional statements in Java:
  1. Equality Operators:
    • ==: Checks if two values are equal.
    • !=: Checks if two values are not equal.
    Example:
    if (x == 5) {
    // Code to execute if x is equal to 5
    }
    if (name != "John") {
    // Code to execute if name is not "John"
    }
  2. Relational Operators:
    • >: Greater than
    • <=: Less than or equal to
    • >=: Greater than or equal to
    Example:
    if (age < 18) {
    // Code to execute if age is less than 18
    }
    if (price >= 100) {
    // Code to execute if price is greater than or equal to 100
    }
  3. Logical Operators:
    • && (Logical AND): Combines multiple conditions, and the if statement evaluates to true only if all conditions are true.
    • || (Logical OR): Combines multiple conditions, and the if statement evaluates to true if at least one condition is true.
    • ! (Logical NOT): Negates a condition, so if (!condition) evaluates to true if the condition is false.
    Example:
    if (score >= 70 && attendance >= 80) {
    // Code to execute if the score is 70 or higher and attendance is 80% or higher
    }
    if (age < 18 || hasParentConsent) {
    // Code to execute if the age is less than 18 or there is parent consent
    }
    if (!isFinished) {
    // Code to execute if the task is not finished
    }
  4. Ternary Operator:
    • (condition) ? true_expression : false_expression: Provides a compact way to perform a simple conditional check and assign a value based on the condition.
    Example:
    int discount = (isMember) ? 10 : 0;
These operators are used in if statements to evaluate conditions and make decisions in your Java programs. Depending on the outcome of these conditions, specific code blocks are executed or skipped.
Programming Code:
Following code write in: BJ_P02.py
// Java Program 
import java.util.Scanner;

public class ConditionCheck {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter an integer: ");
        int number = scanner.nextInt();

        if (number > 0) {
            System.out.println("The number is positive.");
        } else if (number < 0) {
            System.out.println("The number is negative.");
        } else {
            System.out.println("The number is zero.");
        }

        if (number % 2 == 0) {
            System.out.println("The number is even.");
        } else {
            System.out.println("The number is odd.");
        }

        if (number > 10 && number % 2 == 0) {
            System.out.println("The number is greater than 10 and even.");
        } else if (number > 10) {
            System.out.println("The number is greater than 10 but not even.");
        } else {
            System.out.println("The number is not greater than 10.");
        }
    }
}

# Thanks For Reading.
Output:

Post a Comment

0 Comments

Ad Code

Responsive Advertisement