`
tomhibolu
  • 浏览: 1393437 次
文章分类
社区版块
存档分类
最新评论

回圈 (loop)--while do done, until do done (不定回圈)

 
阅读更多

除了 if...then...fi 这种条件判断式之外,回圈可能是程序当中最重要的一环了~回圈可以不断的运行某个程序段落,直到使用者配置的条件达成为止。 所以,重点是那个『条件的达成』是什么。除了这种依据判断式达成与否的不定回圈之外, 还有另外一种已经固定要跑多少次的回圈形态,可称为固定回圈的形态呢!底下我们就来谈一谈:

while do done, until do done (不定回圈)

一般来说,不定回圈最常见的就是底下这两种状态了:

1.当 condition 条件成立时,就进行回圈,直到 condition 的条件不成立才停止:

while [condition] -->中括号内的状态就是判断式

do -->回圈的开始

程序段落

done -->回圈的结束

2.当 condition 条件成立时,就终止回圈,否则就持续进行回圈的程序段:

until [condition]

do

程序段

done

实例一:

#!/bin/bash
#Repeat question until input correct answer.

while [ "$yn" != "yes" -a "$yn" != "YES" ]
do
read -p "Please input yes/YES to stop this program:" yn
done
echo "Oh,you input the correct answer!"

实例二:

#!/bin/bash
#Repeat question until user input the correct answer!
until [ "$yn" == "yes" -o "$yn" == "YES" ]
do
read -p "Please input yes/YES to stop this program:" yn
done
echo "oh,you input the correct answer!"

比较一下有什么不同\(^o^)/~

如果我想要计算 1+2+3+....+100 这个数据呢? 利用回圈啊

#!/bin/bash
#The program will use loop to calculate "1+2+3..+100"

i=0
sum=0
while [ "$i" != "100" ]
do
i=$(($i+1))
sum=$(($sum+$i))
done
echo "The result of "1+2+3..+100" is ==> $sum"

或者用until

#!/bin/bash
#The program will use loop to calculate "1+2+3+...+100"

i=0
sum=0
until [ "$i" == "100" ]
do
i=$(($i+1))
sum=$(($sum+$i))
done
echo "The result of '1+2+3+...+100' is ==> $sum."

执行结果都是:

The result of 1+2+3..+100 is ==> 5050

O(∩_∩)O~,很好玩吧!

分享到:
评论

相关推荐

    2009 达内Unix学习笔记

    clear 清屏,清除(之前的内容并未删除,只是没看到,拉回上面可以看回)。 五、目录管理命令 pwd 显示当前所在目录,打印当前目录的绝对路径。 cd 进入某目录,DOS内部命令 显示或改变当前目录。 cd回车/cd ~ 都...

    au3反编译源码

    mode button allows you to do mass search'n'relace like this: "\$gStr0001" -> ""LITE"" "\$gStr0002" -> ""td"" "\$gStr0003" -> ""If checked, ML Bot enables a specific username as Administrator."" ? ...

    微软内部资料-SQL性能优化3

     SOX001109700040 – INF: Queries with PREFETCH in the plan hold lock until the end of transaction Locking Concepts Delivery Tip Prior to delivering this material, test the class to see if they ...

    微软内部资料-SQL性能优化5

    The IAM allows SQL Server to do efficient prefetching of the table’s extents, but every row still must be examined. General Index Structure All SQL Server Indexes Are Organized As B-Trees ...

    uboott移植实验手册及技术文档

    198 cmp r0, r2 /* until source end addreee [r2] */ 199 ble copy_loop 200 #endif /* CONFIG_SKIP_RELOCATE_UBOOT */ 201 #endif #ifdef CONFIG_S3C2410_NAND_BOOT @ reset NAND mov r1, #NAND_CTL_BASE ...

    LCTF软件备份VariSpec™ Liquid Crystal Tunable Filters

    Ideally, one should check VsIsReady() using a timer or the like to wait efficiently, so that the host PC is free to do other tasks while waiting for the VariSpec. The VariSpec always processes each ...

    Agile Project Management with Scrum

    Some might try valiantly to make the control system work by applying more rigor, and indeed that works for a while. But the people who prevail are those who figure out how to change to a system of ...

    Agile.Project.Management.with.Scrum

    Some might try valiantly to make the control system work by applying more rigor, and indeed that works for a while. But the people who prevail are those who figure out how to change to a system of ...

    GifDecoder

    while (dx ) { // map color and insert in destination int index = ((int) pixels[sx++]) & 0xff; int c = act[index]; if (c != 0) { dest[dx] = c; } dx++; } } } } /** * Gets the image ...

    操作系统(内存管理)

    文将对 Linux™ 程序员可以使用的内存管理技术进行概述,虽然关注的重点是... /* Leave the loop */ break; } } /* If we made it here, it's because the Current memory * block not suitable; move...

    内存管理内存管理内存管理

    内存管理内幕 dragonimp's blog coder.developer.[designer].ArchitecturE.manager.^_^... posts - 29, comments - 121, trackbacks - 27 My Links Home Contact ...文中将为您提供如何管理内存的细节,然后...

Global site tag (gtag.js) - Google Analytics