if
1. அறிக்கைகளின் வரிசை
சில நேரங்களில் ஒரு நிரல் ஒரு மாறியின் மதிப்பு அல்லது வெளிப்பாட்டின் மதிப்பைப் பொறுத்து பல்வேறு செயல்களைச் செய்ய வேண்டும்.
எங்கள் பணி இது போன்றது என்று வைத்துக்கொள்வோம்:
- வெப்பநிலை
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
?
if-else
3. அறிக்கையைப் பயன்படுத்துவதற்கான எடுத்துக்காட்டு
நாங்கள் அறிக்கையை நன்றாக ஆராய்ந்ததால் 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
}
}