డూ-వేల్ లూప్

అందుబాటులో ఉంది

1. రివర్స్ లూప్

జావాలో మరొక రకమైన whileలూప్ ఉంది - do-whileలూప్. ఇది సాధారణ whileలూప్‌తో సమానంగా ఉంటుంది మరియు కేవలం రెండు భాగాలను మాత్రమే కలిగి ఉంటుంది: ఒక "కండిషన్" మరియు "లూప్ బాడీ". పరిస్థితి ఉన్నంత వరకు లూప్ బాడీ మళ్లీ మళ్లీ అమలు చేయబడుతుంది true. సాధారణంగా, do-whileలూప్ ఇలా కనిపిస్తుంది:

do
   statement;
while (condition);

లేదా

do
{
   block of statements
}
while (condition);

లూప్ కోసం while, ఎగ్జిక్యూషన్ యొక్క క్రమం: కండిషన్ , లూప్ బాడీ , కండిషన్ , లూప్ బాడీ , కండిషన్ , లూప్ బాడీ , ...

కానీ do-whileలూప్ కోసం, ఇది కొద్దిగా భిన్నంగా ఉంటుంది: లూప్ బాడీ , కండిషన్ , లూప్ బాడీ , కండిషన్ , లూప్ బాడీ , ...

వాస్తవానికి, whileలూప్ మరియు do-whileలూప్ మధ్య ఉన్న ఏకైక వ్యత్యాసం ఏమిటంటే, లూప్ బాడీ లూప్ కోసం కనీసం ఒక్కసారైనా అమలు చేయబడుతుంది do-while.


do-while2. లూప్ ఉపయోగించడం వల్ల కలిగే ప్రయోజనాలు

do-whileప్రాథమికంగా, లూప్ మరియు లూప్ మధ్య ఉన్న ఏకైక వ్యత్యాసం whileఏమిటంటే, లూప్ యొక్క శరీరంdo-while కనీసం ఒక్కసారైనా అమలు చేయబడుతుంది.

సాధారణంగా, do-whileలూప్ బాడీని అమలు చేయకపోతే లూప్ పరిస్థితిని తనిఖీ చేయడంలో అర్థం లేనప్పుడు లూప్ ఉపయోగించబడుతుంది. ఉదాహరణకు, లూప్ బాడీలో నిర్దిష్ట గణనలను నిర్వహించి , ఆపై కండిషన్‌లో ఉపయోగించినట్లయితే .

ఉదాహరణ:

exitపదాన్ని నమోదు చేసే వరకు ప్రోగ్రామ్ కీబోర్డ్ నుండి పంక్తులను చదువుతుంది

అయితే అయితే చేయండి
String s;
while (true)
{
   s = console.nextLine();
   if (s.equals("exit"))
      break;
}
String s;
do
{
   s = console.nextLine();
}
while (!s.equals("exit"));

లూప్‌లోని breakమరియు స్టేట్‌మెంట్‌లు లూప్‌లో ఉన్న విధంగానే పని చేస్తాయి .continuedo-whilewhile


3. లూప్‌లను పోల్చడం do-while: జావా vs పాస్కల్

మరోసారి, పాస్కల్ లూప్ యొక్క అనలాగ్ను కలిగి ఉంది do-while, కానీ దానిని లూప్ అంటారు repeat-until. అలాగే, ఇది లూప్ నుండి కొద్దిగా భిన్నంగా ఉంటుంది do-while. లూప్‌లో repeat-until, లూప్‌ను ఎప్పుడు కొనసాగించాలో కాకుండా ఎప్పుడు నిష్క్రమించాలో కండిషన్ సూచిస్తుంది.

ఉదాహరణలు:

పాస్కల్ జావా
Repeat
   ReadLn(s);
Until s = 'exit';
String s;
do {
   s = console.nextLine();
}
while ( !s.equals("exit") );

జావాతో పోలిస్తే, పాస్కల్ దీన్ని సూచించే విధానం చాలా అందంగా ఉంది. మేము పాస్కల్ నుండి ఉదాహరణలతో ప్రారంభించాలి, లేకపోతే మీరు నవ్వుతారు.


2
టాస్క్
జావా సింటాక్స్,  స్థాయిపాఠం
లాక్ చేయబడింది
Cat's finalize method
It's difficult to accidentally lose an object: as long as you have even one reference to an object, it remains alive. But if not, then the object is approached by the finalize method, an unpredictable assassin that works for the Java machine. Let's create this method ourselves: protected void finalize() throws Throwable. The last two words will become clear a little later.
2
టాస్క్
జావా సింటాక్స్,  స్థాయిపాఠం
లాక్ చేయబడింది
Zombie cats, zombie dogs
The Grim Reaper has come to CodeGym. In the previous level, we often "constructed" cats and dogs (using the constructors of the Cat and Dog classes) and created new instances of cats and dogs (objects). Now it's time to destroy objects that no one is referencing. In the Cat and Dog classes, write a finalize method, which displays text about the destruction of objects.
2
టాస్క్
జావా సింటాక్స్,  స్థాయిపాఠం
లాక్ చేయబడింది
Cat and Dog objects: 50,000 each
If you have not yet been touched by the beauty of loops in programming, try finishing this task without them. Of course, this task isn't so much about loops as it is about the destruction of unused objects. Anyway, create 50,000 each of Cat and Dog objects in a loop. Doing this will force the Java machine to call the finalize method at least a few times.
2
టాస్క్
జావా సింటాక్స్,  స్థాయిపాఠం
లాక్ చేయబడింది
Cat counter
There is balance in the universe. Stuff arrives somewhere and departs from somewhere else. It's not entirely clear what is flowing through our galaxy or where it is going. But it doesn't work this way with a computer and its discrete memory. Accordingly, you need to edit a couple of important things in this program. In the Cat class constructor, we will increment a counter by 1. Then in the finalize method, we'll decrement it.
10
టాస్క్
జావా సింటాక్స్,  స్థాయిపాఠం
లాక్ చేయబడింది
Even and odd digits
Let's determine how many even digits and how many odd digits are in a number entered from the keyboard. If a number is divisible by 2 without a remainder (i.e. the remainder is zero), then it is even. And we'll increase the even digit counter (static variable even) by 1. Otherwise, the number is odd, so we'll increase the odd digit counter (static variable odd).
వ్యాఖ్యలు
  • జనాదరణ పొందినది
  • కొత్తది
  • పాతది
వ్యాఖ్యానించడానికి మీరు తప్పనిసరిగా సైన్ ఇన్ చేసి ఉండాలి
ఈ పేజీకి ఇంకా ఎలాంటి వ్యాఖ్యలు లేవు