Iterative statements: while, do-while, for loop

Loops કામ ફરી ફરી કરે છે: while ચલાવતાં પહેલાં ચકાસે (કદાચ શૂન્ય વાર), do-while પછી ચકાસે (હંમેશાં એક વાર ચાલે), અને for counter, condition અને update ને એક જ line માં સમેટે, ગણી-ગણી પુનરાવૃત્તિ માટે.

11 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

60 students, program એક વાર ચાલ્યો

Marks program એક student ને સુંદર રીતે સંભાળે છે. Class માં 60 છે.

આજે તમારા વિકલ્પો: program 60 વાર ચલાવો (પટાવાળાની રીત), code ને 60 વાર copy-paste કરો (તબાહીની રીત), કે C ને કહો: "આ block ને ત્યાં સુધી ફરી ફરી કરો જ્યાં સુધી હું અટકવાનું ન કહું".

એ ત્રીજો વિકલ્પ છે iteration, તમારા 3 structured-programming આકારોમાંનો છેલ્લો, અને એ 3 flavours માં આવે છે જેમના ફરક શુદ્ધ exam નું સોનું છે.

Theory

ખરી જિંદગીમાં પુનરાવર્તનની 3 ઢબ

while એ airport security છે: guard તમને અંદર ઘૂસતાં PEHLA ચકાસે છે, અને પહેલી ચકાસણીમાં fail થયા તો તમે અંદર ઘૂસો જ નહીં (શૂન્ય entries શક્ય). do-while એ રાંધતાં દાળ ચાખવા જેવું છે: તમે PEHLA ચાખો છો, પછી નક્કી કરો છો કે ફરી adjust કરવું કે નહીં, પહેલું ચાખવું હંમેશાં થાય છે. for એ attendance લેતી teacher છે: roll 1 થી 60, count એને શરૂ કરતાં પહેલાં ખબર છે અને એ એમાંથી યંત્રવત્ પસાર થાય છે.

Theory

ત્રણેય loops, ચોખ્ખાં

  • while (condition) { }: પહેલા ચકાસો, સાચું હોય તો ચલાવો, ફરી કરો. તરત fail? Body શૂન્ય વાર ચાલે (entry-controlled).
  • do { } while (condition);: પહેલા ચલાવો, પછી ચકાસો, એટલે body હંમેશાં ઓછામાં ઓછું એક વાર ચાલે (exit-controlled). બંધ કરતો semicolon ધ્યાનથી.
  • for (init; condition; update) { }: ગણતરી વાળો loop. init એક વાર ચાલે, condition દરેક રાઉન્ડ પહેલા ચકાસાય, update દરેક રાઉન્ડ પછી:

for (i = 1; i <= 60; i++) વાંચો: student 1 થી શરૂ કરો, 60 ની અંદર રહેતાં ચાલુ રાખો, એક-એક પગલું વધો.

At a glance

તમારો loop પસંદ કરવો

Loopચકાસેઓછામાં ઓછા runsકોના માટે બનેલો
whileBody પહેલા0અજાણ્યો count, આખો skip પણ થઈ શકે
do-whileBody પછી1Menus: ઓછામાં ઓછું એક વાર દેખાય
forપહેલા, counter સાથે0જાણીતો count (60 students)

Practical

આખો class, એક loop

#include <stdio.h>

int main() {
    int i, marks, total = 0;

    for (i = 1; i <= 5; i++) {
        printf("Marks of student %d: ", i);
        scanf("%d", &marks);
        total = total + marks;
    }
    printf("Class total: %d\n", total);
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

એક while loop ની condition પહેલી જ વાર ખોટી છે. એ જ condition એક do-while માં છે. દરેક body કેટલી વાર ચાલે છે?

  1. while: 0 વાર, do-while: 1 વાર
  2. બંને 0 વાર ચાલે છે
  3. બંને 1 વાર ચાલે છે
  4. while: 1 વાર, do-while: 0 વાર
Show the answer

while: 0 વાર, do-while: 1 વાર

while entry-controlled છે: gate પર ખોટી ચકાસણી એટલે body ક્યારેય ચાલતી નથી. do-while exit-controlled છે: પહેલી ચકાસણી પહેલાં body PEHLA જ એક વાર ચાલી ચૂકી હોય છે. આ 0-વિરુદ્ધ-1 વાળો ફરક દરેક C paper નો સૌથી ભરોસાપાત્ર loop સવાલ છે, ઘણી વાર "while અને do-while વચ્ચે તફાવત" તરીકે.

Think first

Loop trace કરો

ઉપરના code માં નાખેલા 5 marks છે 70, 80, 60, 90, 75. Loop મનમાં ચલાવો: દરેક રાઉન્ડ પછી total શું છે, અને અંતે શું print થાય છે?

Show the answer

total વધે છે 0 → 70 → 150 → 210 → 300 → 375, અને program print કરે છે Class total: 375.

જે pattern તમે હમણાં વાપર્યો એના પર ધ્યાન આપો: 0 થી શરૂ થતો એક running total, loop ની અંદર update થતો. એ accumulator pattern તમારી બાકીની programming જિંદગીમાં averages, counts અને sums માં ફરી ફરી પાછો આવે છે.

Watch out

3 loop killers

રખડતો semicolon: for (i = 1; i <= 5; i++); loop ને ખાલી body સાથે પૂરો કરી નાખે; નીચેનો block એક વાર ચાલે. એ જ બીમારી જે if ના semicolon ની છે. Infinite loop: update (i++) ભૂલી જાઓ અને condition ક્યારેય ખોટી ન થાય; program અટકી પડે. Off-by-one: i < 5 0 થી શરૂ થઈને 5 વાર ચાલે, i <= 5 0 થી શરૂ થઈને 6 વાર, exam માં દર વખતે આંગળીઓ પર ગણો.

Theory

ત્રણેય આકાર હાંસલ

Sequence, selection, અને હવે iteration: marks program દરેક structured-programming આકાર વાપરે છે, બરાબર જેમ Unit 1 માં વચન હતું. 2 સુધારા loop ની વાર્તા પૂરી કરે છે: loops ની અંદર loops (pattern-print વાળા સવાલ જે exams ને બહુ ગમે છે), અને કટોકટીના બહાર નીકળવાના રસ્તા break તથા continue. બંને હવે પછી છે.

Summary

Key takeaways

  • while: પહેલા ચકાસ, શૂન્ય વાર પણ ચાલી શકે (entry-controlled).
  • do-while: પછી ચકાસ, હંમેશાં ઓછામાં ઓછું એક વાર ચાલે (exit-controlled), semicolon ધ્યાનથી.
  • for (init; condition; update): જાણીતી પુનરાવૃત્તિ માટે ગણતરી વાળો loop.
  • Killers: for(...) પછી semicolon, છૂટેલો update (infinite loop), off-by-one સીમાઓ.
  • Running-total accumulator એ જિંદગીભરનો loop pattern છે.
  • યાદ રાખવાની યુક્તિ: guard પહેલા ચકાસે (while), પહેલા ચાખો (do-while), attendance ગણતરી (for).

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Decision Making and Iterative statements

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Iterative statements: while, do-while, for loop · Computer Programming and Programming Methodology (CPPM) · Gri-Learn