Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

is this a block within a block?  RSS feed

 
Nathan banks
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am re-reading my books first chapter to beat these concepts into my head.  I am trying to figure out if the for loops target is a block, which also encompass the if statements block.  I think so but would love to be sure.  Also, does the counter need to be initialized before the for loop?  Thanks for any help,
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan banks wrote:does the counter need to be initialized before the for loop?
Just comment counter intialization line using end of line comment like this // counter = 0; then see what happens..
Edit: After commenting that line try to compile and run then tell whether It produces correct output Or gives error, If error then what error says? You must initialize counter variable before It's use. You are using It on line no 10 i.e. counter++; so before this expression counter must have some value assigned to It because counter is a local variable and local variables are not initialized to default values unlike instance variables. You can initialize counter in loop Or outside loop doesn't matter, just need to initialize before It's use. But If you initialize counter in loop then each time loop executes It'll again assign same value to counter.
 
Nathan banks
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help Ganesh.
Ganesh Patekar wrote:You can initialize counter in loop Or outside loop doesn't matter, just need to initialize before It's use. But If you initialize counter in loop then each time loop executes It'll again assign same value to counter.
  Is the block (i think its a block) containing counter++ part of the loop.  If I assigned counter a value one line before counter++ does that make it part of the loop?  Also, the program ran without errors with and without the comment // counter = 0
 
Nathan banks
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, my idiot self realized I could very easily test what you were telling me so I ran it again with
and I got the same (correct) results.  I guess it is not part of the loop.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathan banks wrote:Is the block (i think its a block) containing counter++ part of the loop.
Yes you can call It a block governed by the for statement. Yes the block containing counter++ is the part of for statement.
If I assigned counter a value one line before counter++ does that make it part of the loop? 
Yes
Also, the program ran without errors with and without the comment // counter = 0
Are you sure? I'm talking about commenting counter = 0 on line no 5.. verify again.
Edit: You can read more about for statement here --> The for Statement
Always try compiling and running program in different different ways, It is not going to crash your system   But you will understand why It produces a particular output in this code but not in another changed code Or why It gives compile time error(You can read error message and easily understand what It means) by observing It.
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh explained it well.

Your code worked for me; a few tips:
- Indent! heh
- You can just use gallons as a counter and ditch the counter logic; use gallons % 10 (modulus operator) instead
- You can use printf or String.format to format your numbers to a fixed size and spacing
- You can make gallons an int and you can declare it in the For loop
- You can declare litres in the for loop since it isn't used outside the loop

Solution below (don't read if you want to figure it out).
























ternary operators are cool
 
Nathan banks
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so i should have probably started by saying that this is a chapter 1 exercise from "Java A Beginner Guide Sixth Edition by Herbert Schildt."
Ganesh Patekar wrote:Are you sure? I'm talking about commenting counter = 0 on line no 5.. verify again.
  I did run it again and I got the expected results from the exercise.  Maybe I'm misunderstanding what you're asking me to try.  I inserted the comment after I gave counter a value of 0   I will study the link you gave me more tomorrow (its late now) and I thank you. You cleared up a lot!

Julian West wrote:
- Indent! heh
- You can just use gallons as a counter and ditch the counter logic; use gallons % 10 (modulus operator) instead
- You can use printf or String.format to format your numbers to a fixed size and spacing
- You can make gallons an int and you can declare it in the For loop
- You can declare litres in the for loop since it isn't used outside the loop


This is also terrific.  I did indent in notepad++, I guess, it looked indented anyhow.    You gave me a lot of great tips that I have no grasp of yet, but I will delve into your advice and try to better understand it.  I'm pretty new at this, so it's all very helpful.  Thank you both!
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathan banks wrote:  I did run it again and I got the expected results from the exercise.  Maybe I'm misunderstanding what you're asking me to try.
Yes you did
Nathan banks wrote: I inserted the comment after I gave counter a value of 0
  • No no you are just writing comment but not commenting assignment statement counter = 0; on line no 5.
  • When you comment any code then that code is ignored means will not get executed and will not have any meaning, so comments are used for writing useful information about particular block of code etc in our program.
  • Do like this
  • then you will get compile time error, If you are using IDE It says The local variable counter may not have been initialized and If using command prompt then says  error: variable counter might not have been initialized counter++;
  • Good to read click here --->Java Programming Style Guide

  • Yes you're welcome
     
    Nathan banks
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganesh Patekar wrote:No no you are just writing comment but not commenting assignment statement counter = 0; on line no 5.
    Oh, okay.  I didn't know you could assign a comment statement.  I'll check out the link.  Thanks!
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16028
    87
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't know what you mean by "assign a comment statement", but: comments are purely for humans to read, the Java compiler throws comments away. Putting comments in the code makes no difference at all for how the program works or what it does.
     
    Nathan banks
    Greenhorn
    Posts: 22
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okay, so I now understand what you were showing me.  For a moment I thought that you there was another type of comment that also allowed the assigning of a variable.  It's the semicolon at the end of the comments fault.  I now (hopefully) understand you were showing me what happens if I don't assign it a value before it's initialized (turn it into a comment and it doesn't get assigned a value).  Wow, thanks for bearing with me.
    Ganesh Patekar wrote:
  • Good to read click here --->Java Programming Style Guide

  •   Also, this is a great link.  I need to commit to memory more of what I read.  I have visited this page before, but that was about a week ago.  As I continue to learn more I should revisit pages like this so that I can be sure I am doing everything correctly.  For example, my books exercises do not have the keyword for followed by a space    Thanks again
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can read about 3 types of comments in Java here --->Comments in Java Code, Just read about two comments except documentation comment, skip documentation comment at this moment as beginner, you may learn It later.
    You're welcome
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!