Ingxelo ukuba i-C ++

Ngu-Stephen R. Davis

Xa udibanaukuba, C ++ kuqala iphumeza intetho evakalayo equlethwe kubazali. Kule khowudi ilandelayo, inkqubo ivavanya intetho enemiqathangomngaphezu kwen.



if (m > n) // if m is greater than n... { // ...then do this stuff }

Ukuba intetho yileyinyaniOko kukuthi, ukubamngokwenene mkhulu kunen, emva koko ulawulo ludlula ukuya kwingxelo yokuqala emva kwe {kwaye iqhubeke ukusuka apho. Ukuba intetho enengqiqo ayiyonyani, ulawulo ludlulela kwisitetimenti sokuqala emva kwe}.



Thelekisa abaqhubi kwi-C ++

Itafile ibonisa ii-opharetha ezahlukeneyo ezinokusetyenziselwa ukuthelekisa amaxabiso kwintetho esengqiqweni.

Abaqhubi beBhinary banefomathiUmqhubi we-expr1 expr2.



AbaSebenzi bokuThelekisa
Umsebenzi Intsingiselo
== ulingano; yinyani ukuba intetho ekhohlo kwi
‘==’ inexabiso elifanayo nembonakalo kwi
kunene
! = ukungalingani; ngokuchasene nokulingana
> ngaphezu kwe; Kuyinyani ukuba intetho yasekhohlo inkulu kune
enye ngasekunene
< ngaphantsi ko; Kuyinyani ukuba intetho yasekhohlo ingaphantsi kune
enye ngasekunene
> = inkulu kune okanye ilingana ne; Kuyinyani ukuba intetho yasekhohlo yile
mkhulu okanye ulingana nalowo ungasekunene
<= ngaphantsi okanye kulingana nokuthi; Kuyinyani ukuba intetho yasekhohlo incinci
okanye ilingana naleyo yasekunene

Musa ukudida umlingani wokulingana (==) kunye nomnikezeli womsebenzi (=). Le yimpazamo eqhelekileyo yabaqalayo.

ipilisi engqukuva eblowu c1

EzilandelayoIdilesi yesebeInkqubo ibonisa indlela abaqhubi abasetyenziswa ngayo:

iziphumo ebezingalindelekanga zemagniziyam oxide
// BranchDemo - demonstrate the if statement #include #include #include using namespace std; int main(int nNumberofArgs, char* pszArgs[]) { // enter operand1 and operand2 int nOperand1; int nOperand2; cout <> nOperand1; cout <> nOperand2; // now print the results if (nOperand1 > nOperand2) { cout << 'Argument 1 is greater than argument 2' << endl; } if (nOperand1 < nOperand2) { cout << 'Argument 1 is less than argument 2' << endl; } if (nOperand1 == nOperand2) { cout << 'Argument 1 is equal to argument 2' << endl; } // wait until user is ready before terminating program // to allow the user to see the program results cout << 'Press Enter to continue...' << endl; cin.ignore(10, 'n'); cin.get(); return 0; }

Ukuphunyezwa kwenkqubo kuqala ngeephambili ()njengesiqhelo. Inkqubo yokuqala ibhengeza ezimbiniintIzinto eziguquguqukayo ezinamagama ngobuqiliNOperand1kwayeNOperand2. Emva koko inyanzela umsebenzisi ukubaNgenisa impikiswano 1, efundeka kuyoNOperand1. Inkqubo iphindaphindweNOperand2.



Inkqubo emva koko yenza ulandelelwano lweentelekiso ezintathu. Kuqala kujonge ukuba ingabaNOperand1ingaphantsi kwe-NOperand2. Ukuba kunjalo, inkqubo ikhupha isazisoIngxabano 1 ingaphantsi kwempikiswano 2. UmzuzwanaukubaIsiteyitimenti sibonisa umyalezo ukuba ngaba ii-opaundi zimbini ziyalingana ngexabiso. Thelekisa okokugqibela kuyinyani ukubaNOperand1mkhulu kuneNOperand2.

Oku kulandelayo kubonisa isampulu yokuqhutywa kweIdilesi yesebeInkqubo:

Enter argument 1:  5  Enter argument 2:  10  Argument 1 is less than argument 2 Press Enter to continue . . .

Inani libonisa ukuhamba kolawulo ngokwemizobo yolu hlobo luqhubayo.

umfanekiso0.jpg

Indlela iIdilesi yesebeInkqubo ibhaliwe, zontathu uthelekiso lwenziwa ngalo lonke ixesha. Oku kuyinkcitho kancinci kuba iimeko ezintathu ziyafana. Umzekelo,nOperand1> nOperand2ayinakuba yinyani ukubaNOperand1sele ifunyenwe iyinyani.

I-phenylephrine hcl 10 mg

Yithi Hayi ku-brace

Ngokwenyani ii-brace ziyazikhethela. Ngaphandle kwe-brace, kuphela ibinzana lokuqala emva kweukubaingxelo ineemeko. Nangona kunjalo, kulula kakhulu ukwenza impazamo ngale ndlela, njengoko kubonisiwe kule snippet ilandelayo:

// Can't have a negative age. If age is less than zero... if (nAge <0) cout << 'Age can't be negative; using 0' << endl; nAge = 0; // program continues

Unokucinga ukuba ukubaUbudalaingaphantsi kwe-0, le snippet yenkqubo ikhupha umyalezo kwaye usete kwakhonaUbudalaukuya ku-zero. Ngapha koko, inkqubo ibekaUbudalaUkwenza zero nokuba kunjalo, nokuba ithini na ngexabiso layo lokuqala. I-snippet eyandulelayo ilingana noku kulandelayo:

// Can't have a negative age. If age is less than zero... if (nAge <0) { cout << 'Age can't be negative; using 0' << endl; } nAge = 0; // program continues

Kucacile kwizimvo kunye ne-indent yokuba umdwelisi nkqubo wayethetha oku kulandelayo:

// Can't have a negative age. If age is less than zero... if (nAge <0) { cout << 'Age can't be negative; using 0' << endl; nAge = 0; } // program continues

Umhlanganisi we-C ++ akanakubamba olu hlobo lwempazamo. Kukhuselekile nje ukuba uhlala unikezela nge-braces.

I-C ++ iphatha yonke indawo emhlophe ngokufanayo. Ityeshela ulungelelwaniso lweenkcazo kwiphepha.

Njalo sebenzisa ii-brace ukufaka iingxelo emva kwengxelo if, nokuba inye kuphela. Uya kuvelisa ezininzi iimpazamo ngaloo ndlela.