• Post Reply Bookmark Topic Watch Topic
  • New Topic

I can't make sense of the following  RSS feed

 
Nadya Jawahir
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have trouble understanding why a certain statement in the following code is supposed to go where it has to go..

Code:

The first time I followed the assignment I didn't really write the code anywhere near how the tutorial answers show them but I made sense of that now and think I can move on. The only thing I'm really struggling with to understand is why the code "beerNum = beerNum - 1" doesn't go inside the brackets of any of the two if-statements. I feel like somehow it's more logical to put them in the if-statement. I don't know whether or not that's true.

I hope you can make sense of what I am trying to say. And help me move on.

Thank you in advance.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would happen if the reduction in the number of bottles remaining were inside the if? What number would appear the first time the loop runs? What about the second time? Or the third time?
 
Nadya Jawahir
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What would happen if the reduction in the number of bottles remaining were inside the if? What number would appear the first time the loop runs? What about the second time? Or the third time?

Oh.. uhmm.. the first time the loop runs I think it would say that "beerNum = beerNum -1;" is not something the if statement can check?

I don't know. Honestly I don't know. I'm so lost and I think it might just be a good idea if I find something else to learn. I don't think anyone with a programmers mind would be stuck at such simple logic.. I just can't make sense of it..
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initially beerNum has a value of 99.

It enters the while loop by saying, do something while beerNum is greater than 0.

If you were to place beerNum = beerNum - 1 to an if statement's body, which needs to meet a condition beerNum == 1 --> true in order to execute its body, that means beerNum will never get to that point, as beerNum would never get decremented. Having said that, it would run forever such loop.
If you were have this beerNum decrement statement in second if statement part (not else), loop wouldn't be an infinite, but that would cause other problems. Experiment and you'll see.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras is right. Try putting the decrement statement in the two if statements followed by a print call:-
beerNum = beerNum - 1;
System.out.printf("On line 1234567: beerNum = %d%n", beerNum);
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nadya Jawahir wrote:I feel like somehow it's more logical to put them in the if-statement. I don't know whether or not that's true.

Logic is not wishy-washy. It's either true or false. To paraphrase Yoda in Star Wars: The Empire Strikes Back, "No 'feel like somehow' there is in Logic. Either true it is or not. Know, or know not."

Here's another way to look at it:

Two lines above that statement circled in red, the program does this:
That means, take one bottle of beer down from the wall, right? What does beerNum represent? It represents the number of beers on the wall. So, what happens to that number when you take one bottle down? It gets reduced by 1, right? How do you reduce the value of beerNum by 1? You do this:So, the statement that you have encircled in red essentially does what the statement two lines before it says: it takes one bottle of beer off the wall.

Is the act of taking one beer down from the wall predicated by any condition? That is, do you need to check anything before taking a bottle down from the wall? What are those two if statements doing? Are they checking anything that would prevent you from taking a beer down from the wall? They're not, right? 

The first if-statement checks if we only have 1 bottle of beer left on the wall. If that's true, then we change the variable word from "bottles" to "bottle" to keep the output grammatically correct, because we don't want our program to look stupid and say "1 bottles of beer on the wall" otherwise. The second if-statement is just another refinement of staying grammatically correct.  If there are no more bottles of beer on the wall, we want to change the entire message to "No more bottles of beer on the wall." Neither of these checks controls whether or not we take a bottle of beer off the wall. Rather, they are checking how many beers are left on the wall and adjusting the output so that it's grammatically correct. Therefore, why should you put the command to reduce the number of beers on the wall inside one of those conditional statements? You shouldn't, right?

Does that make more sense now, young padawan?
 
Nadya Jawahir
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Nadya Jawahir wrote:I feel like somehow it's more logical to put them in the if-statement. I don't know whether or not that's true.

Logic is not wishy-washy. It's either true or false. To paraphrase Yoda in Star Wars: The Empire Strikes Back, "No 'feel like somehow' there is in Logic. Either true it is or not. Know, or know not."

Here's another way to look at it:

