Break: Difference between revisions

From EDukeWiki
Jump to navigation Jump to search
Document CON's weird 'break' behavior inside 'while' loops.
No edit summary
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
A single '''break''' command can be
A single '''break''' command can be used to stop code after the '''break''' from executing. Its primary use is inside [[switch]] blocks to close '''[[case]]''' statements, but it can also be used to exit a [[state]] early.  
used to stop code after the '''break''' from executing. Can also be used to exit a [[state]] early. Also used inside [[switch]] blocks to close '''[[case]]''' statements.


Inside '''while''' loops, a '''break''' transfers control to the beginning of the loop where the condition for continuing the iteration is checked. This is in contrast to most imperative programming languages, where a '''break''' inside a loop transfers control to just after the end of the loop body (thus "breaking" it). For example, in CON, the snippet
Inside '''while''' loops, a '''break''' transfers control to the beginning of the loop where the condition for continuing the iteration is checked. This is in contrast to most imperative programming languages, where a '''break''' inside a loop transfers control to just after the end of the loop body (thus "breaking" it).
 
In versions of eduke32 starting from 24th August 2023, the "break" command will report a warning if used inside a loop. Use [[continue]] or [[exit]] instead for clarity.
 
See also [[return]] and [[terminate]].
 
== Examples ==


     setvar j 0
     setvar j 0
Line 10: Line 15:
         userquote 500
         userquote 500
         break  // while is inner: "continue"
         break  // while is inner: "continue"
        userquote 400
     }
     }


makes quote 500 be displayed ''three'' times in total.
makes quote 500 be displayed ''three'' times in total and quote 400 not a single time.


[[Category:Duke3D 1.3/1.5 commands]]
[[Category:Duke3D 1.3/1.5 commands]]
[[Category:Event manipulation]]

Latest revision as of 04:07, 7 January 2024

A single break command can be used to stop code after the break from executing. Its primary use is inside switch blocks to close case statements, but it can also be used to exit a state early.

Inside while loops, a break transfers control to the beginning of the loop where the condition for continuing the iteration is checked. This is in contrast to most imperative programming languages, where a break inside a loop transfers control to just after the end of the loop body (thus "breaking" it).

In versions of eduke32 starting from 24th August 2023, the "break" command will report a warning if used inside a loop. Use continue or exit instead for clarity.

See also return and terminate.

Examples

   setvar j 0
   whilevarn j 3
   {
       addvar j 1
       userquote 500
       break  // while is inner: "continue"
       userquote 400
   }

makes quote 500 be displayed three times in total and quote 400 not a single time.