Yayin cikin shirye -shiryen Yaya kuke amfani da madauki?

A cikin wannan post, zamuyi magana akan Yayin cikin shirye -shiryeWaɗannan umarni ne a cikin tsarin sarrafawa waɗanda ake maimaitawa a lokuta da yawa, daga cikin manyan ayyukansu shine hana wasu umarni masu alaƙa da gaskiya ko ƙarya, ƙari, an bayyana yadda ake amfani da madauki.

yayin-in-programming-1

Yayin cikin shirye -shirye

Menene tsawon lokacin don shirye -shiryen, ana amfani da shi don ba da jerin umarni a cikin tsarin sarrafawa wanda aka maimaita sau da yawa, kuma yana da aikin hana jerin umarni, a cikin yanayin kimantawa da haɗin haɗin da / ko yana da ma'ana ko ƙarya.

Wannan yana nufin cewa yana maimaitawa ne kawai lokacin da kimantawar koyarwar gaskiya ce.

Baya ga tsarin sarrafawa, kamar bayanin if ko canzawa a cikin shirye -shirye, akwai kuma tsarin maimaitawa.

A cikin tsarin shirye -shiryen maimaitawa, masu alaƙa da madauki na ɗan lokaci, yana maimaita toshe lambar idan dai yana ɗauke da ƙima na gaskiya, wanda za'a iya bayyana shi kamar haka:

  • Yayin (sharadi).
  • {.
  • umarni ;.
  • }.

Kamar yadda kuke gani akwai maimaitawa da madaukai, duk da haka, gaba ɗaya iri ɗaya ne, yana da mahimmanci a lura cewa madaukai sun ƙunshi:

  • Yanayin tantancewa gaskiya ne ko karya, kuma ana yin shi akan kowane maimaita madauki.
  • Ana yin bayanin da ke nuna layukan lambar idan yanayin gaskiya ne.

Daga cikin halayen irin wannan madauki shine cewa ana sarrafa yanayin kafin aiwatar da lambar, to, idan sakamakon karya ne, ba za a aiwatar da umarnin ba, yayin da akwai wani nau'in madauki wanda ke aiwatar da sau ɗaya kawai. .

Anan muna ba da shawarar labarin mai ban sha'awa mai alaƙa C harshe.

Bayan haka, yayin da aka ayyana madaukai, yana nufin tsarin juzu'i wanda ke ba da damar sake maimaita layi ɗaya ko daban -daban, ba tare da ƙimar farko ba kuma wani lokacin ba tare da sanin lokacin da zai dawo da ƙimar ƙarshe da ake tsammani ba.

Yayin da madaukai su ne waɗanda ba su ƙarƙashin ƙimar lambobi, akasin haka sun dogara ne da ƙimar Boolean, wanda ke nufin ƙimar gaskiya na yanayin gaskiya ko na ƙarya.

yayin-in-programming-2

Ta yaya Lokacin Loop ke aiki?

Don fahimtar yadda madauki yayin aiki, a cikin wannan ɓangaren za mu ambaci misalai waɗanda ke jagorantar mu don ganin yadda ake aiwatar da shi.

Misali, muna ɗauka cewa saboda wasu dalilai, ana tambayar mai amfani da adadin lambobi da ke faruwa a gare su, kuma suna shigar da lamba fiye da 100.

Kamar yadda kuke gani, ba za ku iya amfani da madauki ba, saboda ba ku da masaniya cewa mai amfani zai shigar da lamba sama da 100, abu ne da ba za a iya tantance shi ba, amma yayin madauki ya yarda ya yi wani aiki ba tare da iyaka ba har sai takamaiman yanayin an kashe, a wannan yanayin shine lambar da aka shigar wanda ya fi 100.

Don haka, idan mai amfani ya ci gaba da shigar da waɗannan lambobi: 1, 50, 99, 49, 21, 30, 100, shirin ba zai ƙare ba, duk saboda lambobin ba su fi 100 ba, amma, a cikin yanayin da ya shigar da lambar 300, shirin yana da sharaɗin cewa zai ƙare nan da nan.

Yayin Loop Syntax a C ++

Haɗin keɓewar ɗan lokaci yana da sauƙi kuma ana iya karantawa fiye da na madauki a C ++, saboda kawai yana buƙatar madaidaicin yanayin tsayawa.

Yawancin manyan harsunan hanyar rubuta madauki na ɗan lokaci iri ɗaya ne, don haka abin da yakamata ku yi shine ku tuna yanayin ƙarewa don madauki da za a shirya.

Bari mu gani a cikin misali mai zuwa yadda za'a sanya yanayin ƙarshe:

  • yayin (yanayin ƙarshe) // misali lamba == 100.
  • {.
  • ....
  • ....
  • ....
  • ....
  • }.

Za mu hango layi ta hanyar layin lambar da aka ambata, ta yadda zai taimaka mana fahimtar abin da ake nufi.

Layi 1: Yana da abun cikin sa mafi mahimmancin ɗan lokaci madauki.

