while 明明符合条件 但不能python 跳出while循环循环

条件,循环和其他语句
Table of Contents
1 print和import的更多信息
1.1 使用逗号输出
说明:使用print时,也可以在语句中添加多个表达式,每个表达式用逗 号分隔;
注意:在用逗号分隔输出时,print语句会在每个输出项后面自动添加一 个空格;
&&& greeting = 'Hello'
&&& salution = 'Mr.'
&&& name = 'Bill'
#以逗号分隔输出项
&&& print(greeting, salution, name)
Hello Mr. Bill
#在逗号前增加了一个空格符
&&& print(greeting, ',', salution, name)
Hello , Mr. Bill
#为了显示成'Hello, Mr. Bill'这个样式,可以使用连接符&+&
&&& print(greeting + ',', salution, name)
Hello, Mr. Bill
1.2 把一些东东作为另一些东东导入
将整个模块导入,格式为:
从某个模块中导入某个函数,格式为:from somemodule
从某个模块中导入多个函数,格式为:from somemodule import firstfunc, secondfunc, thirdfunc
将某个模块中的全部函数导入,格式为:from somemodule import *
注意:如果两个模块中都有相同的函数,则可以使用第一种方法导入模块, 也可以使用关键字 as为相同的函数取个别名,例子:
#第一种导入方法
import module1
import module2
#调用同名函数的方法
module1.open()
module2.open()
#第二种导入方法
#导入函数,并给函数取相应的别名
from module1 import open as open1
from module2 import open as open2
#从math中导入sqrt
&&& from math import sqrt as msqrt
#从cmath中导入sqrt
&&& from cmath import sqrt as csqrt
&&& msqrt(100)
&&& csqrt(-1)
2 赋值魔法
2.1 序列解包
说明:多个赋值操作可以同时进行
#一般的同时赋值操作
&&& x, y, z = (1,2,3)
#从字典中弹出任意一对儿键值对儿,并赋值给两个变量
&&& people = {'first': 'Andy', 'second':'Bill'}
&&& key, value = people.popitem()
2.2 链式赋值
说明:同时将一个值赋给多个变量;
&&& x=y=z=1
2.3 增量赋值
说明:包括以下增量操作:
+=:将右侧的值加到变量上的和,然后再赋值给变量;
-=:将变量减去右侧的值得到的差,再赋值给变量;
/=:用变量除以右侧值得到的商,再赋值给变量;
%=:用变量取右侧值的余数,再赋值给变量;
注意: += 和 \*= 还可以应用在字符串上,见下面的示例;
#针对数字的各种操作
&&& x = 123
&&& x += 1
&&& x -= 4
#字符串的增量赋值
&&& y = 'Test string'
&&& y += ', haha!'
'Test string, haha!'
&&& y *= 2
'Test string, haha!Test string, haha!'
3 语句块:缩排的乐趣
说明:语句块是一组语句,在代码前放置空格来缩进语句即可创建语句 块;
4 条件和条件语句
4.1 这就是布尔变量的作用
说明:布尔值,
假值:false,None,所有类型的数字0,空序列,空字典;
真值:所有的非空值;
bool函数可以用来将其他值转换成布尔值;
注意:尽管假值具有不同的类型,但是不同的假值之前也是 不相等 的
&&& bool ([])
&&& bool ([1,])
&&& bool (0)
&&& bool (0.0)
&&& bool (0.1)
#不同的假值之间也是不相同的
&&& [] == {}
&&& [] == None
4.2 条件执行和if语句
说明:if 判断其后面的条件语句是否为真,如果为真,执行if后面的语句 块,否则不执行;
4.3 else子句
说明:之所以称为子句是因为else必须跟在if语句后面,而不能单独使用;
4.4 elif子句
说明:如果需要更多的判断,可以使用elif,判断更多的条件;
#if, elif, else应用
num = input("Please enter a number:")
num = int(num)
if num & 0:
print ('You input a positive number!')
elif num & 0:
print ('You input a negative number!')
print ('You input a zero!')
4.5 嵌套代码块
说明:在if判断后,还需要进一步进行判断就可以使用嵌套代码的方式。
key = input("Please select type, color(c) or number(n):")
if key == 'c':
color = input ("Please select a color, Red(r), Green(g), Blue(b):")
if color == 'r':
print('You selected red')
elif color == 'g':
print('You selected green')
elif color == 'b':
print('You selected blue')
print("Illegal color type!")
print ("You select number!")
4.6 更复杂的条件
4.6.1 比较运算符
x==y: 等于;
x&y:  小于;
x&y: 大于;
x&=y: 小于等于;
x&=y: 大于等于;
x!=y: 不等于;
x is y:x和y是同一对象;
x is not y:x和y不是同一对象;
x in y: x在y中;
x not in y: x不在y中;
比较运算符是可连接的,例如:14 & age & 26;
比较运算符不能比较不同类型的数据;
4.6.2 相等运算符
说明:用来判断两个数据是否相等;
4.6.3 同一性运算符
说明:用于判断两个变量是否指向同一对象;
注意:避免把 is 比较运算符应用于比较常量值,如数字,字符串等。 即 避免以下比较:
if '123' is '123':
4.6.4 成员资格运算符
说明:判断元素是否被包含在对象中;
4.6.5 字符串和序列比较
说明:字符串可以按照字母顺序排列进行比较;
4.6.6 布尔运算符
说明:包括,and, or, not
#or的特殊用法,如果没有输入,则会返回or后面的值
&&& name = input("Please enter a name:") or '&unknown&'
Please enter a name:
'&unknown&'
&&& a = 'a'
&&& c = 'c'
#如果if后面的判断语句为真,返回a
&&& a if True else c
#如果if后面的判断语句为假,返回c
&&& a if False else c
说明:关键字为 assert , 如果断言的条件判断为假,则程序直接崩溃
&&& age = 10
&&& assert 1&age&120, "Age must be realistic"
&&& age = -1
&&& assert 1&age&120, "Age must be realistic"
Traceback (most recent call last):
File "&pyshell#26&", line 1, in &module&
assert 1&age&120, "Age must be realistic"
AssertionError: Age must be realistic
5.1 while循环
说明:关键字 while ,判断条件为真就一直执行
while not name.strip():
name = input("Please input your name:")
print("Hello,", name)
5.2 for循环
说明:可以用于迭代集合中的每个元素;
#遍历列表中的各个元素
&&& x = [1,2,3,4,5]
&&& for number in x:
print (number)
#使用内建函数range
&&& x = range(10)
range(0, 10)
&&& for number in x:
print(number)
5.3 循环遍历字典元素
说明:通过keys遍历字典,或者通过values;
x = {'a':'1', 'b':'2', 'c':'3'}
for key in x.keys():
print (key, x[key])
for val in x.values():
print(val)
5.4 一些迭代工具
5.4.1 并行迭代
说明:zip内置函数可以将多个序列&压缩&成一个元组的序列;
&&& x = list(range(0,5))
&&& y = list(range(5,10))
&&& z = list(range(10, 15))
[10, 11, 12, 13, 14]
[5, 6, 7, 8, 9]
[0, 1, 2, 3, 4]
&&& zipped = zip(x, y, z)
&&& list(zipped)
[(0, 5, 10), (1, 6, 11), (2, 7, 12), (3, 8, 13), (4, 9, 14)]
5.4.2 编号迭代
说明:使用内建函数enumerate来进行迭代操作;
&&& mylist = ['12312', '12ab', '123sa', '1231s']
&&& for index, string in enumerate(mylist):
print(index, string)
5.4.3 翻转和排序迭代
说明:内建函数reversed用于翻转序列,内建函数sorted用于对序列排 序,他们都是返回操作后的序列,不对原序列进行修改;
&&& data = [1,67,1,13,14,61,2]
&&& sorted(data)
[1, 1, 2, 13, 14, 61, 67]
&&& list(reversed(data))
[2, 61, 14, 13, 1, 67, 1]
5.5 跳出循环
5.5.1 break
说明:符合条件时直接中断循环;
&&& import math
&&&for x in range(99, 0, -1):
root = math.sqrt(x)
if root == int(root):
print ('Max number is:', x)
Max number is 81
5.5.2 continue
说明:结束当前循环,并跳到下一轮循环开始;
#一个打印偶数的例子,不加else 语句,程序也能正确执行
&&& for x in range(10):
if x%2 == 0:
5.5.3 while True/break
说明:while True部分实现了一个永不停止的循环,由内部的if判断语 句控制跳出循环;
while True:
word = input("Please enter a word:")
if not word:
print("You input:" , word)
Please enter a word:TEst
You input: TEst
Please enter a word:ls
You input: ls
Please enter a word:
5.5.4 循环中的else子句
说明:else子句可以用于判断循环操作是否始终没有执行break操作。
#设置一个奇数序列,判断里面是不是有偶数(一个蛋疼的程序,哈哈)
x = list(range(1,100,2))
for val in x:
if val%2 == 0:
print("Did not break!")
Did not break!
6 列表推导式
说明:利用其他列表创建列表,利用for循环遍历序列,将元素执行相应的 操作;
#得到10以内数字的平方的列表
import math
mylist = [math.pow(x, 2) for x in list(range(0,10))]
print (mylist)
#得到10以内偶数的平方的列表
mylist = [math.pow(x, 2) for x in list(range(0,10)) if x % 2 == 0]
print (mylist)
说明:pass关键字用于占位,当函数或者代码块还没有添加时,可以用 pass来占位,以免语法错误
&&& a = 10
#if的语句块中并没有其他语句需要执行,先用pass占位,执行的时候,如果if判断为真直接跳过。
&&& if a&0:
说明:用于删除对象;
注意:del仅能删除变量或者对象中的项,不能直接删除变量指向的对象, 当对象没有被任何变量引用时,python会将变量回收;
&&& x = {'a':'1', 'b':'2', 'c':'3'}
{'a': '1', 'c': '3', 'b': '2'}
#删除变量x,再调用会报&未定义&的错误
Traceback (most recent call last):
File "&pyshell#15&", line 1, in &module&
NameError: name 'x' is not defined
{'a': '1', 'c': '3', 'b': '2'}
#删除字典中的项
&&& del y['a']
{'c': '3', 'b': '2'}
7.3 exec和eval
exec用于执行一个字符串的语句;
eval用于执行字符串语句,并返回语句执行的结果;
注意:通过增加字典,起到命名空间的作用,以防止由字符串的语句导致 的安全问题;
#exec直接执行语句
&&& exec('print("Hello, world!")')
Hello, world!
#exec执行后不返回执行结果
&&& exec("2*2")
#exec在命名空间中执行语句
&&& exec("""
""", scope)
&&& scope.keys()
dict_keys(['__builtins__', 'x', 'z', 'y'])
&&& scope['x']
#eval直接执行语句
&&& eval('print("Hello, world!")')
Hello, world!
#eval在执行后将执行结果返回
&&& eval('2*2')
#eval操作字典中的数据
&&& scope.keys()
dict_keys(['__builtins__', 'x', 'z', 'y'])
&&& eval('x+y+z', scope)
Org version 7.7 with Emacs version 23
阅读(...) 评论()文件操作问题,文件明明是空的while(!feof(fp)){}却要循环一次_c语言吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:311,301贴子:
文件操作问题,文件明明是空的while(!feof(fp)){}却要循环一次收藏
int main(void){Lint choice=1;Node *p;Node *FILE *head=(Node*)malloc(sizeof(Node));
/*链表头建立*/
printf("\n allocate memory failure ");
/*如没有申请到,打印提示信息*/
/*返回主界面*/
}head-&next=NULL;tail=fp=fopen("D:\\data.txt","a+");if(fp==NULL)
/*打开文件*/{printf("FILE open error!\n");exit(0);}while(!feof(fp))
{p=(Node*)malloc(sizeof(Node));
if(!p){printf("\n allocate memory failure!\n");return 0;}fscanf(fp," %s",p-&data.Register);fscanf(fp,"%s",p-&data.Book_title);fscanf(fp,"%s",p-&data.Writer);fscanf(fp,"%s",p-&data.Class_num);fscanf(fp,"%s",p-&data.Publish_data);fscanf(fp,"%s",p-&data.Publisher);fscanf(fp,"%d",&p-&data.Price);p-&next=NULL;tail-&next=p;tail=p;}fclose(fp); /*关闭文件*/
printf("请输入你的操作序号(0--4):");
/*选择操作*/
scanf("%d",&choice);if(choice==0)
/*如果选择0,退出*/{printf("=====&thank you for useness!");getchar();}switch(choice){case 1:Input(head);case 2:Output(head);getch();case 3:Search(head);getch();case 4:Update(head);getch();case 5:Delete(head);getch();default:wrong();}}getchar();return 0;} 这是代码;
嗯 这是feof()函数的一个特性。。。。要判断是否读到末尾你是使用 fget(文件名)==EOF判断
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或Java 的do...while循环问题_百度知道
Java 的do...while循环问题
请问循环执行了多少次求助各位大神;= 5);do{
x--;}while(x &lt!int x = 10
提问者采纳
行一次;=5不成立,所以不会循环执行了,当do的时候,而此条件是9&lt,不管条件符不符合都会执行一次,执行完以后X=9
其他类似问题
为您推荐:
其他24条回答
楼上的各位.明明是一次后不满足条件就不执行了.........晕死我了.while否则是while的话...咋会是6次啊..,幸好是do
是这样的:1.while循环语句是假如条件不满足就一次也不执行,但是do while是先执行后检查是否满足,所以至少会执行一次。2.此处因为x本身就不满足x&=5的条件,但是do while还会再执行一次才退出,所以是一次!
do while循环,先执行一次do的函数体,然后再根据while的条件来判断继续执行该函数体与否。
1次。 10--,然后是9以后不执行。你可以再代码后 输出x的值。 就知道几次了呀。
do...while 不论什么情况循环至少执行一次,由于执行一次后x&=5为false,所以循环执行一次就结束了
1次do while和while的区别就是,do while不论是否满足条件都会执行一次。
while是当满足条件后再执行。所以。你这个只能执行一次
一共执行了1次,因为do-while他是先做在执行,所以这个程序他是一开始就执行了,但是执行完第一次后x=9,但是条件是要x&=5,所以不符合条件,所以跳出循环,所以一共就只是执行了一次
do while是先执行后判断,至少执行一次,9 8 7 6 5 4,一共执行6次,当x=4时,跳出
同学你好。do while的循环是指先执行后判断,所以无论x本次是否满足都会先执行,第一次执行后x=9,但判断不满足x&=5,故循环结束。所以执行一次。
循环1次,do...while()循环是先执行一次,再判断条件、你的程序执行一次后判断条件为false,退出循环,所以只执行了一次
1次啊!do while和while都是当满足条件时执行循环体,但是区别是do while至少要执行一次循环体。
1次。do while循环是先执行一次,然后判断条件,如果条件为true就继续执行,false就跳出循环。
我怎么感觉你这个成为死循环了呢?题目是不是while(x &= 5)啊??奥,不会是死循环,是只执行一次因为
while循环满足或者不满足都要循环一次的
循环一次mmmmmm
一共循环了5次,执行了6次
while()的括号里的数不是判断条件吗?执行一次后 x = 9, 9 &= 5不成立呀,为什么会执行 6 次?
看错条件了,执行一次,无循环!
1次这种问题用eclipse工具跑一下就很快可以看到结果了
一次啊,如果改为x&=5就循环6次
先do后判断!do一次以后条件不符合跳出循环,所以是一次!
你在x--里面加个System.out.println(&次数&)看打印几次就知道了
一共执行6次
除了一楼。。。都是人才
只能执行一次吧
1次啊。。。
while的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁Shell 脚本之循环语句(for,while,until)以及case,select,break,c,Shell,脚本,循环,语句,for,while,until_LINUX_【联盟电脑】
您现在的位置: >
Shell 脚本之循环语句(for,while,until)以及case,select,break,c
关于shell脚本的更多详细实例讲解请参考:/yuexiaxiaoxi/category/646749.htmlShell编程中循环命令用于特定条件下决定某些语句重复执行的控制方式,有三种常用的循环语句:for、while和until。while循环和for循环属于“当型循环”,而until属于“直到型循环”。循环控制符:break和continue控制流程转向。参考:《Linux 与unix shell 编程指南》一、while语句结构w h i l e循环用于不断执行一系列命令,也用于从输入文件中读取数据,其格式为:while 命令d o命令1命令2. . .d o n e虽然通常只使用一个命令,但在 w h i l e和d o之间可以放几个命令。命令通常用作测试条件。只有当命令的退出状态为 0时,d o和d o n e之间命令才被执行,如果退出状态不是 0,则循环终止。命令执行完毕,控制返回循环顶部,从头开始直至测试条件为假。实例#!/bin/bash# Program:#
This program will show the use of if# History:#
Alex First releasei=10while [[ $i -gt 5 ]]do
i=`expr $i - 1`;
doneexit 0注意:在Shell中四则运算不能简简单单的加减乘除,应该要写如下的格式:&val1=`$val2 - 1`其中“=”后面用“`”包住表达式,这个符号在Shell中十分有用,是Tab键上面“~”的原来形式。可以用来将很多命令的结果保存到一个变量中去。接着就是运算符了,运算符的左右两边必须是空格,否则会出错。deyuy/bin/my_shell && ./while1.sh109876注意:  1.expr命令的用法请参考 http://blog.chinaunix.net/uid--id-2937521.html     2.``的作用是运行``之间的命令,并且将命令运行的结果返回。详细请参考:http://blog.csdn.net/miyatang/article/details/8077123     3. 更多while实例参考:http://blog.csdn.net/firefoxbug/article/details/7237319二、until语句u n t i l循环执行一系列命令直至条件为真时停止。 u n t i l循环与w h i l e循环在处理方式上刚好相反。一般w h i l e循环优于u n t i l循环,但在某些时候―也只是极少数情况下, u n t i l循环更加有用。u n t i l循环格式为:until 条件命令1. . .d o n e条件可为任意测试条件,测试发生在循环末尾,因此循环至少执行一次―请注意这一点。until循环中,只要条件不为真,就执行do和done之间的循环命令,或者说,在until循环中,一直执行do和done之间的循环命令,直到条件为真;C避免生成死循环。&# Program:#
This program will show the use of until# History:#
Alex First releasesum=0num=10until test $num -eq 0
sum=`expr $sum + $num`
num=`expr $num - 1`
doneecho &sum = $sum&exit 0deyuy/bin/my_shell && sh until.shsum = 55三、for 语句结构f o r循环一般格式为:for 变量名i n列表d o命令1命令2…d o n e当变量值在列表里, f o r循环即执行一次所有命令,使用变量名访问列表中取值。命令可为任何有效的s h e l l命令和语句。变量名为任何单词。&in列表用法是可选的,如果不用它, f o r循环使用命令行的位置参数。in列表可以包含替换、字符串和文件名,列表可以自定义,也可以通过命令返回值生成,下面是一些常用例子。1) 整数列表 &#!/bin/bash# Program:#
This program will show the use of for# History:#
First release# 自定义列表for loop in 1 2 3 4 5do
echo &loop=$loop&doneexit 0deyuy/bin/my_shell && chmod u+x for1.shdeyuy/bin/my_shell && ./for1.shloop=1loop=2loop=3loop=4loop=5还可以通过读取文件内容生成变量列表deyuy/bin/my_shell && vim num.txt1 2 3 4 5678#!/bin/bash# Program:# This program will show the use of for# History:#
First release# 以命令返回值作为列表i=0for i in `cat num.txt`doecho &i=$i&doneexit 0deyuy/bin/my_shell && ./for1.shi=1i=2i=3i=4i=5i=6i=7i=82) 字符串列表#!/bin/bash# Program:#
This program will show the use of for# History:#
First release# 自定义列表:带引号i=0for loop in &apple hhhh bbbb eeee&do     i=`expr $i + 1`
echo &loop=$loop&     echo &count=$i&doneexit 0deyuy/bin/my_shell && chmod u+x for4.shdeyuy/bin/my_shell && ./for4.shloop=apple hhhh bbbb eeeecount=1说明:从结果可以看出for循环打印字符串到结束,包括空格,只执行了一次。如果把 in列表改为 for loop in &apple& &hhhh& &bbbb& &eeee&则输出结果为loop=applecount=1loop=hhhhcount=2loop=bbbbcount=3loop=eeeecount=4#!/bin/bash# Program:#
This program will show the use of for# History:#
First release# 自定义列表,不带引号i=0for loop in apple hhhh bbbb eeeedo    i=`expr $i + 1`
echo &loop=$loop&   
echo &count=$i&  doneexit 0deyuy/bin/my_shell && ./for4.shloop=applecount=1loop=hhhhcount=2loop=bbbbcount=3loop=eeeecount=4注意:对比两张方式输出的不同可以看出,如果用户想让把空格分割的每个单词都输出要通过第二种方式。
3) 打印当前目录下所有文件# Program:#
This program will show the use of for# History:#
First releasefor loop in `ls`do
echo &$loop&doneexit 0deyuy/bin/my_shell && sh for2.shapp1for1.shfor2.shfun1.shhello.shhh.shif.shif2.shtest.shtest2.shvar.shwhile1.sh4) 循环计数#!/bin/bash# Program:#
This program will show the use of for #
统计当前目录下文件数# History:#
First releasecounter=0for files in *do
counter=`expr $counter + 1`doneecho &There are $counter files in `pwd` directory.&exit 0deyuy/bin/my_shell && sh for3.shThere are 13 files in /home/deyuy/bin/my_shell directory.5) 使用位置参数#!/bin/bash# Program:#
This program will show the use of for# History:#
releasei=0for paramsdo
i=`expr $i + 1`
echo &You supplied $params as a command line option&
echo &count=$i&done
echo $paramsexit 0deyuy/bin/my_shell && ./for5.sh p1 p2 p3You supplied p1 as a command line optioncount=1You supplied p2 as a command line optioncount=2You supplied p3 as a command line optioncount=3p3下面的脚本包含i n&$ @&,结果与上面的脚本相同。#!/bin/bash# Program:#
This program will show the use of for# History:#
releasei=0for params in &$@&do
i=`expr $i + 1`
echo &You supplied $params as a command line option&
echo &count=$i&done
echo $paramsexit 0deyuy/bin/my_shell && ./for5.sh p1 p2 p3You supplied p1 as a command line optioncount=1You supplied p2 as a command line optioncount=2You supplied p3 as a command line optioncount=3p3下面的脚本包含i n&$ *&,结果与上面的脚本不同。#!/bin/bash# Program:#
This program will show the use of for# History:#
releasei=0for params in &$*&do
i=`expr $i + 1`
echo &You supplied $params as a command line option&
echo &count=$i&done
echo $paramsexit 0deyuy/bin/my_shell && ./for5.sh p1 p2 p3You supplied p1 p2 p3 as a command line optioncount=1p1 p2 p3说明:关于$@和$*的区别请参考/yuexiaxiaoxi/articles/4203609.html6)循环嵌套嵌入循环可以将一个f o r循环嵌在另一个f o r循环内:for 变量名1 in列表1d ofor 变量名2 in 列表2d o命令1. . .d o n ed o n e下面脚本即为嵌入f o r循环,这里有两个列表A P P S和S C R I P T S。第一个包含服务器上应用的路径,第二个为运行在每个应用上的管理脚本。对列表 A P P S上的每一个应用,列表S C R I P T S里的脚本将被运行,脚本实际上为后台运行。脚本使用 t e e命令在登录文件上放一条目,因此输出到屏幕的同时也输出到一个文件。查看输出结果就可以看出嵌入 f o r循环怎样使用列表S C R I P T S以执行列表A P P S上的处理。&#!/bin/bash# Program:#
This program will show the use of for# History:#
releaseAPPS=&/apps/accts /apps/claims /apps/stock /apps/serv&SCRIPTS=&audit.check report.run cleanup&MY_DATE=`date +%H:%M& on &%d/%m/%Y`i=0j=0#outer loopfor loop in $APPSdo
i=`expr $i + 1`#inner loop
for loop2 in $SCRIPTS
j=`expr $i + 1`
echo &system $loop now running $loop2 at $MY_DATE&
echo &inner loop counter=$j&
echo &outer loop counter=$i&done
echo $paramsdeyuy/bin/my_shell && ./for6.shsystem /apps/accts now running audit.check at 20:53 on 12/01/2015inner loop counter=2system /apps/accts now running report.run at 20:53 on 12/01/2015inner loop counter=2system /apps/accts now running cleanup at 20:53 on 12/01/2015inner loop counter=2outer loop counter=1system /apps/claims now running audit.check at 20:53 on 12/01/2015inner loop counter=3system /apps/claims now running report.run at 20:53 on 12/01/2015inner loop counter=3system /apps/claims now running cleanup at 20:53 on 12/01/2015inner loop counter=3outer loop counter=2system /apps/stock now running audit.check at 20:53 on 12/01/2015inner loop counter=4system /apps/stock now running report.run at 20:53 on 12/01/2015inner loop counter=4system /apps/stock now running cleanup at 20:53 on 12/01/2015inner loop counter=4outer loop counter=3system /apps/serv now running audit.check at 20:53 on 12/01/2015inner loop counter=5system /apps/serv now running report.run at 20:53 on 12/01/2015inner loop counter=5system /apps/serv now running cleanup at 20:53 on 12/01/2015inner loop counter=5outer loop counter=4四、case结构c a s e语句为多选择语句。可以用 c a s e语句匹配一个值与一个模式,如果匹配成功,执行相匹配的命令。c a s e语句格式如下:case 值 i n模式1 }命令1. . .;;模式2)命令2. . .;;e s a cc a s e工作方式如上所示。取值后面必须为单词 i n,每一模式必须以右括号结束。取值可以为变量或常数。匹配发现取值符合某一模式后,其间所有命令开始执行直至;;。取值将检测匹配的每一个模式。一旦模式匹配,则执行完匹配模式相应命令后不再继续其他模式。如果无一匹配模式,使用星号 *捕获该值,再接受其他输入。模式部分可能包括元字符,与在命令行文件扩展名例子中使用过的匹配模式类型相同,即:* 任意字符。? 任意单字符。[..] 类或范围中任意字符。注意:1.模式字符串中可以使用通配符2.如果一个模式字符串中包含多个模式,那么各模式之间应以竖线(|)隔开,表各模式是“或”的关系,即只要给定字符串与其中一个模式相配,就会执行其后的命令列表。3.各模式字符串应是唯一的,不应重复出现,并且要合理安排它们的出现顺序,例如,不应将“*”作为头一个模式字符串,因为“*”可以与任何字符串匹配,若第一个出现,就不会再检查其他模式了。4.case语句以关键字case开头,以关键字esac结束。5.case的退出(返回)值是整个结构中最后执行的命令的退出值。若没有执行任何命令,则退出值为0.#!/bin/bash# Program:#
This program will show the use of for# History:#
First releasecase $1 iny|Y)
echo &your choice is yes&;;n|N)
echo &your choice is no&;;*)
echo &your choice is others&;;esacexit 0deyuy/bin/my_shell && chmod u+x case.shdeyuy/bin/my_shell && ./case.sh yyour choice is yesdeyuy/bin/my_shell && ./case.sh nyour choice is nodeyuy/bin/my_shell && ./case.sh jjjjyour choice is others五、Select结构格式:select&变量 in 列表do命令行(通常用到循环变量)done&&& 制作一个选择表,在列表中选择一个选项执行命令行。如果选择的变量不在列表序列中,则返回一个空值。需要用break退出循环。#!/bin/bash# Program:#
This program will show the use of for# History:#
First releaseecho &a is 5 ,b is 3. Please select your method: &a=5b=3select var in &a+b& &a-b& &a*b& &a/b&do
breakdonecase $var in&a+b&)
echo 'a+b= '`expr $a + $b`;;&a-b&)
echo 'a-b= '`expr $a - $b`;;&a*b&)
echo 'a*b= '`expr $a /* $b`;;&a/b&)
echo 'a/b= '`expr $a / $b`;;*)
echo &input error&esacdeyuy/bin/my_shell && chmod u+x select.shdeyuy/bin/my_shell && ./select.sha is 5 ,b is 3. Please select your method:1) a+b2) a-b3) a*b4) a/b#? 3a*b= 15六、break和continueC1、break:用于立即终止当前循环的执行,break命令可以使用户从循环体中退出来。C语法:break[n] ,其中,n表示要跳出几层循环,默认值为1C2、continue:跳过循环体中在其之后的语句,会返回到本循环层的开头,进行下一次循环。C语法:continue[n],其中,n表示从包含continue语句的最内层循环体向外跳到第几层循环,默认值为1,循环层数是由内向外编号。
(责任编辑:联盟电脑)
更多相关资讯
1、LVS的定义? LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集.....
【联盟电脑】部分内容自于互联网,其言论不代表本站观点,若本站侵犯到您的版权,请与站长联系,我们将在第一时间核实并删除!
版权所有 & 【联盟电脑】 | 专注分享有价值、实用的电脑技术和知识
Copyright &
All rights reserved. 京ICP备号

我要回帖

更多关于 vb跳出while循环 的文章

 

随机推荐