if else statement किसी भी programming language का सबसे important feature है , Java If else conditions के according किसी Code Of Block को run करने के लिए use होता है।Means जब हमें Condition True होने पर कोई दूसरा code run करना हो condition गलत होने पर कुछ और तब हम If else का use करते हैं।


Java में if else की कुछ forms नीचे define की गयी हैं।

  • if Statement
  • if else Statement
  • if else if (or else if) Statement

Java if Statement

If statement , सबसे basic decision making statement है जिसमें किसी particular condition के true होने पर ही code of block run होता है।

if(condition){
  // code of block.
}

Java if Example

File : IfElse.java

CopyFullscreenClose FullscreenRun
public class IfElse {
  public static void main(String[] args) {
    int age = 25;
    if(age >= 18) { 
      System.out.println("Yes ! You are eligible to vote.");
    }
  }
}
Output
javac IfElse.java
java IfElse
Yes ! You are eligible to vote.

Note : Java programming language में if else statements में use किये जाने वाले expressions हमेशा boolean value (true / false) generate करनी चाहिए। boolean value के according compiler decide करता है कि if block run होगा या else . अगर if statement में pass किया गया expression boolean value नहीं generate करता है तो कुछ इस तरह से error आती है।

// just pass an integer value.
if(89) { 
   System.out.println("Yes ! You are eligible to vote.");
}

error: incompatible types: int cannot be converted to boolean
    if(89) { 
       ^
1 error

किसी expression द्वारा boolean value (true / false) return करने लिए Comparison और Logical Operators का use किया जाता है। Comparison Operators दी हुई दो values को compare करके condition के according boolean value true / false value return करते हैं।

OperatorName
==Equal
!=Not equal
<Less than
>Greater than
<=Less than or equal
>=Greater than or equal
&&And
!Not
||Or

Java If Else

जब हमें Condition true होने पर कोई दूसरा code run करना हो और condition false होने पर कुछ और तब हम If else statement का use करते हैं।

Java if else Example

File : IfElse.java

CopyFullscreenClose FullscreenRun
public class IfElse {
  public static void main(String[] args) {
    int age = 17;
    if(age >= 18) { 
      System.out.println("Yes ! You are eligible to vote.");
    }
    else {
      System.out.println("Sorry ! You are not eligible to vote.");
    }
  }
}
Output
javac IfElse.java
java IfElse
Sorry ! You are not eligible to vote.

Important
अगर if else के code of block में कोई single line code है तो आप if और else के बाद use होने वाले curly brackets को skip कर सकते हैं। लेकिन ध्यान रहे सिर्फ single line के code के लिए , एक से ज्यादा lines होने पर error आ जाएगी।

int age = 25;
if(age >= 18)
  System.out.println("Yes ! You are eligible to vote.");
else
  System.out.println("Sorry ! You are not eligible to vote.");

Java if else if

ऊपर दिए गए दोनों statements (if और if else) को हम तब use करते हैं जब कोई single condition के accordingly code of block run करना हो , एक से ज्यादा condition के accordingly code of block run के लिए हम if else if Statement use करते हैं।

File : IfElseIF.java

CopyFullscreenClose FullscreenRun
public class IfElseIF {
  public static void main(String[] args) {
    int marks = 86;
    if(marks >= 90) {
      System.out.println("Grade : A+");
    }
    else if(marks >= 80) {
      System.out.println("Grade : A");
    }
    else if(marks >= 70) {
      System.out.println("Grade : B");
    }
    else {
       System.out.println("Grade : C");
    }
  }
}
Output
javac IfElseIF.java
java IfElseIF
Grade : A.

Java if else Short Hand

अगर आपको सिर्फ if else statement की जरूरत है तो आप ternary operator की help ले सकते हैं। इसे आप if else की short hand trick कह सकते हैं , और इसे तब use में लेते हैं जब if else block में execute करने के लिए ज्यादा code नहीं होता।

File : IfElse.java

CopyFullscreenClose FullscreenRun
public class IfElse {
  public static void main(String[] args) {
    int age = 25;
    String result = null;
    // use ternary operators to check condition.
    result = (age >= 18) ? "Yes ! You are eligible to vote." : "Sorry ! You are not eligible to vote.";
    System.out.println(result);
  }
}
Output
javac IfElse.java
java IfElse
Yes ! You are eligible to vote..

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers