C++程序控制結(jié)構(gòu)_第1頁
C++程序控制結(jié)構(gòu)_第2頁
C++程序控制結(jié)構(gòu)_第3頁
C++程序控制結(jié)構(gòu)_第4頁
C++程序控制結(jié)構(gòu)_第5頁
已閱讀5頁,還剩253頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第2章程序控制結(jié)構(gòu)

2.1選擇控制

2.2循環(huán)控制

2.3判斷表達(dá)式的使用

2.4轉(zhuǎn)向語句

小結(jié)語句是程序的基本語法成分。程序設(shè)計(jì)語言的語句按功能可以分成三類:

聲明語句指示編譯器分配內(nèi)存,或者提供程序連接信息

操作語句描述對(duì)數(shù)據(jù)的處理

控制語句用于控制程序的執(zhí)行流程。所有程序都只能包含三種控制結(jié)構(gòu): 順序結(jié)構(gòu)、選擇結(jié)構(gòu)和循環(huán)結(jié)構(gòu)第2章程序控制結(jié)構(gòu)對(duì)給定的條件進(jìn)行判斷,并根據(jù)判斷的結(jié)果選擇不同的操作

2.1選擇控制條件運(yùn)算表達(dá)式

根據(jù)判斷條件,決定表達(dá)式的值 不改變程序語句執(zhí)行流程

比較條件運(yùn)算1.if語句的形式和執(zhí)行流程if(表達(dá)式)語句;2.1.1if語句語句形式(1)執(zhí)行流程false(0)true(非0)

表達(dá)式

語句2.1.1if語句false(0)true(非0)

表達(dá)式

語句1.if語句的形式和執(zhí)行流程if(表達(dá)式)語句;2.1.1if語句語句形式(1)執(zhí)行流程

表達(dá)式

語句true(非0)2.1.1if語句false(0)true(非0)

表達(dá)式

語句1.if語句的形式和執(zhí)行流程if(表達(dá)式)語句;2.1.1if語句語句形式(1)執(zhí)行流程

表達(dá)式

false(0)2.1.1if語句2.1.1if語句35abmax例:

:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if語句2.1.1if語句35abmax3例:

:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if語句2.1.1if語句35abmax3例:

:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if語句2.1.1if語句35abmax3例:

:max=a;if(b>a)

max=b;cout<<"max="<<max<<endl;:2.1.1if語句2.1.1if語句35abmax5例:

:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if語句2.1.1if語句35abmax5

max=5輸出例:

:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if語句2.1.1if語句73abmax例:

:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if語句2.1.1if語句73abmax7例:

:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if語句2.1.1if語句73abmax7例:

:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if語句2.1.1if語句73abmax7

max=7輸出例:

:max=a;if(b>a)max=b;cout<<"max="<<max<<endl;:2.1.1if語句2.1.1if語句1.if語句的形式和執(zhí)行流程語句形式(2)if(表達(dá)式)語句1

;

else語句2

;執(zhí)行流程false(0)true(非0)

表達(dá)式

語句1語句22.1.1if語句2.1.1if語句1.if語句的形式和執(zhí)行流程語句形式(2)if(表達(dá)式)語句1

;

else語句2

;執(zhí)行流程false(0)true(非0)

表達(dá)式

語句1語句2true(非0)

表達(dá)式

語句12.1.1if語句2.1.1if語句1.if語句的形式和執(zhí)行流程語句形式(2)if(表達(dá)式)語句1

;

else語句2

;執(zhí)行流程false(0)true(非0)

表達(dá)式

語句1語句2false(0)

表達(dá)式

語句22.1.1if語句2.1.1if語句例:

:if(b>a)max=b;elsemax=a;cout<<"max="<<max<<endl;:35abmax2.1.1if語句2.1.1if語句35abmax例:

:if(b>a)

max=b;elsemax=a;cout<<"max="<<max<<endl;:2.1.1if語句2.1.1if語句例:

:if(b>a)

max=b;

elsemax=a;cout<<"max="<<max<<endl;:35abmax52.1.1if語句2.1.1if語句例:

:if(b>a)max=b;elsemax=a;cout<<"max="<<max<<endl;:35abmax5

max=5輸出2.1.1if語句2.1.1if語句例:

:if(b>a)

max=b;elsemax=a;cout<<"max="<<max<<endl;:73abmax2.1.1if語句2.1.1if語句例:

:if(b>a)max=b;

elsemax=a;cout<<"max="<<max<<endl;:73abmax72.1.1if語句2.1.1if語句例:

