break文で、for文のループを終了させるサンプル。
サンプル
ラベル指定なし
<script type="text/javascript">
for ( var counterVar = 1; counterVar <= 10; counterVar++ ) {
document . write( counterVar + ',' );
if ( counterVar >= 5 ) {
break;
}
}
</script>
for ( var counterVar = 1; counterVar <= 10; counterVar++ ) {
document . write( counterVar + ',' );
if ( counterVar >= 5 ) {
break;
}
}
</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 />' );
break 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 />' );
break label2;
}
}
document . write( '<br />' );
}
}
</script>
↓↓↓出力結果↓↓↓