public class BeerSong { public static void main(String args[]) { int BeerNum = 99; String word = "bottles"; while(BeerNum > 0) { System.out.println(BeerNum + " " + word + " of the beer on the wall"); System.out.println("Catch one."); System.out.println("Take around."); BeerNum = BeerNum - 1; if(BeerNum == 1) { word = "bottle"; } if(BeerNum > 0) { System.out.println(BeerNum + " " + word + " of the beer on the wall"); } else { System.out.println("Nothing on the wall"); } } } } 

The question itself is about:

 if(BeerNum == 1) word = "bottle"; } 

If you put it right after the while, then at the end it will be like this: 1 bottles of beer on the wall. 1 bottle of beer on the wall.

Why is the plural value taken for the first line, and the only one for the second? At the same time, if you put this if statement after BeerNum = BeerNum - 1; then both values ​​will be in the singular. Explain. Why such difference? Is there a difference where to put if at the beginning of a block of code or after decreasing? Thank!

  • различие, где ставить if в начале блока кода или после уменьшения? - Do not you think that you yourself answered the question? - Alexey Shimansky
  • In general, I recommend reading about the cycles, what distinguishes, for example, do from for from while and read this Java Debugging (Debugging) and go through the code yourself, looking at what happens there in both cases - Alexey Shimansky
  • Simply if after while, then for the first one it does not work, and for the second one, if, after decreasing, it will fail for all output lines - Dmitriy
  • I just started studying, but I'm trying to understand all the intricacies, thanks!) - Dmitry
  • The if statement has nothing to do with it, just like the magic of its use or its position in space. Elementary sequential algorithm: if berrNum = 1 - output with bottle, if more than one - with bottles, when less than one - output what is over. With each iteration of the cycle, beerNum decreases by 1. If you cannot master the logic of such a simple algorithm (not understanding what happens during a decrease and what it affects), then it is not too late to think about a different place of effort. No coarseness, just not all programming is given, therefore, such salaries. - pavlofff

0