:if(b>a)max=b;elsemax=a;cout<<"max="<<max<<endl;:73abmax7

max=7輸出2.1.1if語句2.1.1if語句例:

:if(b>a)max=b;elsemax=a;cout<<"max="<<max<<endl;:73abmax7

max=7輸出試一試用條件表達(dá)式修改該程序2.1.1if語句2.1.1if語句2.if語句的嵌套

if語句中的執(zhí)行語句如果又是另一個(gè)if語句,稱為嵌套if語句

if與else的配對(duì)關(guān)系:C++規(guī)定,else總是與它接近的if配對(duì)使用復(fù)合語句,可以改變條件語句的執(zhí)行流程2.1.1if語句2.1.1if語句例2-1

編寫程序計(jì)算貨物運(yùn)費(fèi)。設(shè)貨物運(yùn)費(fèi)每噸單價(jià)p(元)與運(yùn)輸距離s(公里)之間有如下關(guān)系:p=輸入要托運(yùn)的貨物重量為w噸,托運(yùn)距離s公里,計(jì)算總運(yùn)費(fèi)t:

t=p*w*s2.1.1if語句2.1.1if語句控制流圖truefalsetruetruetruefalsefalsefalseS<100?S<200?S<300?S<400?P=30P=20P=22.5P=25P=27.5t=p*w*s2.1.1if語句2.1.1if語句truefalsetruetruetruefalsefalsefalseS<100?S<200?S<300?S<400?P=30P=20P=22.5P=25P=27.5t=p*w*s//例2-1計(jì)算貨物運(yùn)費(fèi)#include<iostream>#include<iomanip>usingnamespacestd;intmain(){doublet,p,w,s;

cout<<"Pleaseinputweight(ton):";

cin>>w;

cout<<"Pleaseinputdistsnce(kilometre):";

cin>>s;if(s<100)p=30;elseif(s<200)p=27.5; elseif(s<300)p=25; elseif(s<400)p=22.5; elsep=20;t=p*w*s;

cout<<"Thecostis:"<<setprecision(2)<<t<<'$'<<endl;}想一想:幾個(gè)else分支語句的次序可以改變嗎?2.1.1if語句//例2-1計(jì)算貨物運(yùn)費(fèi)#include<iostream>#include<iomanip>usingnamespacestd;intmain(){doublet,p,w,s;

cout<<"Pleaseinputweight(ton):";

cin>>w;

cout<<"Pleaseinputdistsnce(kilometre):";

cin>>s;if(s<100)p=30;elseif(s<200)p=27.5; elseif(s<300)p=25; elseif(s<400)p=22.5; elsep=20;t=p*w*s;

cout<<"Thecostis:"<<setprecision(2)<<t<<'$'<<endl;}truefalsetruetruetruefalsefalsefalseS<100?S<200?S<300?S<400?P=30P=20P=22.5P=25P=27.5t=p*w*s2.1.1if語句

例如:

cin>>s;if(s<300)p=25;elseif(s<100)p=30;elseif(s<400)p=22.5;elseif(s<200)p=27.5;elsep=20;t=p*w*s;

若輸入s:150

輸出t值等于多少?為什么?2.1.1if語句2.1.1if語句例2-2

輸入三個(gè)整數(shù),按從小到大順序輸出。解法一

數(shù)據(jù)排序。先找出最小值,放在a中,然后找次小值,放在b中:

1.對(duì)a、b進(jìn)行比較,把小值放于a;

ifa>bab

//a<b2.

對(duì)a、c進(jìn)行比較,把小值放于a;

ifa>cac

//a<c,a<b3.對(duì)

b、c進(jìn)行比較,把小值放于b;

ifb>cbc

//a<b&&b<c4.輸出a,b,c的值。2.1.1if語句2.1.1if語句例2-2

輸入三個(gè)整數(shù),按從小到大順序輸出。a

7

5

2bca>b:ab

a

5

7

2bca

2

7

5bc

2

7a>c:ac

2b>c:bc

a

2

5

7bca<ba<b,a<ca<b&&b<c2.1.1if語句2.1.1if語句例2-2

輸入三個(gè)整數(shù),按從小到大順序輸出。#include<iostream>usingnamespacestd;intmain(){inta,b,c,t;

cout<<"Pleaseinputthreeintegernumbers:";

cin>>a>>b>>c;if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;}if(b>c){t=b;b=c;c=t;}

cout<<a<<""<<b<<""<<c<<endl;}注意語句塊結(jié)構(gòu)2.1.1if語句2.1.1if語句例2-2

