1 / 14

循环语句

循环语句. 一、 DO ……LOOP 循环 DO ……LOOP 结构提供了一种基于某个条件的灵活的循环方法,它有四种形式,分别为: DO UNTIL condition …… LOOP DO WHILE condition …… LOOP DO …… LOOP UNTIL condition DO …… LOOP WHILE condition DO 表示循环的开始, LOOP 表示循环的结束, condition 表示循环条件,是个逻辑表达式,其值为 True 或 False 。.

hovan
Télécharger la présentation

循环语句

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 循环语句 • 一、DO ……LOOP循环 • DO ……LOOP结构提供了一种基于某个条件的灵活的循环方法,它有四种形式,分别为: • DO UNTIL condition …… LOOP • DO WHILE condition …… LOOP • DO …… LOOP UNTIL condition • DO …… LOOP WHILE condition • DO表示循环的开始,LOOP表示循环的结束,condition表示循环条件,是个逻辑表达式,其值为True或False。

  2. 1、DO UNTIL condition ……LOOP的语法格式为: DO UNTIL condition statements LOOP 其执行过程为:先计算条件condition,其结果为False时,执行语句块statements,接着再检查条件,直到condition的结果为True时退出循环。 int sum=0,x=0 do until x>100 if x%2=0 then sum+=x end if x++ loop

  3. 2、DO WHILE condition …… LOOP的语法格式为: DO WHILE condition statements LOOP 其执行过程为:先计算条件condition,其结果为True时,执行语句块statements,接着再检查条件,直到condition的结果为False时退出循环。 int sum=0,x=0 do while x<=100 sum+=x x++ loop

  4. 3、DO …… LOOP UNTIL condition的语法格式为:DO statements LOOP UNTIL condition 其执行过程为:先执行语句块statements,然后计算条件condition,如果其结果为False,则继续执行语句块statements,直到condition的结果为True时退出循环。 int sum=0,x=0 do if x%2<>0 then sum+=x end if x++ loop until x>100

  5. 4、DO……LOOP WHILE condition的语法格式为: DO statements LOOP WHILE condition 其执行过程为:先执行语句块statements,然后计算条件condition,如果其结果为True,则继续执行语句块statements,直到condition的结果False为时退出循环。 int i=1,sum=1 do sum*=I i+=2 loop while i<=100

  6. 二、FOR …… NEXT 循环 FOR ……NEXT是个循环次数确定的循环结构,它使循环体内的语句执行规定的次数。 其语法结构为: FOR varname = start TO end [STEP increment] statementblock NEXT 其中,varname是循环变量,可以是任意的数值类型,start、end分别是循环变量的初值和终值,increment是每次循环后循环变量的步长增量(缺省值为1),statementblock是一组语句,称做循环体,方括号表示该子句可以省略。

  7. integer i,sum=0 for i=1 to 100 sum += i next

  8. 应用举例 1、求斐伯那契数列的前N项和 1、1、2、3、5、8、13、21、34、55、…… 2、屏幕保护程序

  9. Fbef=1 Fbed=1 int sum=2 For i=3 to 10 fnext=fbef+fbed sum+=f3 fbef=fbed fbed=fnext next

  10. 三、EXIT(退出循环) 在DO …… LOOP和FOR ……NEXT语句的循环体中,当我们想在中途退出循环时,使用EXIT语句是个好办法,执行该语句后,程序的控制权转至循环语句后的语句。 例如: do while count<11 if Nbr[Count] = 0 then EXIT Count = Count + 1 loop MessageBox("提示", "Count的值为 " + String(Count) )

  11. 四、CONTINUE(继续循环) 在DO …… LOOP和FOR …… NEXT语句的循环体中,遇到CONTINUE语句后,将不执行CONTINUE语句后与循环结束前的所有语句,而开始新一轮循环。 例如: for index=1 to 3000 if item[index]="" then CONTINUE …… next

  12. 五、循环嵌套 循环嵌套就是把一个循环放在另一循环的内部,下面通过一个实例说明循环嵌套的工作方式:int Matrix[2,3] //说明二维数组 int i , j for i = 1 to 2 //外层循环 for j = 1 to 3 //内层循环 Matrix[i,j]=i+j next //j,内层循环结束 next //i, 外层循环结束 应用举例:产生十个随机数,然后排序

  13. 排序算法(选择法) 15 23 11 45 20 78 99 36 24 17 99 23 11 45 20 78 15 36 24 17 99 78 11 45 20 23 15 36 24 17 99 78 45 11 20 23 15 36 24 17 99 78 45 36 20 23 15 11 24 17 99 78 45 36 24 23 15 11 20 17 99 78 45 36 24 23 15 11 20 17 99 78 45 36 24 23 20 11 15 17 99 78 45 36 24 23 20 17 15 11

  14. j 15 23 11 45 20 78 99 36 24 17 max=i

More Related