next up previous contents index
Next: 5.5.6 Tricks of Actions Up: 5.5 Mapping from Programming Previous: 5.5.4 Loops   Contents   Index

5.5.5 Break and Continue

The break statement and the continue statement in a loop can be transformed into extra transitions.

Suppose statement comp_stm is such a compound statement:

$init$;
loop_label:
if ($cond$) {
$stm1$;
if (finished)
break;
$stm2$;
$step$;
goto loop_label;
}


The break statement stops the for-loop by changing the execution point out of the compound statement. It is equivalent to:

$init$;
loop_label1:
$v$ = $cond$; // evaluate $cond$ and store the result in $v$
if ($v$) {
$stm1$;
if (finished)
goto loop_label2;
$stm2$;
$step$;
}
if ($v$)
goto loop_label1;
loop_label2:

Figure 5.6: An example of the transformation from a break statement into DCharts transitions
Image break2comp

The transformation of the model is illustrated in Figure 5.6. The break statement in the for-loop is eliminated in this example.

The continue statement in a loop can be eliminated in a similar way. If the break statement in the above comp_stm is replaced by the continue statement, it is equivalent to:

$init$;
loop_label:
$v$ = $cond$; // evaluate $cond$ and store the result in $v$
if ($v$) {
$stm1$;
if (finished)
goto loop_label;
$stm2$;
$step$;
}
if ($v$)
goto loop_label;

Figure 5.7: An example of the transformation from a continue statement into DCharts transitions
Image continue2comp

The transformation of this code with the continue statement is shown in Figure 5.7.


next up previous contents index
Next: 5.5.6 Tricks of Actions Up: 5.5 Mapping from Programming Previous: 5.5.4 Loops   Contents   Index
Thomas Huining Feng 2004-04-28