日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

循环结构 案例分析

發布時間:2023/12/13 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 循环结构 案例分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

怎樣才能知道循環是否提前結束了

循環通常依賴于一個或多個變量,你可以在循環外檢查這些變量,以確保循環被正確執行。請看下例:

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++)
這條語句一次完成了三個動作,依次為:
  • 把k值賦給i。這是因為左值(lvaule)總是等于最右邊的參數,本例的左值等于k。注意,本例的左值不等于k++,因為k++是一個后綴自增表達式,在把k值賦給j之后k才會自增。如果所用的表達式是++k,則++k的值會被賦給i,因為++k是一個前綴自增表達式,k的自增發生在賦值操作之前。
  • j自增。
  • 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語句。
  • 你可以在局部程序塊中說明一些變量,這種變量被稱為局部變量,它們只能在局部程序塊的開始部分說明,并且只在說明它的局部程序塊中有效。
  • 如果局部變量與局部程序塊以外的變量重名,則前者優先于后者。

  • 下面是一個使用局部程序塊的例子:
    #include <stdio.h>
    void main(void);
    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。

    總結

    以上是生活随笔為你收集整理的循环结构 案例分析的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。