B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Java if...else | Login Technologies

Java if...else Statements

The if...else statement in Java allows your program to make decisions and execute different blocks of code based on whether a condition is true or false. This is essential for controlling program flow, validating inputs, and handling different cases dynamically.

Java में if...else स्टेटमेंट प्रोग्राम को निर्णय लेने और शर्त के true या false होने पर अलग-अलग कोड ब्लॉक चलाने की अनुमति देता है। यह प्रोग्राम के प्रवाह को नियंत्रित करने, इनपुट वैलिडेशन करने और विभिन्न स्थितियों को संभालने के लिए आवश्यक है।

Why do we need if...else?

We need if...else statements because programs often need to choose between two or more actions depending on conditions. Without this decision-making capability, programs would be static and unable to respond differently to different inputs or scenarios.

हमें if...else स्टेटमेंट इसलिए चाहिए क्योंकि प्रोग्राम को अक्सर स्थितियों के आधार पर दो या अधिक कार्यों में से चुनना होता है। बिना इस निर्णय लेने की क्षमता के, प्रोग्राम स्थिर होंगे और विभिन्न इनपुट या परिस्थितियों पर अलग प्रतिक्रिया नहीं दे पाएंगे।

Requirements to use if...else in Java

  • A valid boolean condition/expression inside if.
  • The code blocks for if and optionally else.
  • Proper syntax with braces { } for multiple statements (optional for single statements).
  • if के अंदर एक वैध boolean शर्त/व्यक्ति होनी चाहिए।
  • if और वैकल्पिक रूप से else के लिए कोड ब्लॉक।
  • सही सिंटैक्स जिसमें बहु-वाक्यों के लिए ब्रेसेस { } शामिल हों (एकल वाक्यों के लिए वैकल्पिक)।

1. Check Even or Odd
public class Main {
    public static void main(String[] args) {
        int num = 4;
if (num % 2 == 0) {
    System.out.println("Even");
} else {
    System.out.println("Odd");
}    }
}

Output: Even

Explanation: Checks if the number is even or odd.

व्याख्या: यह जांचता है कि संख्या सम है या विषम।


2. Positive or Negative
public class Main {
    public static void main(String[] args) {
        int num = -5;
if (num >= 0) {
    System.out.println("Positive");
} else {
    System.out.println("Negative");
}    }
}

Output: Negative

Explanation: Determines if the number is positive or negative.

व्याख्या: यह जांचता है कि संख्या धनात्मक है या ऋणात्मक।


3. Voting Eligibility
public class Main {
    public static void main(String[] args) {
        int age = 20;
if (age >= 18) {
    System.out.println("Eligible to vote");
} else {
    System.out.println("Not eligible to vote");
}    }
}

Output: Eligible to vote

Explanation: Checks if age is 18 or more for voting eligibility.

व्याख्या: यह जांचता है कि व्यक्ति मतदान के योग्य है या नहीं।


4. Greater Number
public class Main {
    public static void main(String[] args) {
        int a = 10, b = 15;
if (a > b) {
    System.out.println("a is greater");
} else {
    System.out.println("b is greater");
}    }
}

Output: b is greater

Explanation: Compares two numbers and prints the greater one.

व्याख्या: दो संख्याओं की तुलना करता है और बड़ी संख्या को प्रिंट करता है।


5. Check Leap Year
public class Main {
    public static void main(String[] args) {
        int year = 2024;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
    System.out.println("Leap year");
} else {
    System.out.println("Not a leap year");
}    }
}

Output: Leap year

Explanation: Determines if the year is a leap year using leap year rules.

व्याख्या: यह जांचता है कि वर्ष लीप ईयर है या नहीं।


6. Check Divisibility by 5
public class Main {
    public static void main(String[] args) {
        int num = 25;
if (num % 5 == 0) {
    System.out.println("Divisible by 5");
} else {
    System.out.println("Not divisible by 5");
}    }
}

Output: Divisible by 5

Explanation: Checks if the number is divisible by 5.

व्याख्या: यह जांचता है कि संख्या 5 से विभाज्य है या नहीं।


7. Check Character is Vowel
public class Main {
    public static void main(String[] args) {
        char ch = 'e';
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
    System.out.println("Vowel");
} else {
    System.out.println("Consonant");
}    }
}

Output: Vowel

Explanation: Checks if the character is a vowel.

व्याख्या: यह जांचता है कि वर्ण स्वर है या व्यंजन।


8. Check for Zero
public class Main {
    public static void main(String[] args) {
        int num = 0;
if (num == 0) {
    System.out.println("Zero");
} else {
    System.out.println("Non-zero");
}    }
}

Output: Zero

Explanation: Checks if the number is zero or non-zero.

व्याख्या: यह जांचता है कि संख्या शून्य है या नहीं।


9. Check Marks Pass or Fail
public class Main {
    public static void main(String[] args) {
        int marks = 65;
if (marks >= 40) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}    }
}

Output: Pass

Explanation: Checks if the marks are enough to pass.

व्याख्या: यह जांचता है कि छात्र पास है या फेल।


10. Check Even and Positive
public class Main {
    public static void main(String[] args) {
        int num = 6;
if (num > 0 && num % 2 == 0) {
    System.out.println("Positive Even Number");
} else {
    System.out.println("Not a Positive Even Number");
}    }
}

Output: Positive Even Number

Explanation: Checks if a number is both positive and even.

व्याख्या: यह जाँचता है कि संख्या धनात्मक और सम है या नहीं।


11. Find Max of 3 Numbers
public class Main {
    public static void main(String[] args) {
        int a = 5, b = 8, c = 3;
if (a > b && a > c) {
    System.out.println("a is greatest");
} else if (b > c) {
    System.out.println("b is greatest");
} else {
    System.out.println("c is greatest");
}    }
}

Output: b is greatest

Explanation: Compares three numbers to find the greatest one.

व्याख्या: यह तीन संख्याओं में से सबसे बड़ी संख्या को दर्शाता है।


12. Check Uppercase or Lowercase
public class Main {
    public static void main(String[] args) {
        char ch = 'G';
if (ch >= 'A' && ch <= 'Z') {
    System.out.println("Uppercase Letter");
} else {
    System.out.println("Lowercase Letter");
}    }
}

Output: Uppercase Letter

Explanation: Checks if the character is uppercase or lowercase.

व्याख्या: यह अक्षर कैपिटल है या स्मॉल यह जाँचता है।


13. Check Alphabet or Not
public class Main {
    public static void main(String[] args) {
        char ch = '@';
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
    System.out.println("Alphabet");
} else {
    System.out.println("Not an alphabet");
}    }
}

Output: Not an alphabet

Explanation: Checks if the character is an alphabet letter.

व्याख्या: यह चेक करता है कि दिया गया कैरेक्टर अक्षर है या नहीं।


14. Check Equal Numbers
public class Main {
    public static void main(String[] args) {
        int a = 10, b = 10;
if (a == b) {
    System.out.println("Both numbers are equal");
} else {
    System.out.println("Numbers are not equal");
}    }
}

Output: Both numbers are equal

Explanation: Compares two values to check if they are equal.

व्याख्या: यह दो संख्याओं की समानता को जाँचता है।


15. Positive, Negative or Zero
public class Main {
    public static void main(String[] args) {
        int num = 0;
if (num > 0) {
    System.out.println("Positive");
} else if (num < 0) {
    System.out.println("Negative");
} else {
    System.out.println("Zero");
}    }
}

Output: Zero

Explanation: Checks the sign of the number (positive, negative, or zero).

व्याख्या: यह जाँचता है कि संख्या धनात्मक, ऋणात्मक या शून्य है।