輸入三個(gè)整數(shù),按從小到大順序輸出。#include<iostream>usingnamespacestd;intmain(){inta,b,c,t;

cout<<"Pleaseinputthreeintegernumbers:";

cin>>a>>b>>c;if(a>b)

{t=a;a=b;b=t;}if(a>c)

{t=a;a=c;c=t;}if(b>c)

{t=b;b=c;c=t;}

cout<<a<<""<<b<<""<<c<<endl;}注意語句塊結(jié)構(gòu)2.1.1if語句2.1.1if語句 if(E1) if(E2)S1

elseS2if(E1)

{if(E2)S1

}elseS2分析S1和S2的執(zhí)行條件:E1&&E2

執(zhí)行S1E1&&!E2

執(zhí)行S2E1&&E2

執(zhí)行S1

!E1

執(zhí)行S2注意括號(hào)的作用2.if語句的嵌套2.1.1if語句3.應(yīng)用舉例(1)

把輸入字符轉(zhuǎn)換為小寫字母。對(duì)輸入字符進(jìn)行判斷,如果是大寫字母,則轉(zhuǎn)換為小寫字母;否則,不轉(zhuǎn)換。//例2-3#include<iostream>usingnamespacestd;intmain(){charch;

cout<<"ch=";

cin>>ch;if(ch>='A'&&ch<='Z')ch+=32;

cout<<ch<<endl;}2.1.1if語句3.應(yīng)用舉例//例2-3

#include<iostream>usingnamespacestd;intmain(){charch;

cout<<"ch=";

cin>>ch;if(ch>='A'&&ch<='Z')

ch+=32;

cout<<ch<<endl;}輸入大寫字母(1)

把輸入字符轉(zhuǎn)換為小寫字母。對(duì)輸入字符進(jìn)行判斷,如果是大寫字母,則轉(zhuǎn)換為小寫字母;否則,不轉(zhuǎn)換。2.1.1if語句3.應(yīng)用舉例//例2-3#include<iostream>usingnamespacestd;intmain(){charch;

cout<<"ch=";

cin>>ch;if(ch>='A'&&ch<='Z')ch+=32;

cout<<ch<<endl;}計(jì)算ASCII碼偏移值(1)

把輸入字符轉(zhuǎn)換為小寫字母。對(duì)輸入字符進(jìn)行判斷,如果是大寫字母,則轉(zhuǎn)換為小寫字母;否則,不轉(zhuǎn)換。2.1.1if語句3.應(yīng)用舉例//例2-3#include<iostream>usingnamespacestd;intmain(){charch;

cout<<"ch=";

cin>>ch;if(ch>='A'&&ch<='Z')ch+=32;

cout<<ch<<endl;}(1)

把輸入字符轉(zhuǎn)換為小寫字母。對(duì)輸入字符進(jìn)行判斷,如果是大寫字母,則轉(zhuǎn)換為小寫字母;否則,不轉(zhuǎn)換。2.1.1if語句3.應(yīng)用舉例//例2-3#include<iostream>usingnamespacestd;intmain(){charch;

cout<<"ch=";

cin>>ch;

if(ch>='A'&&ch<='Z')ch+=32;

cout<<ch<<endl;}改寫為條件表達(dá)式ch=(ch>='A'&&ch<='Z')?ch+32:ch;(1)

把輸入字符轉(zhuǎn)換為小寫字母。對(duì)輸入字符進(jìn)行判斷,如果是大寫字母,則轉(zhuǎn)換為小寫字母;否則,不轉(zhuǎn)換。2.1.1if語句3.應(yīng)用舉例

(2)

求一元二次方程ax2+bx+c=0的根。求根公式:①當(dāng)a=0時(shí),方程不是二次方程②當(dāng)b2-4ac=0時(shí),有兩個(gè)相同的實(shí)根:③當(dāng)b2-4ac>0時(shí),有兩個(gè)不同的實(shí)根:④當(dāng)b2-4ac<0時(shí),有兩個(gè)共軛復(fù)根:2.1.1if語句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;

cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)

cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)

cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);

cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);

cout<<"Ithastwocomplexroots:"<<endl;

cout<<rp<<"+"<<ip<<"i"<<endl;

cout<<rp<<"-"<<ip<<"i"<<endl; } }}2.1.1if語句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;

cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)

cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)

cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);

cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);

cout<<"Ithastwocomplexroots:"<<endl;

cout<<rp<<"+"<<ip<<"i"<<endl;

