Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

While Loop Not Ending

 
Andrew Ceronoski
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew,
How about changing the loop to:

That way you get the check twice.
 
Campbell Ritchie
Sheriff
Posts: 48647
56
  • Mark post as helpful
  • send pies
  • 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#.
 
Frank Kellinghusen
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
 
anilkumar patil
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic