Write about Java if…else Statement

Java if...else Statement

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java if...else Statement
Java if…else Statement

Write about Java if…else Statement

PROGRAM: Java if…else Statement

/* Java if...else Statement */

What is an If-Else Statement in Java?

In Java, an if-else statement helps you control the flow of your program by executing a block of code only when a particular condition is satisfied.
If the condition is true, Java runs the if block.
If the condition is false, Java runs the else block.

This makes your program intelligent and interactive.


🧩 Java If-Else Statement Syntax

if (condition) {
// code to run if condition is true
} else {
// code to run if condition is false
}

🖼️ Diagram of If-Else Statement (Flowchart)

┌──────────────┐
│ Condition │
└───────┬──────┘

┌─────────┴─────────┐
│ True │            False
▼                          ▼
┌────────────────┐    ┌────────────────┐
│ Execute IF │         │ Execute ELSE │
│ block │                │ block │
└────────────────┘    └────────────────┘

This flowchart shows that the condition is checked first.
Depending on true/false, the relevant block runs.


Java Program Using If-Else (Explained Line-by-Line)

Below is a simple Java program that checks whether a number is positive, negative, or zero.


Java Program

public class NumberCheck {
public static void main(String[] args) {int number = 10;

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.”);
}
}
}



🔍 Line-by-Line Explanation

public class NumberCheck {

  • This creates a public class named NumberCheck.

  • All Java programs start with a class.


public static void main(String[] args) {

  • This is the main() method.

  • It is the entry point of every Java program.


int number = 10;

  • A variable named number is created.

  • It stores the value 10 (you can change it anytime).


if (number > 0) {

  • Java checks whether the number is greater than 0.

  • If this condition is true, the following message will be printed.


System.out.println("The number is positive.");

  • Displays: The number is positive.


} else if (number < 0) {

  • When the first condition is false, and this condition is true,
    Java prints the negative message.


System.out.println("The number is negative.");

  • Displays the negative result.


} else {

  • This block runs only when all previous conditions are false.

  • Meaning the number must be 0.


System.out.println("The number is zero.");

  • Displays: The number is zero.

 

Leave a Reply

Your email address will not be published. Required fields are marked *