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சிவப்பு நிறத்தில் முன்னிலைப்படுத்தப்படுகின்றன. கூடுதலாக, சுருள் பிரேஸ்கள் சந்தேகத்திற்கு இடமின்றி வைக்கப்படுகின்றன, என்ன செயல்கள் செய்யப்படும் என்பதை தெளிவாகக் காட்டுகிறது. நீங்கள் வேலை செய்ய வேண்டிய அவசியமில்லை என்ற சரம், 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.

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
   }
}
குறைந்தபட்சம் இரண்டு எண்களைக் காட்டுகிறது