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 // 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");
हालाँकि, प्रोग्रामर आमतौर पर इस निर्माण को थोड़ा अलग तरीके से लिखते हैं:
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
।
उदाहरण:
हमारा कोड | यह कैसे काम करेगा |
---|---|
|
|
यदि आप बाईं ओर कोड देखते हैं, तो ऐसा लगता है कि स्क्रीन आउटपुट "आपको काम करने की ज़रूरत नहीं है" होगा। लेकिन ऐसा नहीं है। हकीकत में, else
ब्लॉक और "आपको काम करने की ज़रूरत नहीं है" कथन दूसरे (करीब) if
कथन से जुड़ा हुआ है।
दाईं ओर कोड में, संबंधित if
और else
लाल रंग में हाइलाइट किए गए हैं। इसके अतिरिक्त, घुंघराले ब्रेसिज़ को स्पष्ट रूप से रखा गया है, यह स्पष्ट रूप से दिखा रहा है कि कौन से कार्य किए जाएंगे। क्या आपको जिस स्ट्रिंग पर काम नहीं करना है, वह कभी भी प्रदर्शित नहीं होती है जब age
वह इससे बड़ी होती है 60
?
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
}
}
GO TO FULL VERSION