Two lines above that statement circled in red, the program does this:
That means, take one bottle of beer down from the wall, right? What does beerNum represent? It represents the number of beers on the wall. So, what happens to that number when you take one bottle down? It gets reduced by 1, right? How do you reduce the value of beerNum by 1? You do this:So, the statement that you have encircled in red essentially does what the statement two lines before it says: it takes one bottle of beer off the wall.

Is the act of taking one beer down from the wall predicated by any condition? That is, do you need to check anything before taking a bottle down from the wall? What are those two if statements doing? Are they checking anything that would prevent you from taking a beer down from the wall? They're not, right? 

The first if-statement checks if we only have 1 bottle of beer left on the wall. If that's true, then we change the variable word from "bottles" to "bottle" to keep the output grammatically correct, because we don't want our program to look stupid and say "1 bottles of beer on the wall" otherwise. The second if-statement is just another refinement of staying grammatically correct.  If there are no more bottles of beer on the wall, we want to change the entire message to "No more bottles of beer on the wall." Neither of these checks controls whether or not we take a bottle of beer off the wall. Rather, they are checking how many beers are left on the wall and adjusting the output so that it's grammatically correct. Therefore, why should you put the command to reduce the number of beers on the wall inside one of those conditional statements? You shouldn't, right?

Does that make more sense now, young padawan?
Oh my gosh. Can you sit next to me all day and explain it like that cause you just made it CLICK in my head. It really does make sense now, thank you old padawan.
 
Nadya Jawahir
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Liutauras is right. Try putting the decrement statement in the two if statements followed by a print call:-
beerNum = beerNum - 1;
System.out.printf("On line 1234567: beerNum = %d%n", beerNum);


Thanks a lot for your help.
 
Nadya Jawahir
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Initially beerNum has a value of 99.

It enters the while loop by saying, do something while beerNum is greater than 0.

If you were to place beerNum = beerNum - 1 to an if statement's body, which needs to meet a condition beerNum == 1 --> true in order to execute its body, that means beerNum will never get to that point, as beerNum would never get decremented. Having said that, it would run forever such loop.
If you were have this beerNum decrement statement in second if statement part (not else), loop wouldn't be an infinite, but that would cause other problems. Experiment and you'll see.


Thank you!

You guys are amazing and so helpful. I owe you!!!
 
Nadya Jawahir
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[long unnecessary quote removed]

Hey 1 more thing.

I don't want to be annoying but I just want to take my chance cause if I don't ask it now I probably won't figure it out on my own..

So I wanted to experiment a bit more and seemed to understand most of the errors I got.. only there was one logical error which I didn't really understand.

I put

beerNum = beerNum - 1

above the following code

System.out.println(beerNum + " "+ word + " of beer on the wall");
System.out.println(beerNum + " " + word + " of beer.");
System.out.println("Take one down.");
System.out .println("Pass it around. ");

so like this:

beerNum = beerNum - 1
System.out.println(beerNum + " "+ word + " of beer on the wall");
System.out.println(beerNum + " " + word + " of beer.");
System.out.println("Take one down.");
System.out .println("Pass it around. ");

The logical error it gave me was: instead of starting at 99 bottles it started at 98. Now what I don't understand is, is why it still runs.. but starts at 98..

I think the problems I'm having stem from the lack of understanding the syntax and the order in which stuff has to go, do you have any recommendations for me?

I am so sorry for continuously bothering you guys. I'm truly embarrassed.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Statements will generally execute in the order that they appear in a method, from top to bottom. Control Flow Statements like if-else, for-loops, and while-loop allow you to alter that flow.

So, if you change the variable then display it, that will give you a different result than if you first display the value then reduce it.
 
Nadya Jawahir
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Statements will generally execute in the order that they appear in a method, from top to bottom. Control Flow Statements like if-else, for-loops, and while-loop allow you to alter that flow.

So, if you change the variable then display it, that will give you a different result than if you first display the value then reduce it.


Got it! Thanks a lot for your time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!