• 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

While Loop Not Ending

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello:

For the code below; when I run the code as part of a method I made, it always runs through the for loop before ending.

What i am trying to do is: When the code has to use this section to choose a new step matching a branch value, it must go through the rest of the steps. As soon as it reaches a good step, i want to set s=1, and stop the while loop, ending the for loop and therefore ending this section of code. I tried putting the while loop inside the for loop, but then the program hangs in the for loop. Any help would be most appreciated!
Thank you.

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew,
How about changing the loop to:

That way you get the check twice.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using a number to check whether the loop has finished? You should have a boolean called valueFound or similar. Your code style suggests you have come from C to Java; you use numbers for true and false in C, but not in Java or C#.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew,

it is hard to help you, because from your example I can't follow what you try to accomplish.

You might want to look at the continue or break statements, which let you exit the current iteration or the whole loop itself. The following link also explains labeled and unlabeled loops:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/branch.html
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew , there are lot of issues in this code.
1. I assume ma is of type String and if this is String then you should not do ma =="ALL". ma and "ALL" will never be same as in case of Strings == will check for the object reference and not the value.
2. You are not considering the case when ma does not contain 'fire' and ma is not equal to "ALL", so that means the variable S will never be set to 1 and even if the for loop ends after some pre defined iterations you are again starting the for loop as the S is still 0 and as you are ierating over the same previous data set which does not match your condition this will keep looping.

Please modify your conditions to consider all the scenarios and ideally you should always have a else for the if condition dont just end with else if.

thanks
 
What? What, what, what? What what tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic