This Website for Those Students who are searching for a platform to study online in their own Language Hindi, on this website they will learn all courses like IAS, programming, web designing, data science, and competitive exam preparation, etc.


Java If-else Statement

JAVA - If-else Statement

Java If-else Statement





हेलो दोस्तों आज हम java मे Conditions Statements If ... Else के बारे में पढ़ेंगे.

तो दोस्तों Java में आप Logical operation भी कर सकते हो जैसे कोई 2 Variable को Comparison करना हो या कोई Mathematical Logical operation करना हो और जो आपकी Requirement के हिसाब से आप Use कर सकते हो।

जैसे अगर हम कोई Form create कर रहे है जिसमे User की Information होगी पर हम चाहते है की ये फॉर्म वही भर पाये जिसकी Age 18 से ऊपर हो तो आप यहाँ If-else Statement Use कर सकते हो चलिए आपको एक Example की मदत से समझाता हु।



Example :



  
  
  

    //Java Program to demonstate the use of if statement.  

public class IfExample {  

public static void main(String[] args) {  

    //defining an 'age' variable  

    int age=20;  

    //checking the age  

    if(age>18){  

        System.out.print("Age is greater than 18");  

    } 





else{  

        System.out.print("Sorry..! Your Age is not greater than 18");  

    } 



   

}  

}  



  


अब आपको अंदाजा लग गया होगा की If-else Statement का Use कैसे कर सकते है जैसे आपने ऊपर Example में देखा की कैसे हम किसी की age Check कर सकते है की वो 18 का है या नहीं अगर है तो वो आगे फॉर्म भर सकता है और अगर नहीं हो तो हम else Statement का use कर के कोई message print करा सकते है.

ये तो एक Example था आप अपनी Requirement के हिसाब से condition Statement का Use कर सकते हो .

तो चलिए अब इसके type के बारे में विस्तार से जानते है.

Type Of if-else condition Statement



Java मे 3 Type के conditional statements है जो की आप आगे विस्तार में Example के सात जानेंगे।

if

else

if-else

if Statement



if condition Statement का Use हम तब करते है जब हमें किसी Particular Code of block के True होने पर ही executed तो हम वह if Statement का use कर सकते है।

तो चलिए अब Syntax को देखते कैसे इसको define किया जाता है।

ध्यान रहे if keyword को English के lowercase letters में Use करे नहीं तो error आएगा।

Syntax



  
  
  

    if (condition) {

      // block of code to be executed if the condition is true
    
    }
    


  




तो अब Example की मदत से if Statement को देखते है।

Example



  
  
  

    if (20 > 18) {

      System.out.println("20 is greater than 18");
    
    }


  


जैसे आपने Example में देखा की हम if Statement में एक Condition लगाई की अगर 20 18 से बड़ा है तो print हो 20 is greater than 18 है।

अब यहाँ जरूरी नहीं की आप Direct कोई Value if condtion में Use करे आप कोई variables Create कर के आप variables भी Use कर सकते हो जैसे निचे Example में बताया गया है। :



Example



  
  
  

    int x = 20;

    int y = 18;
    
    if (x > y) {
    
      System.out.println("x is greater than y");
    
    }


  


else Statement



तो अब देखते है की else Statement का Use कैसे और कहा किया जाता है।

else statement का Use हम if statement के सात करते है जब if condition false होती है तो हम decide कर सकते है conditon false होने कोनसा code block को executed कराना है।

तो अब इसके Syntax और Example को समझ लेते है।



Syntax



  
  
  

   
if (condition) {

  // block of code to be executed if the condition is true

} else {

  // block of code to be executed if the condition is false

}



  


Example



   

 
int time = 20;

if (time < 18) {

  System.out.println("Good day.");

} else {

  System.out.println("Good evening.");

}





  


else-if Statement



else-if Statement का Use कैसे और कहा किया जाता है।

else if statement का Use हम if और else statement बीच में Define करते है जब हमे एक या एक से ज्यादा condition Statment का Use करना होतो हम else if का use करते है ।

अगर फिर भी आपको समझ में नहीं आया हो तो में आपको एक Example की मदत से समझाता हु।

तो आप मान लीजिये आपने एक Programe बनाया जो User से time input में लेगा और वो time के According वो Greeting करेगा अगर time 10 होगा तो Good Morning और 20 से कम होगा तो Good Afternoon और अगर 20 से ज्यादा होगा तो good Night तो आप देख सकते हो की हमने यहाँ 2 Condition का use करा है else if की मदत से और अगर दोनों false होती है तो else code Block चलेगा।

तो अब इसके Syntax और Example को समझ लेते है।



Syntax



  
  
  

    if (condition1) {

      // block of code to be executed if condition1 is true
    
    } else if (condition2) {
    
      // block of code to be executed if the condition1 is false and condition2 is true
    
    } else {
    
      // block of code to be executed if the condition1 is false and condition2 is false
    
    }


  


Example



  
  
  

int time = 22;

if (time < 10) {

  System.out.println("Good morning.");

} else if (time < 20) {

  System.out.println("Good day.");

} else {

  System.out.println("Good evening.");

}


  

Output

  
  
  

    Good evening.


  
Previous
Next Post »

Quote

"Educationists should build the capacities of the spirit of inquiry, creativity, entrepreneurial and moral leadership among students and become their role model."

Dr. Abdual Kalam