13.2 C语言风格的for命令、while命令和until命令

C语言风格的for命令

在C语言中,for循环通常定义ige变量,然后这个变量会在每次迭代时自动改变。

c语言的for命令

C语言的for命令有一个用来指明变量的特定方法,一个必须保持成立才能继续迭代的条件,以及另一个在每个迭代中改变变量的方法,当条件不成立时,for循环就会停止。条件等式通过标准的数学符号定义。如下:

[chendajie@CHENDAJIE for]$ cat for.c 
#include<stdio.h>

/*      Use of for statements   */
main()
{
        int i;
        for(i = 0; i < 10; i++)
                printf("%d\n", i);
}

执行情况:

[chendajie@CHENDAJIE for]$ ./a.out 
0
1
2
3
4
5
6
7
8
9

可以看到该程序执行至i=10时就结束了。
下面是bash中C语言风格的for循环的基本格式

for (( variable assigment ; condition ; iteration process ))

C语言风格的for命令看起来是这样的:

for (( a = 1; a < 10; a++ ))

和bash shell传统的不同点有以下几种:
1、变量赋值可以有空格;
2、条件中的变量不以美元符开头
3、迭代过程的算式未使用expr命令格式
下面是例子:

[chendajie@CHENDAJIE C_Like_For]$ cat test1 
#!/bin/bash
# Testing the C-style for loop

for (( i = 1; i < 10; i++ ))
do 
        echo "The next number is $i"
done
[chendajie@CHENDAJIE C_Like_For]$ chmod u+x test1 
[chendajie@CHENDAJIE C_Like_For]$ ./test1 
The next number is 1
The next number is 2
The next number is 3
The next number is 4
The next number is 5
The next number is 6
The next number is 7
The next number is 8
The next number is 9

使用多个变量

C语言风格的for命令也允许为迭代使用多个变量。循环会单独处理每个变量,尽管可以使用多个变量,但只能在for循环中定义一种条件。

[chendajie@CHENDAJIE C_Like_For]$ cat test2
#!/bin/bash
# Testing a multicommand while loop

for (( a = 1, b = 10; a <= 10; b--, a++ ))
do
        echo "$a - $b"
done
#
[chendajie@CHENDAJIE C_Like_For]$ chmod u+x test2
[chendajie@CHENDAJIE C_Like_For]$ ./test2
1 - 10
2 - 9
3 - 8
4 - 7
5 - 6
6 - 5
7 - 4
8 - 3
9 - 2
10 - 1

变量a和变量b分别使用了不同的值来初始化并且定义了不同的迭代过程,循环的每次迭代在增加变量a的同时减少了变量b。

while命令

while命令某种意义上上是if–then语句和for循环的混杂体。while命令允许定义一个要测试的命令,然后循环执行一组命令,只要定义的测试命令返回的是退出状态码0.它会在每次迭代的一开始测试test命令。在test命令返回非零退出状态码时,while命令会停止执行那组命令。
例:

[chendajie@CHENDAJIE while]$ cat test1 
#!/bin/bash
# While command test

var1=10

while [ $var1 -gt 0 ]
do
        echo $var1
        var1=$[ $var1 -1 ]
done
[chendajie@CHENDAJIE while]$ chmod u+x test1^C
[chendajie@CHENDAJIE while]$ ./test1 
10
9
8
7
6
5
4
3
2
1

只要测试条件成立,while命令就会不停止循环执行定义好的命令,在这些命令中,测试条件用到的变量必须改变,否则会陷入无限循环。

使用多个测试命令

[chendajie@CHENDAJIE while]$ cat -n test2
     1  #!/bin/bash
     2  # Testing a multicommand while loop
     3
     4  var1=10
     5
     6  while echo $var1
     7          [ $var1 -ge 0 ]
     8  do
     9          echo "This is inside the loop"
    10          var1=$[ $var1 -1 ]
    11  done
[chendajie@CHENDAJIE while]$ chmod u+x test2
[chendajie@CHENDAJIE while]$ ./test2
10
This is inside the loop
9
This is inside the loop
8
This is inside the loop
7
This is inside the loop
6
This is inside the loop
5
This is inside the loop
4
This is inside the loop
3
This is inside the loop
2
This is inside the loop
1
This is inside the loop
0
This is inside the loop
-1

在这个例子中,while语句中定义了两个测试命令

while echo $var1
	[ $var1 -ge 0 ]	# var1是否大于或等于0
var1=$[ $var1 -1 ]	# 每次都执行var1-1

注意此时程序结束的时候var1=-1;
while循环会在var1变量等于0时执行echo语句,然后将var1变量的值减一。接下来再次执行测试命令,用于下一次迭代。echo测试命令被执行并显示了var1变量的值。知道shell执行test测试命令,while循环才会结束。

until命令

until命令和while命令工作的方式完全相反。until命令要求你指定一个通常返回非零退出状态码的测试命令。只有测试命令的退出状态码不为0,bash shell才会执行循环中列出的命令。
格式如下:

until test commands
do
	other commands
done

和while命令类似,你可以在until命令语句中放入多个测试命令。只有最后一个命令的退出状态码决定了bash shell是否执行已定义的other commands。
例:

[chendajie@CHENDAJIE until]$ cat -n test1 
     1  #!/bin/bash
     2  # Using the until command
     3
     4  var1=100
     5
     6  until [ $var1 -eq 0 ]
     7  do
     8          echo $var1
     9          var1=$[ $var1 - 25 ]
    10  done
[chendajie@CHENDAJIE until]$ chmod u+x test1 ^C
[chendajie@CHENDAJIE until]$ ./test1 
100
75
50
25

上面的例子测试了until循环何时停止。只要返回状态码为0时就会停止了。
同while命令一样,在until命令中使用多个测试命令时要注意:

[chendajie@CHENDAJIE until]$ cat -n test2
     1  #!/bin/bash
     2  # Using the until command
     3
     4  var1=100
     5
     6  until echo $var1
     7          [ $var1 -eq 0 ]
     8  do
     9          echo Inside the loop: $var1
    10          var1=$[ $var1 -25 ]
    11  done
[chendajie@CHENDAJIE until]$ chmod u+x test2
[chendajie@CHENDAJIE until]$ ./test2
100
Inside the loop: 100
75
Inside the loop: 75
50
Inside the loop: 50
25
Inside the loop: 25
0

shell会执行指定的多个测试命令,只有在最后一个命令成立时停止。

刚好最近做毕业设计要求用到C语言在复习,感叹一下自己学艺不精哈。怎么说呢,加油吧。欲买桂花同载酒,终不似,少年游。

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值