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

回圈 (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~,很好玩吧!

分享到:
评论

相关推荐

    打印菱形星形图案的Java程序.docx

    // Using do-while loop // Importing input output classes import java.io.*; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Declaring and ...

    mysql存储过程之循环语句(WHILE,REPEAT和LOOP)用法分析

    WHILE x <= 5 DO SET str = CONCAT(str,x,','); SET x = x + 1; END WHILE; SELECT str; END$$ DELIMITER ; ``` 在这个例子中,`WHILE x 是条件,当x的值大于5时,循环停止。 2. REPEAT循环: REPEAT...

    Shell-脚本常用命令-文档.zip

    - **循环结构**:`for`、`while`和`until`用于重复执行某段代码,如: ``` for i in {1..5}; do echo "This is loop number $i" done ``` 5. **输入/输出重定向** - `用于输入重定向,将文件内容作为命令的...

    2009 达内Unix学习笔记

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

    shell脚本编程之循环语句

    echo "This is an infinite loop." done ``` - **读取文件内容**:通过`文件`重定向,逐行读取文件内容。 ```bash while read user; do echo "User: $user" done ``` 这些循环语句是Shell脚本编程中非常...

    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."" ? ...

    浅谈Shell 流程控制

    本文将深入探讨Shell中的流程控制,包括if、if-else、if-elif-else、for、while和until循环,以及它们在实际应用中的示例。 1. **if 语句**: Shell中的if语句用于根据条件执行相应的命令。与PHP不同,Shell的if...

    bash shell programming

    #### 七、循环:for、while 和 until 循环是 Bash Shell 编程中的另一种控制结构,用于重复执行一系列命令直到满足特定条件。 ##### 7.1 for 循环示例 - **语法**: ```bash for file in *.txt; do echo ...

    Shell基本语法

    循环结构有`for`、`while`和`until`。`for`循环常用于遍历列表,例如: ```bash for i in {1..5}; do echo "This is loop number $i" done ``` `while`循环会在条件为真时持续执行,`until`则相反: ```bash count...

    linux shell编程

    2. **Until Loop**:与While Loop相反,直到条件为真时才停止执行。 3. **For Loop**:遍历一系列项目或执行固定次数的操作。 - `for 变量 in 列表; do 命令; done`:遍历列表。 - `for ((初始化; 终止条件; 更新)...

    mysql存储过程 游标 循环使用介绍

    MySQL提供了多种类型的循环结构,包括WHILE、LOOP、REPEAT和GOTO。其中,WHILE和REPEAT是标准的循环,而GOTO是非标准的,通常不推荐使用。 - **WHILE...END WHILE**: 这种循环在满足条件时执行循环体,条件在...

    基于MySQL游标的具体使用详解

    WHILE (tmpname IS NOT NULL) DO SET tmpName = CONCAT(tmpName, ';'); SET allName = CONCAT(allName, tmpName); FETCH cur1 INTO tmpName; END WHILE; ``` 循环结束后,关闭游标: ```sql CLOSE cur1; ...

    微软内部资料-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 ...

    Linux下的shell编程入门

    - **循环语句**:`for`、`while`和`until`用于重复执行一段代码,如遍历文件或执行特定次数: ``` for i in {1..5}; do echo "This is loop iteration $i" done ``` 3. **函数**:在Shell脚本中定义函数,可...

    mysql存储过程基础之遍历多表记录后插入第三方表中详解

    WHILE 条件 DO 循环体; END WHILE; ``` 2. **LOOP循环**: ```sql loop_name: LOOP if 条件 THEN LEAVE loop_name; -- 结束循环 end if; END LOOP loop_name; ``` 3. **REPEAT循环**: ```sql REPEAT ...

    微软内部资料-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 ...

    linux下shell编程教程

    4. **循环结构**:包括`for`、`while`和`until`循环,用于重复执行一段代码。例如,一个简单的`for`循环: ``` for i in {1..5}; do echo "This is loop iteration $i" done ``` 5. **函数**:在Shell中,你...

    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 ...

Global site tag (gtag.js) - Google Analytics