if1. विधानांचा क्रम

कधीकधी प्रोग्रामला व्हेरिएबलच्या मूल्यावर किंवा अभिव्यक्तीच्या मूल्यावर अवलंबून अनेक भिन्न क्रिया करण्याची आवश्यकता असते.

समजा आमचे कार्य असे काहीतरी आहे:

  • जर तापमान 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लाल रंगात हायलाइट केलेले आहेत. याव्यतिरिक्त, कुरळे ब्रेसेस अस्पष्टपणे ठेवल्या जातात, स्पष्टपणे कोणत्या क्रिया केल्या जातील हे दर्शवितात. पेक्षा मोठी असताना तुम्हाला काम करण्याची गरज नसलेली स्ट्रिंग कधीही प्रदर्शित होत नाही .age60



if-else3. विधान वापरण्याचे उदाहरण

आम्ही 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
   }
}
किमान दोन संख्या दाखवत आहे