YouTip LogoYouTip

Perl Goto Statement

# Perl goto Statement [![Image 4: Perl Loops](#) Perl Loops](#) **goto** is a control flow statement in the Perl programming language that allows you to jump the execution of the program to a specified label. Perl has three forms of goto: **goto LABEL, goto EXPR, and goto &NAME**: | No. | goto Type | | --- | --- | | 1 | **goto LABEL** finds the statement labeled LABEL and resumes execution from there. | | 2 | **goto EXPR** The goto EXPR form is just the general form of goto LABEL. It expects an expression to evaluate to a label name and jumps to that label for execution. | | 3 | **goto &NAME** Replaces the currently running subroutine with a call to the named subroutine. | ### Syntax The syntax format is as follows: goto LABEL or goto EXPR or goto &NAME ### Flowchart !(#) ### Examples The following two examples exit the output when the variable $a is 15. Here is a common goto example: ## Example #/usr/bin/perl$a = 10; LOOP:do{if($a == 15){# Skip iteration$a = $a + 1; # Using goto LABEL form print"Exit output n"; goto LOOP; print"This line will not be executed n"; }print"a = $an"; $a = $a + 1; }while($a<20); Executing the above program, the output result is: a = 10 a = 11 a = 12 a = 13 a = 14Exit output a = 16 a = 17 a = 18 a = 19 The following example uses the goto EXPR form. We use two strings and concatenate them with a dot (.). ## Example $a = 10; $str1 = "LO"; $str2 = "OP"; LOOP:do{if($a == 15){# Skip iteration$a = $a + 1; # Using goto EXPR form goto$str1.$str2; # Similar to goto LOOP}print"a = $an"; $a = $a + 1; }while($a **Note:** Using goto often makes code difficult to understand and maintain. Therefore, in most cases, it should be avoided. Try to use structural control flow statements (such as if, else, while, for, etc.) to write code. [![Image 6: Perl Loops](#) Perl Loops](#)
← Perl Date TimePerl Continue Statement β†’