continue文を使い、for文のループの現在の処理を終了し、次の処理へ移るサンプル。
サンプル
ラベル指定なし
<script type="text/javascript">
for ( var $counterVar = 1; $counterVar <= 10; $counterVar++ ) {
if ( $counterVar % 2 ) {
continue;
}
document . write( $counterVar + ',' );
}
</script>
for ( var $counterVar = 1; $counterVar <= 10; $counterVar++ ) {
if ( $counterVar % 2 ) {
continue;
}
document . write( $counterVar + ',' );
}
</script>
↓↓↓出力結果↓↓↓
ラベル指定あり
<script type="text/javascript">
label1:
for ( var $counterVarA = 1; $counterVarA <= 3; $counterVarA++ ) {
label2:
for ( var $counterVarB = 1; $counterVarB <= 3; $counterVarB++ ) {
label3:
for ( var $counterVarC = 1; $counterVarC <= 3; $counterVarC++ ) {
document . write( $counterVarA + '-' );
document . write( $counterVarB + '-' );
document . write( $counterVarC + ', ' );
if ( $counterVarC >= 2 ) {
document . write( '<br />' );
continue label2;
}
}
document . write( '<br />' );
}
}
</script>
label1:
for ( var $counterVarA = 1; $counterVarA <= 3; $counterVarA++ ) {
label2:
for ( var $counterVarB = 1; $counterVarB <= 3; $counterVarB++ ) {
label3:
for ( var $counterVarC = 1; $counterVarC <= 3; $counterVarC++ ) {
document . write( $counterVarA + '-' );
document . write( $counterVarB + '-' );
document . write( $counterVarC + ', ' );
if ( $counterVarC >= 2 ) {
document . write( '<br />' );
continue label2;
}
}
document . write( '<br />' );
}
}
</script>
↓↓↓出力結果↓↓↓