• 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

Is a for-loop broken out of as soon as the condition is not met?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example, I am testing something w/ the following code:



And it is only printing out:

Number: 8

I don't exactly understand why it is not starting the for loop again how that I have updated b & e?
 
Bartender
Posts: 1357
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply because initialization expression in a for loop is executed exactly once, not at every iteration.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
R Pats, welcome to the Ranch.

Claude basically answered right to the point. In order to achieve desired behaviour, you can try to use while loop, that should work.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I'm missing something but what exactly is the desired behavior? What was the desired output of this program?
 
Claude Moore
Bartender
Posts: 1357
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:What was the desired output of this program?


This makes me guess you didn't write that piece of code...
Where did you take it from ? Looks like it is an example from a tutorial or handbook aimed to make the reader understand how iteration in java (for, while and do .. while) works
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Maybe I'm missing something but what exactly is the desired behavior?

Based on the users question below, I assumed user expected loop control to be reset to 0 when i hits 8 (in fact during the first iteration). By changing assignment on line 5 to i = 0 program would satisfy quote below. Is it what OP expects? I'm not that sure.

R Pats wrote:I don't exactly understand why it is not starting the for loop again how that I have updated b & e?

By the way, my advice earlier to use while loop wasn't accurate. As wrote above, you can achieve that by amending assignment at line 5. But, as Junilu rightly said, you should define the expected output after program gets executed.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic