Slide 1
Slide 2
Slide 3

If statement in java programming (V34)

No Comments

Decision Making

Decision-making structures contain one or more conditions that are evaluated or tested by the program, along with one or more statements that will be executed if the condition is evaluated as true, and optionally, other statements that will not be executed if the condition is evaluated as false.
Below is the general form of a typical decision-making structure found in most programming languages:

  Flow Diagram


Statement Description
if statement The if statement is a conditional control structure that allows the program to execute a block of code only if the specified condition is true.
if...else statement The if-else statement is used for decision making. It allows the program to execute the if block if the condition is true and a different block if the condition is false.
nested if statement You can use an if or else if statement inside another if or else if statement.
switch statement The switch statement allows a variable to be tested for equality against a list of values.

If Statement

The if statement is a conditional control structure that allows the program to execute a block of code only if the specified condition is true.

Flow Diagram


Source Code:

Copied!
import java.util.Scanner;

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

        System.out.print("Enter a number: ");
        int num = scanner.nextInt();

        if (num % 2 == 0) {
            System.out.println("The number " + num + " is even.");
        }
        if (num % 2 != 0) {
            System.out.println("The number " + num + " is odd.");
        }
        scanner.close();
    }
}

Result:

Source Code:

Copied!
import java.util.Scanner;
public class StudentGrades {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Taking input as float for marks
        System.out.print("Enter marks: ");
        float marks = scanner.nextFloat();

        // Grade and feedback based on the marks
        if (marks >= 90 && marks <= 100) {
            System.out.println("Grade: A+");
            System.out.println("Feedback: Excellent");
        }
        if (marks >= 80 && marks < 90) {
            System.out.println("Grade: A");
            System.out.println("Feedback: Very Good");
        }
        if (marks >= 70 && marks < 80) {
            System.out.println("Grade: B");
            System.out.println("Feedback: Good");
        }
        if (marks >= 60 && marks < 70) {
            System.out.println("Grade: C");
            System.out.println("Feedback: Better");
        }
        if (marks >= 50 && marks < 60) {
            System.out.println("Grade: D");
            System.out.println("Feedback: Poor");
        }
        if (marks < 50 && marks >= 0) {
            System.out.println("Grade: F");
            System.out.println("Feedback: Failed");
        }
        if (marks > 100 || marks < 0) {
            System.out.println("Invalid Score! Please try again.");
        }

        scanner.close();
    }
}

Result:

Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top