cout<<rp<<"-"<<ip<<"i"<<endl; } }}數(shù)據(jù)說明2.1.1if語句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;

cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)

cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)

cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);

cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);

cout<<"Ithastwocomplexroots:"<<endl;

cout<<rp<<"+"<<ip<<"i"<<endl;

cout<<rp<<"-"<<ip<<"i"<<endl; } }}提示并輸入系數(shù)2.1.1if語句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;

cout<<"a,b,c=";cin>>a>>b>>c;

if(fabs(a)<=1e-8)

cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)

cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);

cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);

cout<<"Ithastwocomplexroots:"<<endl;

cout<<rp<<"+"<<ip<<"i"<<endl;

cout<<rp<<"-"<<ip<<"i"<<endl; } }}不是二次方程2.1.1if語句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;

cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)

cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)

cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);

cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);

cout<<"Ithastwocomplexroots:"<<endl;

cout<<rp<<"+"<<ip<<"i"<<endl;

cout<<rp<<"-"<<ip<<"i"<<endl; } }}a等于0浮點(diǎn)數(shù)的誤差判斷2.1.1if語句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;

cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)

cout<<"Itisnotquadratic."<<endl;

else

{d=b*b-4*a*c;if(fabs(d)<=1e-8)

cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);

cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);

cout<<"Ithastwocomplexroots:"<<endl;

cout<<rp<<"+"<<ip<<"i"<<endl;

cout<<rp<<"-"<<ip<<"i"<<endl; } }}有根2.1.1if語句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;

cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)

cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)

cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);

cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);

cout<<"Ithastwocomplexroots:"<<endl;

cout<<rp<<"+"<<ip<<"i"<<endl;

cout<<rp<<"-"<<ip<<"i"<<endl; } }}求判別式2.1.1if語句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;

cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)

cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;

if(fabs(d)<=1e-8)

cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);

cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);

cout<<"Ithastwocomplexroots:"<<endl;

cout<<rp<<"+"<<ip<<"i"<<endl;

cout<<rp<<"-"<<ip<<"i"<<endl; } }}有兩個(gè)相同的實(shí)根2.1.1if語句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;

cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)

cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)

cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);

cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);

cout<<"Ithastwocomplexroots:"<<endl;

cout<<rp<<"+"<<ip<<"i"<<endl;

cout<<rp<<"-"<<ip<<"i"<<endl; } }}有兩個(gè)不相同的實(shí)根2.1.1if語句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;

cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)

cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)

cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);

cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);

cout<<"Ithastwocomplexroots:"<<endl;

cout<<rp<<"+"<<ip<<"i"<<endl;

cout<<rp<<"-"<<ip<<"i"<<endl; } }}有兩個(gè)共軛復(fù)根2.1.1if語句#include<iostream> //例2-4#include<cmath>usingnamespacestd;intmain(){doublea,b,c,d,x1,x2,rp,ip;

cout<<"a,b,c=";cin>>a>>b>>c;if(fabs(a)<=1e-8)

cout<<"Itisnotquadratic."<<endl;else{d=b*b-4*a*c;if(fabs(d)<=1e-8)

cout<<"Ithastwoequalrealroots:"<<-b/(2*a)<<endl;elseif(d>1e-8) {x1=(-b+sqrt(d))/(2*a);x2=(-b-sqrt(d))/(2*a);

cout<<"Ithastwodistinctrealroots:"<<x1<<"and"<<x2<<endl; } else{rp=-b/(2*a);ip=sqrt(-d)/(2*a);

cout<<"Ithastwocomplexroots:"<<endl;

cout<<rp<<"+"<<ip<<"i"<<endl;

cout<<rp<<"-"<<ip<<"i"<<endl; } }}a,b,c=021Itisnotquadratic.a,b,c=121Ithastwoequalrealroots:-1a,b,c=151Ithastwodistinctrealroots:-0.208712and–4.791288a,b,c=234Ithastwocomplexroots:-0.75+1.198958i-0.75-1.198958i2.1.1if語句根據(jù)一個(gè)整型表達(dá)式的值決定程序分支2.1.2switch語句一般形式:

switch(表達(dá)式){case常量表達(dá)式1:語句1

case常量表達(dá)式2:語句2

case常量表達(dá)式n:語句ndefault:語句n+1}注:

表達(dá)式類型為非浮點(diǎn)型

