![C++程序控制結(jié)構(gòu)_第1頁](http://file4.renrendoc.com/view/a121d95ff154df58afbbbb9369eb530b/a121d95ff154df58afbbbb9369eb530b1.gif)
![C++程序控制結(jié)構(gòu)_第2頁](http://file4.renrendoc.com/view/a121d95ff154df58afbbbb9369eb530b/a121d95ff154df58afbbbb9369eb530b2.gif)
![C++程序控制結(jié)構(gòu)_第3頁](http://file4.renrendoc.com/view/a121d95ff154df58afbbbb9369eb530b/a121d95ff154df58afbbbb9369eb530b3.gif)
![C++程序控制結(jié)構(gòu)_第4頁](http://file4.renrendoc.com/view/a121d95ff154df58afbbbb9369eb530b/a121d95ff154df58afbbbb9369eb530b4.gif)
![C++程序控制結(jié)構(gòu)_第5頁](http://file4.renrendoc.com/view/a121d95ff154df58afbbbb9369eb530b/a121d95ff154df58afbbbb9369eb530b5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第2章程序控制結(jié)構(gòu)
2.1選擇控制
2.2循環(huán)控制
2.3判斷表達(dá)式的使用
2.4轉(zhuǎn)向語句
小結(jié)語句是程序的基本語法成分。程序設(shè)計語言的語句按功能可以分成三類:
聲明語句指示編譯器分配內(nèi)存,或者提供程序連接信息
操作語句描述對數(shù)據(jù)的處理
控制語句用于控制程序的執(zhí)行流程。所有程序都只能包含三種控制結(jié)構(gòu): 順序結(jié)構(gòu)、選擇結(jié)構(gòu)和循環(huán)結(jié)構(gòu)第2章程序控制結(jié)構(gòu)對給定的條件進(jìn)行判斷,并根據(jù)判斷的結(jié)果選擇不同的操作
2.1選擇控制條件運算表達(dá)式
根據(jù)判斷條件,決定表達(dá)式的值 不改變程序語句執(zhí)行流程
比較條件運算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í)行語句如果又是另一個if語句,稱為嵌套if語句
if與else的配對關(guān)系:C++規(guī)定,else總是與它接近的if配對使用復(fù)合語句,可以改變條件語句的執(zhí)行流程2.1.1if語句2.1.1if語句例2-1
編寫程序計算貨物運費。設(shè)貨物運費每噸單價p(元)與運輸距離s(公里)之間有如下關(guān)系:p=輸入要托運的貨物重量為w噸,托運距離s公里,計算總運費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計算貨物運費#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;}想一想:幾個else分支語句的次序可以改變嗎?2.1.1if語句//例2-1計算貨物運費#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
輸入三個整數(shù),按從小到大順序輸出。解法一
數(shù)據(jù)排序。先找出最小值,放在a中,然后找次小值,放在b中:
1.對a、b進(jìn)行比較,把小值放于a;
ifa>bab
//a<b2.
對a、c進(jìn)行比較,把小值放于a;
ifa>cac
//a<c,a<b3.對
b、c進(jìn)行比較,把小值放于b;
ifb>cbc
//a<b&&b<c4.輸出a,b,c的值。2.1.1if語句2.1.1if語句例2-2
輸入三個整數(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
輸入三個整數(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
輸入三個整數(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注意括號的作用2.if語句的嵌套2.1.1if語句3.應(yīng)用舉例(1)
把輸入字符轉(zhuǎn)換為小寫字母。對輸入字符進(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)換為小寫字母。對輸入字符進(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;}計算ASCII碼偏移值(1)
把輸入字符轉(zhuǎn)換為小寫字母。對輸入字符進(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)換為小寫字母。對輸入字符進(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)換為小寫字母。對輸入字符進(jìn)行判斷,如果是大寫字母,則轉(zhuǎn)換為小寫字母;否則,不轉(zhuǎn)換。2.1.1if語句3.應(yīng)用舉例
(2)
求一元二次方程ax2+bx+c=0的根。求根公式:①當(dāng)a=0時,方程不是二次方程②當(dāng)b2-4ac=0時,有兩個相同的實根:③當(dāng)b2-4ac>0時,有兩個不同的實根:④當(dāng)b2-4ac<0時,有兩個共軛復(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浮點數(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; } }}有兩個相同的實根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; } }}有兩個共軛復(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ù)一個整型表達(dá)式的值決定程序分支2.1.2switch語句一般形式:
switch(表達(dá)式){case常量表達(dá)式1:語句1
case常量表達(dá)式2:語句2
…
case常量表達(dá)式n:語句ndefault:語句n+1}注:
表達(dá)式類型為非浮點型
各常量表達(dá)式類型要與之匹配各常量表達(dá)式要求各不相等default子句可選。缺省時,沒有匹配值switch語句為空語句標(biāo)號2.1.2switch語句根據(jù)一個整型表達(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ǐ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";}}觀察不同輸入時的輸出結(jié)果2.1.2switch語句2.1.2switch語句例2-5根據(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ǐ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ǐ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ǐ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ǐ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ǐ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ǐ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ǐ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ǐ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ǐ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ǐ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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權(quán)】 ISO 23649:2025 EN Chemicals for the leather tanning industry - Determination of cyclosiloxanes
- 【正版授權(quán)】 ISO 3518:2025 EN Essential oil of sandalwood (Santalum album L.)
- 【正版授權(quán)】 ISO 18162:2024 EN Biotechnology - Biobanking - Requirements for human neural stem cells derived from pluripotent stem cells
- 2025年度標(biāo)準(zhǔn)托盤租賃及全程跟蹤服務(wù)協(xié)議
- 二手機械二手交易與環(huán)保處理服務(wù)合同(2025版)
- 2025年度二手房購房貸款保險合同范本
- 2025年辦公設(shè)備升級改造項目合作協(xié)議
- 生產(chǎn)計劃的周期性評審與調(diào)整
- 農(nóng)業(yè)生產(chǎn)的安全防護(hù)措施計劃
- 如何制定子女的教育基金計劃
- 建筑基坑工程監(jiān)測技術(shù)標(biāo)準(zhǔn)
- 2024年廣東汕尾市“奔向海陸豐”事業(yè)單位(綜合崗類)招聘工作人員176人歷年高頻500題難、易錯點模擬試題附帶答案詳解
- 【2024高考萬能答題模版】數(shù)學(xué)答題模板1
- DG-TJ 08-2242-2023 民用建筑外窗應(yīng)用技術(shù)標(biāo)準(zhǔn)
- 2024-2025上期學(xué)校心理健康教育工作計劃(附每周工作安排)
- 【中考真題】2024年河南省普通高中招生考試歷史試卷(含答案)
- YYT 0653-2017 血液分析儀行業(yè)標(biāo)準(zhǔn)
- JT-T-445-2021汽車底盤測功機
- 體育科學(xué):田徑考試考試題(三)
- 高考英語經(jīng)常用的七百個詞匯
- 不定代詞用法總結(jié)及配套練習(xí)題
評論
0/150
提交評論