天天看点

循环结构 案例分析

怎样才能知道循环是否提前结束了 循环通常依赖于一个或多个变量,你可以在循环外检查这些变量,以确保循环被正确执行。请看下例:

int x

char * cp[REQUESTED_BLOCKS]

/ * Attempt (in vain, I must add... )to

     allocate 512 10KB blocks in memory.  * /

for (x = 0;  x<REQUESTED_ BLOCKS ; x++ )

{

     cpi[x]= (char * ) malloc (10000,1)

     if (cp[x]= = (char * ) NULL)

           break

}

/ * If x is less than REQUESTED-BLOCKS,

     the loop has ended prematurely.  * /

if (x<REQUESTED_BLOCKS)

  printf ("Bummer ! My loop ended prematurely ! \n" );

注意:如果上述循环执行成功,它一定会循环512次。紧接着循环的if语句用来测试循环次数,从而判断循环是否提前结束。如果变量x的值小于512,就说明循环出错了。

在C语言中 除了for语句中之外,在哪些情况下还要使用逗号运算符

逗号运算符通常用来分隔变量说明、函数参数、表达式以及for语句中的元素。

下例给出了使用逗号的多种方式:

#include <stdio.h>

#include <stdlib.h>

void main(void);

void main ()

     / * Here, the comma operator is used to separate

          three variable declarations.  * /

     int i, j, k;

     / * Notice how you can use the comma operator to perform

          multiple initializations on the same line.  * /

     i=0, j=1, k=2;

printf("i= %d, j=%d, k= %d\n", i, j, k);

     / * Here, the comma operator is used to execute three expressions

          in one line: assign k to i, increment j, and increment k.

          The value that i receives is always the rigbtmost expression.  * /

     i= ( j++, k++ );

     printf("i=%d, j=%d, k=%d\n", i, j, k);

     / * Here, the while statement uses the comma operator to

          assign the value of i as well as test it.  * /

     while (i=(rand() % 100), i !=50)

        printf("i is %d, trying again... \n", i)

     printf ("\nGuess what? i is 50!\n" )

请注意下述语句:

     i:(j++,k++)

这条语句一次完成了三个动作,依次为:

  1. 把k值赋给i。这是因为左值(lvaule)总是等于最右边的参数,本例的左值等于k。注意,本例的左值不等于k++,因为k++是一个后缀自增表达式,在把k值赋给j之后k才会自增。如果所用的表达式是++k,则++k的值会被赋给i,因为++k是一个前缀自增表达式,k的自增发生在赋值操作之前。
  2. j自增。
  3. k自增。

此外,还要注意看上去有点奇怪的while语句:

   while (i=(rand() % 100), i !=50)

   printf("i is %d, trying again... \n");

这里,逗号运算符将两个表达式隔开,while语句的每次循环都将计算这两个表达式的值。逗号左边是第一个表达式,它把0至99之间的一个随机数赋给i;第二个表达式在while语句中更常见,它是一个条件表达式,用来判断i是否不等于50。while语句每一次循环都要赋予i一个新的随机数,并且检查其值是否不等于50。最后,i将被随机地赋值为50,而while语句也将结束循环。

请参见:

1、运算符的优先级总能保证是“自左至右”或“自右至左”的顺序吗?

2、++var和var++有什么区别?

什么是C语言局部程序块(local block) 局部程序块是指一对大括号({})之间的一段C语言程序。一个C函数包含一对大括号,这对大括号之间的所有内容都包含在一个局部程序块中。if语句和swich语句也可以包含一对大括号,每对大括号之间的代码也属于一个局部程序块。

此外,你完全可以创建你自己的局部程序块,而不使用C函数或基本的C语句。

  1. 你可以在局部程序块中说明一些变量,这种变量被称为局部变量,它们只能在局部程序块的开始部分说明,并且只在说明它的局部程序块中有效。
  2. 如果局部变量与局部程序块以外的变量重名,则前者优先于后者。

下面是一个使用局部程序块的例子:

void main()

     / * Begin local block for function main() * /

     int test_ var = 10;

     printf("Test variable before the if statement: %d\n", test_var);

     if (test_var>5)

     {

           / * Begin local block for "if" statement * /

           int test_ var = 5;

           printf("Test variable within the if statement: %d\n",

                   test_var);

           {

                 / * Begin independent local block (not tied to

                      any function or keyword) * /

                  int test_var = 0;

                  printf (

                 "Test variable within the independent local block: %d\n",

                 test_var)

      }

      / * End independent local block * /

      printf ("Test variable after the if statement: %d\n", test_var);

/*End local block for function main () * /

上例产生如下输出结果:

Test variable before the if statement: 10

Test variable within the if statement: 5

Test variable within the independent local block:0

Test variable after the if statement: 10

注意:在这个例子中,每次test_var被定义时,它都要优先于前面所定义的test_var变量。此外还要注意,当if语句的局部程序块结束时,程序重新进入最初定义的test_var变量的作用范围,此时test_var的值为10。