Perl-goto-statement
提供:Dev Guides
Perl gotoステートメント
Perlは goto ステートメントをサポートします。 3つの形式があります:goto LABEL、goto EXPR、およびgoto&NAME。
Sr.No. | goto type |
---|---|
1 |
goto LABEL goto LABELフォームは、LABELというラベルの付いたステートメントにジャンプし、そこから実行を再開します。 |
2 |
goto EXPR goto EXPRフォームは、goto LABELを一般化したものです。 式はラベル名を返すことを期待し、そのラベル付きステートメントにジャンプします。 |
3 |
goto &NAME 現在実行中のサブルーチンを名前付きサブルーチンの呼び出しに置き換えます。 |
構文
*goto* ステートメントの構文は次のとおりです-
goto LABEL
or
goto EXPR
or
goto &NAME
流れ図
例
次のプログラムは、 goto ステートメントの最も頻繁に使用される形式を示しています-
#/usr/local/bin/perl
$a = 10;
LOOP:do {
if( $a == 15) {
# skip the iteration.
$a = $a + 1;
# use goto LABEL form
goto LOOP;
}
print "Value of a = $a\n";
$a = $a + 1;
} while( $a < 20 );
上記のコードが実行されると、次の結果が生成されます-
Value of a = 10
Value of a = 11
Value of a = 12
Value of a = 13
Value of a = 14
Value of a = 16
Value of a = 17
Value of a = 18
Value of a = 19
次の例は、goto EXPRフォームの使用方法を示しています。 ここでは、2つの文字列を使用し、文字列連結演算子(。)を使用してそれらを連結しています。 最後に、ラベルとgotoの形成は、ラベルにジャンプするために使用されています-
#/usr/local/bin/perl
$a = 10;
$str1 = "LO";
$str2 = "OP";
LOOP:do {
if( $a == 15) {
# skip the iteration.
$a = $a + 1;
# use goto EXPR form
goto $str1.$str2;
}
print "Value of a = $a\n";
$a = $a + 1;
} while( $a < 20 );
上記のコードが実行されると、次の結果が生成されます-
Value of a = 10
Value of a = 11
Value of a = 12
Value of a = 13
Value of a = 14
Value of a = 16
Value of a = 17
Value of a = 18
Value of a = 19