Tuesday, September 9, 2014

Break out of a sentinel-controlled loop in Java/C#

The issue is the same in Java and C# . To exit a loop with a sentinel-controlled value, you need a break statement.

Example:
while(someValue != "Q"){
      //code for loop to execute while sentinel value not entered
      if(someValue.equals("Q"){
             break;//MUST USE THIS OR YOU WILL LOOP FOREVER
}

I think MSDN does a much better and simpler job explaining break. But I did manage to find some Oracle documentation that included code examples here.

In general, I am finding any documentation on Java to be spotty and not well written in the sense of not providing a substantive explanation with code examples. (Sorry to all the Java die-hards I offend.) In this case, you can see that MSDN uses one sentence to explain the break statement while the title of the Oracle documentation will make it difficult to even find this in a search using keyword "break".

The moral of the story: Java is a competitor to C# so they are very similar. If you can't find info on your Java issue, you might find it in MSDN and be able to apply it to Java.

No comments:

Post a Comment