Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# I can't make sense of the following

Greenhorn
Posts: 13
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.

Marshal
Posts: 59108
180
• 1
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?

Greenhorn
Posts: 13

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..

Sheriff
Posts: 5694
393
• 1
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: 59108
180
• 1
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);

Sheriff
Posts: 12013
196
• 2

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?

Greenhorn
Posts: 13

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.

Greenhorn
Posts: 13

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.

Greenhorn
Posts: 13

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!!!

Greenhorn
Posts: 13
[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: 12013
196
• 1
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.

Greenhorn
Posts: 13

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.

 Let's get him boys! We'll make him read this tiny ad! Rocket Oven Kickstarter - from the trailboss https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss