In the last chapter we learned about inputing data to the program. Well now you'll be learning how to repeat a function over and over again. Remember anything to the right of an apostrophe (') are comments in one version of basic, and should be removed if they cause problems.
This loop will continue forever, unless interrupted by the operating system. If you wanted to have a condition where the loop ends, you can put a WHILE or UNTIL keyword after LOOP.
This question already has an answer here:
- How do I break out of nested loops in Java? 33 answers
If I have loop in a loop and once an if
statement is satisfied I want to break main loop, how am I supposed to do that?
This is my code:
marked as duplicate by Steve Chambers, Mark Rotteveel javaOct 26 '16 at 14:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
6 Answers
Using a labeled break:
Also See
You can add labels to your loop, and use that labelled break
to break out of the appropriate loop: -
See these links for more information:
You can just return
the control from that function. Or use the ugly break labels
approach :)
If there is another code parts after your for
statement, you can refactor the loops in a function.
IMO, the use of breaks and continue should be discouraged in OOP, since they affect the readability and the maintenance. Sure, there are cases where they are handy, but in general I think that we should avoid them, since they will encourage the use of goto style programing.
Apparently variations to this questions are posted a lot. Here Peter provided some good and odd uses using labels.
It looks like for Java a labeled break appears to be the way to go (based on the consensus of the other answers).
But for many (most?) other languages, or if you want to avoid any goto
like control flow, you need to set a flag:
Just for fun:
Comment on break label
: it's a forward goto. It can break any statement and jump to the next:
Not the answer you're looking for? Browse other questions tagged javaloopssyntaxfor-loopbreak or ask your own question.
The IF...THEN...ELSEIF...ELSE control statement allows identifying if a certain condition is true, and executes a block of code if it is the case.
In some implementations of BASIC (but permitted by most versions), the IF statement may need to be contained in one line. However, ELSEIF may not be available in this case, and there is no need for an explicit END IF:
Nested Loop For C
This carries over into some implementations of BASIC where if the 'IF...THEN' statement is followed by code on the same line then it is fully contained. That is, the compiler assumes the lines ends with 'ENDIF', even if it not stated. This is important when dealing with nested 'IF...THEN' clauses:
The ELSE clause, while following the 'IF 2<3' statement, is associated with the 'IF X<2' statement, because the 'IF 2<3' statement has a PRINT statement on the same line.
GW-BASIC is a dialect of the programming language BASIC developed by Microsoft in the 1980s, and was included in most versions of MS-DOS. It is similar to Microsoft/IBM BASICA, but is a fully self-contained executable.