Google


yorick banner

Home

Manual

Packages

Global Index

Keywords

Quick Reference

Back: for Forward: Scoping   FastBack: Conditionals Up: Loops FastForward: Scoping         Top: Yorick Contents: Table of Contents Index: Concept Index About: About this document

1.2.4.3 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;
     more_statements_A;
   }
   more_statements_B;
  skip_i:
 }
done:
 more_statements_C;

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 (;;) {
  body_before_test;
  if (!condition) break;
  body_after_test;
}