📄 perl 语言-perl 中文教程(第七章).htm
字号:
<TR>
<TD><A onmouseover="MM_swapImage('Image15','','11a.gif',1)"
onmouseout=MM_swapImgRestore()
href="http://www.sun126.com/perl5/perl5-11.htm"><IMG
src="Perl 语言-Perl 中文教程(第七章).files/11.gif" border=0
name=Image15></A></TD></TR>
<TR>
<TD><A onmouseover="MM_swapImage('Image17','','12a.gif',1)"
onmouseout=MM_swapImgRestore()
href="http://www.sun126.com/perl5/perl5-12.htm"><IMG
src="Perl 语言-Perl 中文教程(第七章).files/12.gif" border=0
name=Image17></A></TD></TR>
<TR>
<TD><A onmouseover="MM_swapImage('Image16','','13a.gif',1)"
onmouseout=MM_swapImgRestore()
href="http://www.sun126.com/perl5/perl5-13.htm"><IMG
src="Perl 语言-Perl 中文教程(第七章).files/13.gif" border=0
name=Image16></A></TD></TR>
<TR>
<TD><A onmouseover="MM_swapImage('Image18','','14a.gif',1)"
onmouseout=MM_swapImgRestore()
href="http://www.sun126.com/perl5/perl5-14.htm"><IMG
src="Perl 语言-Perl 中文教程(第七章).files/14.gif" border=0
name=Image18></A></TD></TR>
<TR>
<TD><A onmouseover="MM_swapImage('Image19','','y1a.gif',1)"
onmouseout=MM_swapImgRestore()
href="http://www.sun126.com/perl5/perl5-15.htm"><IMG
src="Perl 语言-Perl 中文教程(第七章).files/y1.gif" border=0
name=Image19></A></TD></TR>
<TR>
<TD><A onmouseover="MM_swapImage('Image20','','y2a.gif',1)"
onmouseout=MM_swapImgRestore()
href="http://www.sun126.com/perl5/perl5-16.htm"><IMG
src="Perl 语言-Perl 中文教程(第七章).files/y2.gif" border=0
name=Image20></A></TD></TR>
<TR>
<TD><A onmouseover="MM_swapImage('Image21','','y3a.gif',1)"
onmouseout=MM_swapImgRestore()
href="http://www.sun126.com/perl5/perl5-17.htm"><IMG
src="Perl 语言-Perl 中文教程(第七章).files/y3.gif" border=0
name=Image21></A></TD></TR></TBODY></TABLE></TD>
<TD class=myFont vAlign=top width=30><IMG height=1
src="Perl 语言-Perl 中文教程(第七章).files/x.gif" width=10> </TD>
<TD class=myFont vAlign=top>
<DIV align=center><IMG height=30
src="Perl 语言-Perl 中文教程(第七章).files/top.gif" width=180><BR><SPAN
class=myFont><SPAN class=myFont><FONT face=宋体>翻译:</FONT></SPAN></SPAN>
<SPAN class=myFont><SPAN class=myFont>flamephoenix</SPAN></SPAN> <BR>
<HR width="100%" SIZE=1>
<DIV align=left></DIV></DIV>
<P align=center><B>第七章 控制结构</B></P>
<P><A
href="http://www.sun126.com/perl5/perl5-7.htm#一、条件判断">一、条件判断</A><BR><A
href="http://www.sun126.com/perl5/perl5-7.htm#二、循环:">二、循环:</A><BR> <A
href="http://www.sun126.com/perl5/perl5-7.htm#1、while循环">1、while循环</A>
<BR> <A
href="http://www.sun126.com/perl5/perl5-7.htm#2、until循环">2、until循环</A>
<BR> <A
href="http://www.sun126.com/perl5/perl5-7.htm#3、类C的for循环">3、for循环</A>
<BR> <A
href="http://www.sun126.com/perl5/perl5-7.htm#4、针对列表(数组)每个元素的循环">4、针对列表(数组)每个元素的foreach循环</A><BR>
<A
href="http://www.sun126.com/perl5/perl5-7.htm#5、do循环">5、do循环</A><BR> <A
href="http://www.sun126.com/perl5/perl5-7.htm#6、循环控制">6、循环控制</A>
<BR> <A
href="http://www.sun126.com/perl5/perl5-7.htm#7、传统的goto label;语句。">7、传统的goto语句</A><BR><A
href="http://www.sun126.com/perl5/perl5-7.htm#三、单行条件">三、单行条件</A><BR><BR><A
name=一、条件判断>一、条件判断</A> <BR> if ( <expression>)
{<BR> <statement_block_1><BR> }<BR> elsif
( <expression> )
{<BR> <statement_block_2><BR> }<BR> ...<BR> else{<BR> <statement_block_3><BR> }<BR><BR><A
name=二、循环:>二、循环:</A> <BR><A name=1、while循环>1、while循环</A>
<BR> while ( <expression> )
{<BR> <statement_block><BR> }<BR><A
name=2、until循环>2、until循环</A> <BR> until ( <expression> )
{<BR> <statement_block><BR> }<BR><A
name=3、类C的for循环>3、类C的for循环</A> ,如<BR> for ($count=1; $count
<= 5; $count++) {<BR> # statements inside the
loop go here<BR> }<BR>下面是在for循环中使用逗号操作符的例子:<BR> for
($line = <STDIN>, $count = 1; $count <= 3; $line =
<STDIN>, $count++) {<BR> print
($line);<BR> }<BR>它等价于下列语句:<BR> $line =
<STDIN>;<BR> $count = 1;<BR> while ($count
<= 3) { <BR> print
($line);<BR> $line =
<STDIN>;<BR> $count++;<BR> }<BR><A
name=4、针对列表(数组)每个元素的循环>4、针对列表(数组)每个元素的循环:foreach,语法为:</A><BR> foreach
localvar (listexpr)
{<BR> statement_block;<BR> }<BR>例:<BR> foreach
$word (@words) {<BR> if ($word eq "the")
{<BR> print ("found the word 'the'\n");
<BR> }<BR> }<BR>注:<BR>(1)此处的循环变量localvar是个局部变量,如果在此之前它已有值,则循环后仍恢复该值。<BR>(2)在循环中改变局部变量,相应的数组变量也会改变,如:<BR> @list
= (1, 2, 3, 4, 5);<BR> foreach $temp (@list)
{<BR> if ($temp == 2)
{<BR> $temp =
20;<BR> }<BR> }<BR>此时@list已变成了(1, 20, 3,
4, 5)。<BR><A name=5、do循环>5、do循环</A> <BR> do
{<BR> statement_block<BR> }
while_or_until (condexpr);<BR> do循环至少执行一次循环。<BR><A
name=6、循环控制>6、循环控制</A>
<BR> 退出循环为last,与C中的break作用相同;执行下一个循环为next,与C中的continue作用相同;PERL特有的一个命令是redo,其含义是重复此次循环,即循环变量不变,回到循环起始点,但要注意,redo命令在do循环中不起作用。<BR><A
name="7、传统的goto label;语句。">7、传统的goto label;语句。</A> <BR><BR><A
name=三、单行条件>三、单行条件</A> <BR> 语法为statement keyword
condexpr。其中keyword可为if、unless、while或until,如:<BR> print
("This is zero.\n") if ($var == 0);<BR> print
("This is zero.\n") unless ($var != 0);<BR> print
("Not zero yet.\n") while ($var-- >
0);<BR> print ("Not zero yet.\n") until ($var-- ==
0);<BR> 虽然条件判断写在后面,但却是先执行的。</P></TD></TR></TBODY></TABLE>
<DIV align=center></DIV>
<DIV align=center><BR></DIV>
<DIV align=center><SPAN class=myFont><A
href="http://www.sun126.com/perl5/perl5-6.htm">上页</A> <A
href="http://www.sun126.com/perl5/perl5-8.htm">下页</A> <A
href="http://www.sun126.com/perl5/perl5index.htm">回目录</A> <A
href="http://www.sun126.com/perl5/perl5-7.htm#a"><FONT face="Arial, 宋体">Go
Top</FONT></A></SPAN><BR><BR></DIV>
<TABLE height=50 cellSpacing=0 cellPadding=0 width="100%" bgColor=#000000
border=0>
<TBODY>
<TR>
<TD bgColor=#cccc99 height=4>
<DIV align=center><IMG height=4 src="" width=4></DIV></TD></TR>
<TR>
<TD height=50>
<DIV align=center><FONT class=myfont size=2><SPAN class=myfont><FONT
color=#99cc99><A href="http://www.sun126.com/bbs/ccb/index.cgi"><FONT
color=#99cc99>中国CCB论坛</FONT></A> 整理 麻辣
2003.7.10</FONT></SPAN></FONT><FONT class=myfont color=#99cc99
size=2><SPAN class=myfont><FONT
color=#99cc66><BR></FONT></SPAN></FONT><SPAN class=myfont><FONT
class=myfont><SPAN class=myfont><FONT face="Arial, Helvetica, sans-serif"
color=#99cc99>© 2000
http://www.sun126.com</FONT></SPAN></FONT></SPAN></DIV></TD></TR></TBODY></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -