C語(yǔ)言菜鳥(niǎo)基礎(chǔ)教程之條件判斷
(一)if...else
先動(dòng)手編寫(xiě)一個(gè)程序
#include <stdio.h>
int main()
{
  int x = -1;
  if(x > 0)
  {
    printf("x is a positive number!\n");
  }
  else
  {
    printf("x is not a positive number!\n");
  }
          
  
  return 0;
}
運(yùn)行結(jié)果:
x is not a positive number!
程序分析:
定義一個(gè)整數(shù)x,并給他賦值。這個(gè)值要么大于0,要么不大于0(等于或小于0)。
若是大于0,則打印x is a positive number!
若不大于0,則打印x is not a positive number!
這里建議不要再使用在線(xiàn)編譯器,而是使用本機(jī)編譯器(蘋(píng)果電腦推薦Xcode,PC推薦dev C++)。在本機(jī)編譯器上設(shè)置斷點(diǎn)逐步執(zhí)行,會(huì)發(fā)現(xiàn)if中的printf語(yǔ)句和else中的printf語(yǔ)句只會(huì)執(zhí)行一個(gè)。這是因?yàn)閕f和else是互斥的關(guān)系,不可能都執(zhí)行。
(二)if...else if...else
稍微改動(dòng)程序
#include <stdio.h>
int main()
{
  int x = 0;
  if(x > 0)
  {
    printf("x is a positive number!\n");
  }
  else if(x == 0)
  {
    printf("x is zero!\n");
  }
  else
  {
    printf("x is a negative number!\n");
  }       
  
  return 0;
}
運(yùn)行結(jié)果:
x is zero!
程序分析:
假如條件不止兩種情況,則可用if...else if...else...的句式。
這個(gè)程序里的條件分成三種: 大于0、等于0或小于0。
大于0則打印x is a positive number!
等于0則打印x is zero!
小于0則打印x is a negative number!
注意,x == 0,這里的等號(hào)是兩個(gè),而不是一個(gè)。
C語(yǔ)言中,一個(gè)等號(hào)表示賦值,比如b = 100;
兩個(gè)等號(hào)表示判斷等號(hào)的左右側(cè)是否相等。
(三)多個(gè)else if的使用
#include <stdio.h>
int main()
{
  int x = 25;
  if(x < 0)
  {
    printf("x is less than 0\n");
  }
  if(x >= 0 && x <= 10)
  {
    printf("x belongs to 0~10\n");
  }
  else if(x >= 11 && x <= 20)
  {
    printf("x belongs to 11~20\n");
  }
  else if(x >= 21 && x <= 30)
  {
    printf("x belongs to 21~30\n");
  }
  else if(x >= 31 && x <= 40)
  {
    printf("x belongs to 31~40\n");
  }
  else
  {
    printf("x is greater than 40\n");
  }
  
  return 0;
}
運(yùn)行結(jié)果:
x belongs to 21~30
程序分析:
(1)
這里把x的值分為好幾個(gè)區(qū)間:(負(fù)無(wú)窮大, 0), [0, 10], [11, 20], [21, 30], [31, 40], (40, 正無(wú)窮大)
(負(fù)無(wú)窮大, 0)用if來(lái)判斷
[0, 10], [11, 20], [21, 30], [31, 40]用else if來(lái)判斷
(40, 正無(wú)窮大)用else來(lái)判斷
(2)
符號(hào)“&&”代表“并且”,表示“&&”左右兩側(cè)的條件都成立時(shí),判斷條件才成立。
上一篇:C語(yǔ)言菜鳥(niǎo)基礎(chǔ)教程之for循環(huán)
欄 目:C語(yǔ)言
本文標(biāo)題:C語(yǔ)言菜鳥(niǎo)基礎(chǔ)教程之條件判斷
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1133.html
您可能感興趣的文章
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用函數(shù)刪除字符
 - 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)式函數(shù)庫(kù)
 - 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
 - 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)數(shù)怎么表達(dá)
 - 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段函數(shù)
 - 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排序法函數(shù)
 - 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
 - 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段函數(shù)
 - 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎么打出三角函數(shù)的值
 - 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求階乘
 


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
 - 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
 - 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語(yǔ)言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
 - 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
 - 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
 - 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
 - 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
 - 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
 - 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
 - 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
 - 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
 - 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
 
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
 - 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
 - 01-10C#中split用法實(shí)例總結(jié)
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
 - 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 04-02jquery與jsp,用jquery
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
 


