• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple for loop,break statements  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I made a very very small program to count loop I 10 times and print the number i to the terminal BUT I use the break statement in between the loop to break(terminate) the loop when it reaches the fifth value.

BUT the question I have is how come when I place the break statement before the System.out.println(i); statement it counts from 0 to 4(first example) AND when I put the System.out.println(i) before the break statement(2nd example) it counts from 0 to 5?? how come this happens??





and the examnple where it counts from 0 to 5





Thanks for the help guys =)
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about what happens when i is 5 in your second example. Will the i be printed or not?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first loop will be run like this:

i = 0 , print 0 , increment 1
i = 1, print 1, increment 1
.
.
i = 4, print 4, increment 1
i =5 break, come out of loop.


Your second loop will be run like this:

i = 0 , print 0 , increment 1
i = 1, print 1, increment 1
.
.
i = 4, print 4, increment 1
i =5, print 5, break, come out of loop.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Secondly as a coding standards, class name should be start from Capital. So name should be 'Bra'
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the "break" statement does not mean "finish the current loop iteration and quit". It means "quite the loop RIGHT NOW".

So if the break comes before the print, then when the condition is true, we hit the break and leave the loop immediately, before the println is reached on that loop.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys =))

I should have noticed that,I broke it down slowly and now know what the break; statement is ;p

another related question to loops I find if you use a continue statement in a for loop to skip an iteration the same thing will not work for a while loop whis is this it just puts me into an infinite loop.




and then the while loop puts me into an infinite loop



why is this?? and how can I fix it IF you can fix it?

Thanks much appreciated for all the help =)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's really the same answer.

in the for-loop, when you continue, you jump to the top of the loop. Part of the start of each loop iteration is the third piece of the for-statement:


So when you jump to the top for the next iteration, the third thing runs. in your example, that is the "i++", so i gets incremented.

however, in a while loop, there isn't anything at the top. So when you hit the continue statement on line 14, you jump to line 7. Since I didn't change, i is still 5, so you enter the if-block, hit the continue, i didn't change...etc.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The break keyword means terminate the iteration at this point. Now. Completely. Go on to the next piece of code.
The continue keyword means terminate the current iteration at this point. Now. Try starting again with the next run.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!