• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with for loop and break

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ! Can anyone please explain me that in the following, the break is used to come out of the for loop or if loop ? (this is from the book Head First Java (pg no. 104)

My exact doubt is how will the numofHits value increase to locationCells.length when at the first instance only the system comes out of the loop ?

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code won't compile, but let's assume you have numofHits == locationCells.length on line 8.

I don't quite understand your question.

First the loop is executed. It may or may not exit through break statement.
Then there is an if statement comparing numofHits with locationCells.length.

I am assuming numofHits was 0 before this code gets executed.

It looks like the condition from line 8 will can be true only if locationCells.length is equal to 0 or 1.
 
Manish Daryani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.. numofHits is 0 before the code gets executed. This is just the portion of the code wherein i'm unable to understand the break command.
Even I am of the view that condition from line 8 will be true only when locationCells.length equals 0 or 1.

But according to the book, locationCells.length = 3

Do you want me to post the entire code?

Paweł Baczyński wrote:Your code won't compile, but let's assume you have numofHits == locationCells.length on line 8.

I don't quite understand your question.

First the loop is executed. It may or may not exit through break statement.
Then there is an if statement comparing numofHits with locationCells.length.

I am assuming numofHits was 0 before this code gets executed.

It looks like the condition from line 8 will can be true only if locationCells.length is equal to 0 or 1.

 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manish Daryani wrote:Hi ! Can anyone please explain me that in the following, the break is used to come out of the for loop or if loop ?


  • Loop means for, do-while, while and enhanced for each which executes code contained in the body governed by these loops until certain condition is true. If is not a loop.
  • A break statement transfers control to the innermost enclosing switch, while, do, or for statement.
  • In your example when guess == cell becomes true then control goes in the body of If statement then assigns result = "hit" and increments numofHits by 1 then break; statement gets executed which transfers control Or in your wording comes out of for loop.

  • Manish Daryani wrote:Do you want me to post the entire code?

    I hope you understand by just a portion of code no one will be able to answer you properly. If you post complete code then someone who knows the answer can help you. Please use code tags to post code with proper indentation. You can refer these links for that. Click here ---> Use Code Tag and Java Programming Style
     
    Manish Daryani
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Ganesh ! Here's the entire code. I will try an explain a little more in detail.
    The numofHits value should become 3 to be equal to locationCells.length. However, as per my understanding of the code, at the first instance only (when guess == cell), numofHits increase from 0 to 1 and we come out of the for loop. So how will the value of numofHits increase to 3 "?

    Ganesh Patekar wrote:. . .

    [edit]CR removed quote because there is no point in writing the whole of the same post twice.
     
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Did you forget to post the code?
     
    Manish Daryani
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes ! I'm sorry ! But again, this is the just a portion of the code (this is all that is there in the book)




    Knute Snortum wrote:Did you forget to post the code?

     
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Does checkYourself get called more than once?
     
    Manish Daryani
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I am sorry, I don't know that much java !

    This is all that is there in the book.

     
    Knute Snortum
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There is actually more to that code:



     
    Knute Snortum
    Sheriff
    Posts: 7125
    184
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    From the code above, I would say that result will be "kill" only if guess is the index of the last element in the locationCells array.
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
  • I hope now you know locationCells is an array of int having elements 2, 3 and 4.
  • numOfHits = 0 and result = "miss"
  • If you see following code in main method of SimpleDotComTester class
  • which leads to assigns int guess = 2 in below code in checkYourself(String stringGuess) method of SimpleDotCom class. so guess = 2.
  • When loop is executed first time the value at index 0 of array locationCells is 2 which is assigned to int variable cell in for loop. Now we have cell =0.
  • if (guess == cell) means 2 == 2 returns true then control goes in If statement then assigns result = "hit" and increments numofHits by 1 so numofHits becomes 1, then break; statement gets executed which transfers control out of for loop and goes to below code.

  • where checks numOfHits == locationCells.length means 1 == 3 (because locationCells has 3 elements) so returns false means   result = "kill"; will not get executed.
  • Then System.out.println(result); prints value of result i.e. "hit" and returns String result to code on line no 10 in main method of SimpleDotComTester class.

  • Hope you understood
    reply
      Bookmark Topic Watch Topic
    • New Topic