<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 3.C語言if語句詳解 用if語句可以構成分支結構。它根據給定的條件進行判斷,以決定執行某個分支程序段。C語言的if語句有三種基本形式。 ## 語句的三種形式 1) 第一種形式為基本形式:if ? ? ? ? if(表達式) 語句 其語義是:如果表達式的值為真,則執行其后的語句,否則不執行該語句。其過程可表示為下圖。 ![](http://www.lvtao.net/content/uploadfile/201404/e5b821262fb2ae56abdb2e77fefcfd4620140421041439.gif) 【例5-3】 ~~~ main(){ int a,b,max; printf("\n input two numbers: "); scanf("%d%d",&a,&b); max=a; if (max<b) max=b; printf("max=%d",max); } ~~~ 本例程序中,輸入兩個數a、b。把a先賦予變量max,再用if語句判別max和b的大小,如max小于b,則把b賦予max。因此max中總是大數,最后輸出max的值。 2) 第二種形式為: if-else if(表達式)? ? ? 語句1; else? ? ? 語句2; 其語義是:如果表達式的值為真,則執行語句1,否則執行語句2 。其執行過程可表示為下圖。 ![](http://www.lvtao.net/content/uploadfile/201404/9a94e38ef0a1423d3951febe6445a97720140421041439.gif) 【例5-4】 ~~~ main(){ int a, b; printf("input two numbers: "); scanf("%d%d",&a,&b); if(a>b) printf("max=%d\n",a); else printf("max=%d\n",b); } ~~~ 輸入兩個整數,輸出其中的大數。改用if-else語句判別a,b的大小,若a大,則輸出a,否則輸出b。 3) 第三種形式為if-else-if形式 前二種形式的if語句一般都用于兩個分支的情況。當有多個分支選擇時,可采用if-else-if語句,其一般形式為: ? ? ?if(表達式1) ??????? 語句1; ??? else? if(表達式2)? ??????? 語句2; ??? else? if(表達式3)? ??????? 語句3; ??????? …? ??? else? if(表達式m)? ??????? 語句m; ??? else ??????? 語句n; 其語義是:依次判斷表達式的值,當出現某個值為真時,則執行其對應的語句。然后跳到整個if語句之外繼續執行程序。 如果所有的表達式均為假,則執行語句n。然后繼續執行后續程序。 if-else-if語句的執行過程如下圖所示。 ![](http://www.lvtao.net/content/uploadfile/201404/128c0f5520da3ac09175aa975480c98b20140421041439.jpg) 【例5-5】 ~~~ #include"stdio.h" main(){ char c; printf("input a character: "); c=getchar(); if(c<32) printf("This is a control character\n"); else if(c>='0'&&c<='9') printf("This is a digit\n"); else if(c>='A'&&c<='Z') printf("This is a capital letter\n"); else if(c>='a'&&c<='z') printf("This is a small letter\n"); else printf("This is an other character\n"); } ~~~ 本例要求判別鍵盤輸入字符的類別。可以根據輸入字符的ASCII碼來判別類型。由ASCII碼表可知ASCII值小于32的為控制字符。在“0”和“9”之間的為數字,在“A”和“Z”之間為大寫字母, 在“a”和“z”之間為小寫字母,其余則為其它字符。這是一個多分支選擇的問題,用if-else-if語句編程,判斷輸入字符ASCII碼所在的范圍,分別給出不同的輸出。例如輸入為“g”,輸出顯示它為小寫字符。 在使用if語句中還應注意以下問題: 1. 在三種形式的if語句中,在if關鍵字之后均為表達式。 該表達式通常是邏輯表達式或關系表達式,但也可以是其它表達式,如賦值表達式等,甚至也可以是一個變量。例如: ? ? if(a=5) 語句; ? ? if(b) 語句; 都是允許的。只要表達式的值為非0,即為“真”。如在: ? ? if(a=5)…; 中表達式的值永遠為非0,所以其后的語句總是要執行的,當然這種情況在程序中不一定會出現,但在語法上是合法的。 又如,有程序段: ~~~ if(a=b) printf("%d",a); else printf("a=0"); ~~~ 本語句的語義是,把b值賦予a,如為非0則輸出該值,否則輸出“a=0”字符串。這種用法在程序中是經常出現的。 2. 在if語句中,條件判斷表達式必須用括號括起來,在語句之后必須加分號。 3. 在if語句的三種形式中,所有的語句應為單個語句,如果要想在滿足條件時執行一組(多個)語句,則必須把這一組語句用{}括起來組成一個復合語句。但要注意的是在}之后不能再加分號。例如: ~~~ if(a>b){a++; b++; }else{ a=0; b=10; } ~~~ ## if語句的嵌套 當if語句中的執行語句又是if語句時,則構成了if 語句嵌套的情形。其一般形式可表示如下: ? ? if(表達式)? ? ? ? ? if語句;? 或者為: ? ? if(表達式)? ? ? ? ? if語句;? ? ? else? ? ? ? ? if語句; 在嵌套內的if語句可能又是if-else型的,這將會出現多個if和多個else重疊的情況,這時要特別注意if和else的配對問題。例如: ? ? if(表達式1) ? ? ? ? if(表達式2) ? ? ? ? ? ? 語句1; ? ? ? ? else ? ? ? ? ? ? 語句2; 其中的else究竟是與哪一個if配對呢?應該理解為: ? ? if(表達式1) ? ? ? ? if(表達式2) ? ? ? ? ? ? 語句1; ? ? ? ? else ? ? ? ? ? ? 語句2; 還是應理解為: ? ? if(表達式1) ? ? ? ? if(表達式2) ? ? ? ? ? ? 語句1; ? ? else ? ? ? ? 語句2; 為了避免這種二義性,C語言規定,else 總是與它前面最近的if配對,因此對上述例子應按前一種情況理解。 【例5-6】 ~~~ main(){ int a,b; printf("please input A,B: "); scanf("%d%d",&a,&b); if(a!=b) if(a>b) printf("A>B\n"); else printf("A<B\n"); else printf("A=B\n"); } ~~~ 比較兩個數的大小關系。本例中用了if語句的嵌套結構。采用嵌套結構實質上是為了進行多分支選擇,實際上有三種選擇即A>B、A<B或A=B。這種問題用if-else-if語句也可以完成。而且程序更加清晰。因此,在一般情況下較少使用if語句的嵌套結構。以使程序更便于閱讀理解。 【例5-7】 ~~~ main(){ int a,b; printf("please input A,B: "); scanf("%d%d",&a,&b); if(a==b) printf("A=B\n"); else if(a>b) printf("A>B\n"); else printf("A<B\n"); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看