Uyenza njani iNgxelo eSisiseko ye-IF-ELSE ku-C

NguDan Gookin

Rhoqo kunokuba kungenjalo, iinkqubo zakho zeelwimi ze-C zisabela kwifayile ye-ukubaukuthelekisa ngeendlela ezimbini. Kuqala, benza ingxelo xa imeko iyinyani, kodwa kwenzeka ntoni xa imeko ibubuxoki? Umzekelo, jonga le khowudi ilandelayo:



#include int main() { float temp; printf('What is the temperature outside?'); scanf('%f',&temp); if(temp = 65) printf('My how pleasant!'); return(0); }

Gcina le khowudi, uyiqokelele, emva koko uyiqhube. Ixesha lokuqala, faka ixabiso elingaphantsi kwama-65. Okwesibini, faka ixabiso elingama-65 okanye ngaphezulu. Ikhowudi isebenza kuba eyokuqalaukubaingxelo ibamba onke amaxabiso angaphantsi kwama-65. Eyesibiniukubaingxelo ibamba onke amaxabiso angama-65 okanye ngaphezulu. Kodwa, unendlela engcono yokuluka esi sisombululo kulwimi lwe-C:enye into.



Izigqibo ezininzi kwiinkqubo zakho ze-C zi nokuba-okanye uhlobo. Inkqubo ithatha ikhondo elinye lesenzo ukuba imeko iyinyani, kwaye ukuba imeko ayiyonyani, inkqubo ithatha enye inyathelo. Ikhowudi engaphambi koku izama ukwenza oku ngendlela engacacanga ngababiniukubaiingxelo. Kodwa C unesisombululo esingcono: Sebenzisa ienye intoigama elingundoqo, njengoko kubonisiwe apha:

#include int main() { float temp; printf('What is the temperature outside?'); scanf('%f',&temp); if(temp <65) { printf('My but it's a bit chilly out!n'); } else { printf('My how pleasant!'); } return(0); }

Le khowudi ibandakanya onke ama-brace ukuze afundeke, kwaye eyesibiniukubaingxelo evela kumzekelo wokuqala wekhowudi ithathelwe indawoenye into. Kwenzeka ntoni xa iukubaimeko iyinyani, iingxelo zeukubabayabulawa. Kodwa xa imeko ingeyonyani, iingxelo zezikaenye intoziyenziwa endaweni yoko.



ukubakwaye ke nisebenze kunye; awunakho ukusebenzisaenye intongaphandle kweukubaingxelo phambi kwayo.

Imveliso ayihlukanga kwinkqubo engaphambili, kodwa ikhowudi yomthombo ifundeka ngakumbi kwaye isengqiqweni; ukuqonda imeko echaseneyo akuyomfuneko xa unokusebenzisa njeenye into.