Back: for Forward: Scoping   FastBack: Conditionals Up: Loops FastForward: Scoping         Top: Yorick Contents: Table of Contents Index: Concept Index About: About this document Using break, continue, and goto

The break statement jumps out of the loop containing it. The continue statement jumps to the end of the loop body, "continuing" with the next pass through the loop (if any). In deeply nested loops -- which are extremely rare in Yorick programs --- you can jump to more general places using the goto statement. For example, here is how to break out or continue from one or both levels of a doubly nested loop:

 for (i=1 ; i<=n ; ++i) {
    for (j=1 ; j<=m ; ++j) {
      if (skip_to_next_j_now) continue;
      if (skip_to_next_i_now) goto skip_i;
      if (break_out_of_inner_loop_now) break;
      if (break_out_of_both_loops_now) goto done;

The continue jumps just after more_statements_A, the break just before more_statements_B.

Break and continue statements may be used to escape from while or do while loops as well.

If while tests the condition before the loop body, and do while checks after the loop body, you may have wondered what to do when you need to check the condition in the middle of the loop body. The answer is the "do forever" construction, plus the break statement (note the inversion of the sense of the condition):

for (;;) {
   if (!condition) break;