continue文を使い、while文のループの現在の処理を終了し、次の処理へ移るサンプル。
サンプル
ラベル指定なし
<script type="text/javascript">
var varA = 5;
while ( varA <= 10 ) {
if ( varA == 7 ) {
document . write( varA++ + ' ☆ラッキーセブン☆<br>' );
continue;
}
document . write( varA++ + ' ・・・<br>' );
}
</script>
var varA = 5;
while ( varA <= 10 ) {
if ( varA == 7 ) {
document . write( varA++ + ' ☆ラッキーセブン☆<br>' );
continue;
}
document . write( varA++ + ' ・・・<br>' );
}
</script>
↓↓↓出力結果↓↓↓
ラベル指定あり
<script type="text/javascript">
var varA = 1;
while ( varA <= 5 ) {
document . write( '<p>varA: ' );
document . write( varA++ );
document . write( '</p>' );
var varB = 1;
labelA:
while ( varB < varA ) {
document . write( '<p> varB: ' );
document . write( varB++ );
document . write( '</p>' );
document . write( '<p> varC: ' );
var varC = 1;
while ( varC < varB ) {
document . write( varC++ + ',' );
if ( varC > 2 ) {
document . write( '</p>' );
continue labelA;
}
}
document . write( '</p>' );
}
}
</script>
var varA = 1;
while ( varA <= 5 ) {
document . write( '<p>varA: ' );
document . write( varA++ );
document . write( '</p>' );
var varB = 1;
labelA:
while ( varB < varA ) {
document . write( '<p> varB: ' );
document . write( varB++ );
document . write( '</p>' );
document . write( '<p> varC: ' );
var varC = 1;
while ( varC < varB ) {
document . write( varC++ + ',' );
if ( varC > 2 ) {
document . write( '</p>' );
continue labelA;
}
}
document . write( '</p>' );
}
}
</script>
↓↓↓出力結果↓↓↓