Ma’anar kalma ce mai sauqi qwarai, za ka ga cewa akwai wani yanayi a cikin baka, misali: “==. >, <, >=, <=, !=” ko kuma watakila wasu, sharadi na musamman da aka bayyana, shi ne wanda zai ba da damar madauki ya ci gaba da cikawa har sai ya kai ga rashin aiwatar da irin wannan yanayin.

Don haka misali, kuna tabbatar da cewa wani adadi == 50, ana yin zagayowar ne kawai lokacin da kowane lamba yayi daidai da 50; lokacin da aka canza ƙimarsa zuwa kowane adadin, yayin madauki yana ƙare aikinsa, amma zai ci gaba da ɗayan ɓangaren aiwatar da shirin.

Ya kamata a yi la’akari da cewa a bayyane yake cewa yanayin da aka yi rijista koyaushe zai ɗauki ƙimar Boolean, wato, gaskiya ko ƙarya.

Layi 2: A cikin wannan layin an buɗe “{”, wanda ke nufin cewa a cikin wannan ɓangaren ana fara wani shingen umarni wanda zai cika da zarar zagayowar ta fara juyawa.

Koyaya, sanya wannan maɓallin ba tilas bane, amma, idan ba a sanya shi ba, za a kashe shi ne kawai yayin madauki wanda aka nuna a layin farko na farko, sannan sanarwar madauki, wanda ke nufin idan ba ku so Idan ana aiwatar da layuka daban -daban a cikin sake zagayowar, dole ne a sanya makullin.

Layi na 3 zuwa 7: Waɗannan layukan sune inda za a sanya duk ayyukan da kuke son aiwatarwa akai -akai a cikin tsarin sake zagayowar. Wannan shinge na iya ƙunsar adadin layukan da ake buƙata.

Layin 8: Yana da layi na ƙarshe kuma dole ne a yi amfani da takalmin gyaran kafa "}", kamar yadda aka kafa ta hanyar toshe madauki kuma aikin zai ƙare, duk da haka, sauran algorithm za su ci gaba.

Yanzu za mu nuna wasu misalai waɗanda za su jagoranci masu amfani don fahimta ta hanya mai sauƙi da sauƙi amfani da yayin madaukai a C ++, za mu fara da:

Misali 1: Nemi lambobi akan allon har sai mutum ya zarce 100

Za mu ci gaba da misalin da aka nuna a sama, inda za mu ci gaba zuwa shirin muna neman mai amfani ya shigar da lambobi da yawa, komai abin da suke, kuma hakan zai daina yi, lokacin da mai amfani ya shiga lamba fiye da 100, shine misali mai aiki da sauƙi, don tabbatar da cewa an fahimci abin da aka ambata.

Misali 1:

Za mu ba da mafita, dole ne a tuna cewa dole ne a aiwatar da yanayin don sake zagayowar yana buƙatar lambar, sake zagayowar zai tsaya ne kawai lokacin da lambar da ta shiga ta fi 100, sannan sharadin na ci gaba aiki shi ne cewa adadin bai kai 100 ba, don a tsayar da lambar dole ne ya fi 100, kuma a ci gaba da aiwatar da lambar dole ne ƙasa ko daidai da 100.

Ana iya ganin cewa yana nunawa ta hanyoyi masu zuwa:

  • lambar int ;.
  • cin >> number;.
  • yayin da (lamba <= 100).
  • {.
  • cout << "Shigar da lamba";.
  • cin >> number;.
  • }.

Cikakken lambar aiki yana da sauƙin sarrafawa ta mai amfani, a ƙasa akwai aikin yadda ake amfani dashi:

  • # hada da "iostream".
  • yin amfani da sunan sarari std;.
  • int main ().
  • {.
  •  lambar int ;.
  •  cout << "Shigar da lamba";.
  •  cin >> number;.
  •  yayin da (lamba <= 100).
  •  {.
  •  cout << "Shigar da lamba";.
  •  cin >> number;.
  •  }.
  •  tsarin ("DAKATARWA");.
  •  dawo 0;.
  • }.

Yayin da madaukai a cikin shirye -shirye suna da fa'ida sosai, amma dole ne a tuna cewa daga yanayin inganci da inganci kamar sauran don madaukai, ana ba da shawarar cewa ba za a yi amfani da su ba, duk lokacin da madauki ko wani nau'in madauki ke tafiya sake zagayowar, yana da kyau a tuntuɓi kafin amfani da shi ya zama tilas, ko kuma idan akwai wata hanya mafi dacewa don amfani da ita.


Abubuwan da ke cikin labarin suna bin ka'idodinmu na ka'idojin edita. Don yin rahoton kuskure danna a nan.

Kasance na farko don yin sharhi

Bar tsokaci

Your email address ba za a buga. Bukata filayen suna alama da *

*

*

  1. Hakkin bayanan: Actualidad Blog
  2. Dalilin bayanan: Gudanar da SPAM, gudanar da sharhi.
  3. Halacci: Yarda da yarda
  4. Sadarwar bayanan: Ba za a sanar da wasu bayanan ga wasu kamfanoni ba sai ta hanyar wajibcin doka.
  5. Ajiye bayanai: Bayanin yanar gizo wanda Occentus Networks (EU) suka dauki nauyi
  6. Hakkoki: A kowane lokaci zaka iyakance, dawo da share bayanan ka.