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


4
టాస్క్
Java Syntax,  స్థాయిపాఠం
లాక్ చేయబడింది
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
   }
}
కనిష్టంగా రెండు సంఖ్యలను ప్రదర్శిస్తోంది