Perl | last in loop Last Updated : 27 Feb, 2019 Comments Improve Suggest changes 1 Likes Like Report last keyword is used to loop control statement which immediately causes the current iteration of the loop to become the last. If a label given, then it comes of the loop by the label. Syntax: # Comes out of the current loop. last # Comes out of the loop specified by # MY_LABEL last MY_LABEL Example 1: Perl #!/usr/bin/perl $sum = 0; $a = 0; $b = 0; while(1) { $sum = $a + $b; $a = $a + 2; # Condition to end the loop if($sum > 10) { print "Sum = $sum\n"; print "Exiting the loop\n"; last; } else { $b = $b - 1; } } print "Loop ended at Sum > 10\n"; Output: Sum = 11 Exiting the loop Loop ended at Sum > 10 Example 2: Perl #!/usr/local/bin/perl $a = 1; $sum = 0; # Outer Loop Label1: while($a < 16) { $b = 1; # Inner Loop Label2: while ($b < 8) { $sum = $sum + $b; if($a == 8) { print "Sum is $sum"; # terminate outer loop last Label1; } $b = $b * 2; } $a = $a * 2; } Output: Sum is 22 Create Quiz Comment C Code_Mech Follow 1 Improve C Code_Mech Follow 1 Improve Article Tags : Perl perl-basics Perl-Loops Explore BasicsPerl Programming Language2 min readIntroduction to Perl7 min readPerl Installation and Environment Setup in Windows, Linux, and MacOS3 min readPerl | Basic Syntax of a Perl Program10 min readHello World Program in Perl3 min readFundamentalsPerl | Data Types3 min readPerl | Boolean Values3 min readPerl | Operators | Set - 112 min readPerl | Operators | Set - 27 min readPerl | Variables4 min readPerl | Modules3 min readPackages in Perl4 min readControl FlowPerl | Decision Making (if, if-else, Nestedâif, if-elsif ladder, unless, unless-else, unless-elsif)6 min readPerl | Loops (for, foreach, while, do...while, until, Nested loops)7 min readPerl | given-when Statement4 min readPerl | goto statement3 min readArrays & ListsPerl | Arrays6 min readPerl | Array Slices3 min readPerl | Arrays (push, pop, shift, unshift)3 min readPerl List and its Types4 min readHashPerl Hash4 min readPerl | Hash Operations8 min readPerl | Multidimensional Hashes6 min readScalarsPerl | Scalars2 min readPerl | Comparing Scalars6 min readPerl | scalar keyword2 min readStringsPerl | Quoted, Interpolated and Escaped Strings4 min readPerl | String Operators4 min readPerl | String functions (length, lc, uc, index, rindex)4 min readOOP ConceptsObject Oriented Programming (OOPs) in Perl7 min readPerl | Classes in OOP6 min readPerl | Objects in OOPs6 min readPerl | Methods in OOPs5 min readPerl | Constructors and Destructors4 min readPerl | Method Overriding in OOPs6 min readPerl | Inheritance in OOPs7 min readPerl | Polymorphism in OOPs4 min readPerl | Encapsulation in OOPs6 min readRegular ExpressionsPerl | Regular Expressions2 min readPerl | Operators in Regular Expression4 min readPerl | Regex Character Classes3 min readPerl | Quantifiers in Regular Expression4 min readFile HandlingPerl | File Handling Introduction7 min readPerl | Opening and Reading a File4 min readPerl | Writing to a File3 min readPerl | Useful File-handling functions2 min read Like