各常量表達(dá)式類型要與之匹配各常量表達(dá)式要求各不相等default子句可選。缺省時(shí),沒有匹配值switch語句為空語句標(biāo)號(hào)2.1.2switch語句根據(jù)一個(gè)整型表達(dá)式的值決定程序分支2.1.2switch語句表達(dá)式語句1語句2語句3語句n語句n+1=常量1=常量2=常量3=常量n=常量n+1執(zhí)行流程2.1.2switch語句2.1.2switch語句例2-5根據(jù)考試成績(jī)的等級(jí)打印出百分制分?jǐn)?shù)段。#include<iostream>usingnamespacestd;intmain(){chargrade;

cout<<"Inputgradeofscore(a_d):"<<endl;

cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}觀察不同輸入時(shí)的輸出結(jié)果2.1.2switch語句2.1.2switch語句例2-5根據(jù)考試成績(jī)的等級(jí)打印出百分制分?jǐn)?shù)段。#include<iostream>usingnamespacestd;intmain(){chargrade;

cout<<"Inputgradeofscore(a_d):"<<endl;

cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}輸入a2.1.2switch語句2.1.2switch語句例2-5根據(jù)考試成績(jī)的等級(jí)打印出百分制分?jǐn)?shù)段。#include<iostream>usingnamespacestd;intmain(){chargrade;

cout<<"Inputgradeofscore(a_d):"<<endl;

cin>>grade;

switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}輸入a2.1.2switch語句2.1.2switch語句例2-5根據(jù)考試成績(jī)的等級(jí)打印出百分制分?jǐn)?shù)段。#include<iostream>usingnamespacestd;intmain(){chargrade;

cout<<"Inputgradeofscore(a_d):"<<endl;

cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}輸入a輸出85__10070__8460__6960error2.1.2switch語句2.1.2switch語句例2-5根據(jù)考試成績(jī)的等級(jí)打印出百分制分?jǐn)?shù)段。#include<iostream>usingnamespacestd;intmain(){chargrade;

cout<<"Inputgradeofscore(a_d):"<<endl;

cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}輸入a輸出85__10070__8460__6960error2.1.2switch語句2.1.2switch語句例2-5根據(jù)考試成績(jī)的等級(jí)打印出百分制分?jǐn)?shù)段。#include<iostream>usingnamespacestd;intmain(){chargrade;

cout<<"Inputgradeofscore(a_d):"<<endl;

cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}輸入a輸出85__10070__8460__6960error2.1.2switch語句2.1.2switch語句例2-5根據(jù)考試成績(jī)的等級(jí)打印出百分制分?jǐn)?shù)段。#include<iostream>usingnamespacestd;intmain(){chargrade;

cout<<"Inputgradeofscore(a_d):"<<endl;

cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}輸入a輸出85__10070__8460__6960error2.1.2switch語句2.1.2switch語句例2-5根據(jù)考試成績(jī)的等級(jí)打印出百分制分?jǐn)?shù)段。#include<iostream>usingnamespacestd;intmain(){chargrade;

cout<<"Inputgradeofscore(a_d):"<<endl;

cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}輸入a輸出85__10070__8460__6960error2.1.2switch語句2.1.2switch語句例2-5根據(jù)考試成績(jī)的等級(jí)打印出百分制分?jǐn)?shù)段。#include<iostream>usingnamespacestd;intmain(){chargrade;

cout<<"Inputgradeofscore(a_d):"<<endl;

cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}輸入a輸出85__10070__8460__6960error2.1.2switch語句2.1.2switch語句例2-5根據(jù)考試成績(jī)的等級(jí)打印出百分制分?jǐn)?shù)段。#include<iostream>usingnamespacestd;intmain(){chargrade;

cout<<"Inputgradeofscore(a_d):"<<endl;

cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}輸入a輸出85__10070__8460__6960error2.1.2switch語句2.1.2switch語句例2-5根據(jù)考試成績(jī)的等級(jí)打印出百分制分?jǐn)?shù)段。#include<iostream>usingnamespacestd;intmain(){chargrade;

cout<<"Inputgradeofscore(a_d):"<<endl;

cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}輸入a輸出85__10070__8460__6960error2.1.2switch語句2.1.2switch語句例2-5根據(jù)考試成績(jī)的等級(jí)打印出百分制分?jǐn)?shù)段。#include<iostream>usingnamespacestd;intmain(){chargrade;

cout<<"Inputgradeofscore(a_d):"<<endl;

cin>>grade;switch(grade){case'a':cout<<"85__100\n";case'b':cout<<"70__84\n";case'c':cout<<"60__69\n";case'd':cout<<"<60\n";default:cout<<"error\n";}}輸入a輸出85__10070__8460__6960error2.1.2switch語句2.1.2

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論