More Control Structures
----------------------------
1. The until Control Structure
The util loop runs until the conditional expression returns true. But it's really just a while loop in disguise, except that this one repeats as long as the conditional is false, rather than true. The conditional expression is evaluated before the first iteration, so this is still a zero-or-more-times loop.
2. The Naked Block Control Structure
As a general guideline, all variables should be declared in the smallest scope available. If you need a variable for just a few lines of code, you can put those lines into a naked block and declare the variable inside that block. Of course, if we would need the value of either $n or $root later, we would need to declare them in a larger scope.
3. Autoincrement and Autodecrement
A common use of these operators is in connection with a hash, to identify when an item has been seen before:
my @people = qw{ fred barney bamm-bamm wilma dino barney betty pebbles };
my %seen;
foreach (@people) {
print "I've seen you somewhere before, $_!\n"
if $seen{$_}++;
}
When barney shows up for the first time, the value of $seen{$_}++ is false, since it's the value of $seen{$_}, which is $seen{"barney"}, which is undef. But that expression has the side effect of incrementing $seen{"barney"}. When barney shows up again, $seen{"barney"} is now a true value, so the message is printed.
4. The for Control Structure
It turns out that, inside the Perl grammar, the keyword foreach is exactly equivalent to the keyword for. That is, any time Perl sees one of them, it's the same as if you had typed the other. Perl can tell which you meant by looking inside the parentheses. If you've got the two semicolons, it's a computed for loop (like we've just been talking about). If you don't have the semicolons, it's really a foreach loop.
5. Loop Controls
1) The last operator immediately ends execution of the loop. (If you've used the "break" operator in C or a similar language, it's like that.) It's the "emergency exit" for loop blocks. When you hit last, the loop is done.
2) The next operator: just like continue in Java.
3) The third member of the loop control triad is redo. It says to go back to the top of the current loop block, without testing any conditional expression or advancing to the next iteration. The big difference between next and redo is that next will advance to the next iteration, but redo will redo the current iteration.
6. Control Structures Using Partial-Evaluation Operators
1) Fortunately, you'll notice this only when the controlled expression has side effects, like altering a variable's value or causing some output. For example, suppose you ran across this line of code:
($a < $b) && ($a = $b);
Right away, you should notice that the result of the logical AND isn't being assigned anywhere.(But don't forget to consider that it might be a return value, as the last expression in a subroutine.) Why not?
If $a is really less than $b, the left side is true, so the right side will be evaluated, thereby doing the assignment. But if $a is not less than $b, the left side will be false, and thus the right side would be skipped. So that line of code would do essentially the same thing as this one, which is easier to understand:
if ($a < $b) { $a = $b; }
2) There is another way to write the logical AND and logical OR operators. You may wish to write them out as words: and and or. These word-operators have the same behaviors as the ones written with punctuation, but the words are much lower on the precedence chart. Since the words don't "stick" so tightly to the nearby parts of the expression, they may need fewer parentheses.
分享到:
相关推荐
《深入理解RPKI与HSM集成:开源项目hapi-structures-v23-2.2.zip解析》 在IT行业中,网络安全与数据保护是至关重要的领域。资源公钥基础设施(RPKI)是一种用于验证互联网路由安全性的重要技术,而硬件安全模块...
《振动控制的活动结构》(Vibration Control of Active Structures-2011)是一部深入探讨如何通过先进的技术手段来控制和减小结构物振动的专业著作。本书不仅为读者提供了理论层面的深入分析,还介绍了实际应用中的...
在"Algorithm-data-structures-questions.zip"的"data-structures-questions-master"文件中,可能包含了关于算法与数据结构的各种练习和问题,旨在帮助学习者深入理解和掌握这些概念。通过解决这些问题,你可以提升...
《Play-with-Data-Structures-master》是一个涵盖了数据结构基础到高级应用的全面教程,包含1-12章的源代码实现。这个课程是学习和深入理解数据结构的理想资源,对于计算机科学的学生、程序员以及对算法和数据结构有...
根据提供的文件信息,我们可以推断出本书《Active Control of Structures》由André Preumont与Kazuto Seto共同编写,并于2008年由John Wiley & Sons出版社出版。该书聚焦于结构主动控制领域的理论与实践,对于从事...
这是数据结构与算法分析(C语言描述)的课后答案。其中包括一些经典习题的答案,比如用Huffman 算法写出一个程序来实现文件压缩和解压,确定希尔排序对于下述输入的运行时间a.拍过序的输入数据 b....
Algorithm-Problem-Solving-with-Algorithms-and-Data-Structures-using-Python.zip,使用python的算法和数据结构解决问题的代码,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
Sound knowledge of data structures and algorithms will allow you to solve new problems, develop more performant applications, and discover new design paradigms. The learning of data structures and ...
Algorithm-Play-with-Data-Structures.zip,我的MOOC课程代码中的游戏数据结构>。更新的内容和做法也包括在内。并用Java语言编写了Java语言。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
**More Control Structures** - **章节概述**: 介绍Perl中的更多控制结构。 - **学习目标**: 掌握Perl中的循环与条件判断语句;理解控制流操作符。 - **关键知识点**: - 循环结构:for、while、foreach等循环...
This book is a state-of-the-art report on the ductility of steel structures, containing a comprehensive review of the technical literature available, and presenting the results of the authors' own ...
Antennas-and-RF-Structures-master
Algorithm-javascript-datastructures-algorithms.zip,用于教育目的的javascript和typescript数据结构和算法的集合。javascript算法和数据结构手册的源代码包,算法是为计算机程序高效、彻底地完成任务而创建的一组...
ISA–88.00.02–2001 Data Structures and Guidelines for Languages.pdf ISA–88.00.03–2003 General and Site Recipe Models and Representation.pdf ISA-88.00.04-2006 Batch Production Records.pdf ISA-88.00....
Algorithm-Data-Structures-and-Algorithms-in-Java-2nd-Edition-by-Robert-Lafore.zip,Robert Lafore第二版的数据结构与算法,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
第十章《Control Structures in Objective-C》详细讨论了Objective-C中的各种控制结构,如条件语句(`if`、`switch`)、循环语句(`for`、`while`)等,以及如何使用这些结构来控制程序的流程。 #### 十二、类别、扩展...
Python-for-Algorithms--Data-Structures--and-Interviews, 关于算法和数据结构的Udemy课程文件 用于算法。数据结构和访谈的 python ! 欢迎访问Udemy课程的知识库: 用于算法,数据结构和访谈的python !这是为你...
Tekla-Structures-18.0(32位) 测试稳定
2,ANSI ISA–88.00.02–2001 Data Structures and Guidelines for Languages - 完整英文电子版(123页).pdf 3,ANSI ISA–88.00.03–2003 General and Site Recipe Models and Representation - 完整英文电子版...