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
}
}
GO TO FULL VERSION