1. ifकथनों का क्रम

कभी-कभी किसी प्रोग्राम को चर के मान या अभिव्यक्ति के मान के आधार पर कई अलग-अलग क्रियाएं करने की आवश्यकता होती है।

मान लीजिए कि हमारा काम कुछ ऐसा है:

  • अगर तापमान 20डिग्री से ज्यादा है तो शर्ट पहन लें
  • यदि तापमान 10डिग्री से अधिक और (या इसके बराबर) से कम है 20, तो स्वेटर पहन लें
  • यदि तापमान 0डिग्री से अधिक और (या इसके बराबर) से कम है 10, तो रेनकोट पहनें
  • अगर तापमान 0डिग्री से कम है तो कोट लगाएं।

यहां बताया गया है कि कोड में इसका प्रतिनिधित्व कैसे किया जा सकता है:

int temperature = 9;

if (temperature > 20) {
   System.out.println("put on a shirt");
} else { // Here the temperature is less than (or equal to) 20
   if (temperature > 10) {
      System.out.println("put on a sweater");
   } else { // Here the temperature is less than (or equal to) 10
      if (temperature > 0) {
         System.out.println("put on a raincoat");
      } else // Here the temperature is less than 0
         System.out.println("put on a coat");
   }
}

If-elseबयानों को एक दूसरे के भीतर नेस्टेड किया जा सकता है । यह एक कार्यक्रम में बल्कि जटिल तर्क को लागू करना संभव बनाता है।

हालाँकि, प्रोग्रामर आमतौर पर इसे थोड़ा अलग तरीके से लिखते हैं:

int temperature = 9;

if (temperature > 20) {
   System.out.println("put on a shirt");
} else if (temperature > 10) { // Here the temperature is less than (or equal to) 20
   System.out.println("put on a sweater");
} else if (temperature > 0) { // Here the temperature is less than (or equal to) 10
   System.out.println("put on a raincoat");
} else { // Here the temperature is less than 0
   System.out.println("put on a coat");
}

दिए गए दो उदाहरण समतुल्य हैं, लेकिन दूसरे को समझना आसान है।


2. elseब्लॉक की बारीकियां

एक महत्वपूर्ण बिंदु:

यदि किसी निर्माण में घुंघराले ब्रेसिज़ का उपयोग नहीं किया जाता है if-else, तो elseनिकटतम पिछले को संदर्भित करता है if

उदाहरण:

हमारा कोड यह कैसे काम करेगा
int age = 65;

if (age < 60)
   if (age > 20)
      System.out.println("You must work");
else
   System.out.println("You don't have to work");
int age = 65;

if (age < 60) {
   if (age > 20)
     System.out.println("You must work");
   else
     System.out.println("You don't have to work");
}

यदि आप बाईं ओर कोड देखते हैं, तो ऐसा लगता है कि स्क्रीन आउटपुट "आपको काम करने की ज़रूरत नहीं है" होगा। लेकिन ऐसा नहीं है। हकीकत में, elseब्लॉक और "आपको काम करने की ज़रूरत नहीं है" कथन दूसरे (करीब) ifकथन से जुड़ा हुआ है।

दाईं ओर कोड में, संबंधित ifऔर elseलाल रंग में हाइलाइट किए गए हैं। इसके अतिरिक्त, घुंघराले ब्रेसिज़ को स्पष्ट रूप से रखा गया है, यह स्पष्ट रूप से दिखा रहा है कि कौन से कार्य किए जाएंगे। जिस स्ट्रिंग पर आपको काम नहीं करना है वह कभी भी प्रदर्शित नहीं होती है जब ageवह से अधिक होती है 60


4
टास्क
Java Syntax,  स्तर 4सबक 1
लॉक
This age doesn't work for me…
Sometimes we all want to change our age. First, they don't want to sell you cigarettes or beer. Then your hair starts thinning and your back aches! A programmer may not have control over time, but he or she has total control over data in programs. In this task, we correct an error so that a Person object's age variable receives a different value.

3. एक if-elseकथन का उपयोग करने का उदाहरण

चूँकि हमने इस if-elseकथन का इतनी अच्छी तरह से अन्वेषण किया है, आइए एक उदाहरण दें:

import java.util.Scanner;
public class Solution {
   public static void main(String[] args) {
     Scanner console = new Scanner(System.in); // Create a Scanner object
     int a = console.nextInt(); // Read the first number from the keyboard
     int b = console.nextInt(); // Read the second number from the keyboard
     if (a < b)                   // If a is less than b
       System.out.println(a);     // we display a
     else                         // otherwise
       System.out.println(b);     // we display b
   }
}
कम से कम दो नंबर प्रदर्शित करना