Jumping statements: goto, break, continue

break loop ને તરત છોડી દે છે, continue ફક્ત આ round નો બાકીનો ભાગ skip કરે છે, અને goto કોઈ label પર કૂદે છે: legal છે, પણ એને ટાળવામાં આવે છે.

5 min read · 9 cards · 5 checks

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


Theory

આ ક્યાં અટકે છે?

5 સુધી ગણવાના એક loop માં એક વધારાની line છે:

for (i = 1; i <= 5; i++) {

if (i == 3) break;

printf("%d ", i);

}

Screen પર શું આવે છે?

Think first

પહેલાં તમારો અંદાજ

જે print થશે તે બરાબર એ જ રીતે type કરો.

Show the answer

1 2. i = 3 પર break printf ચાલે તે પહેલાં જ આખો loop પૂરો કરી દે છે.

Theory

break, continue, goto

break; જે loop કે switch માં હોય તેને તરત છોડી દે છે. continue; ફક્ત આ round નો બાકીનો ભાગ skip કરે છે; for loop માં update તો પણ ચાલે છે. goto label; એ જ function માં label: પર કૂદે છે: legal છે, પણ spaghetti code બનાવે છે, એટલે એને ટાળવામાં આવે છે.

Quiz

શરૂઆતના loop માં break ની જગ્યાએ continue મૂકો. હવે શું print થાય છે?

  1. 1 2 4 5
  2. 1 2
  3. 1 2 3 4 5
  4. 3
Show the answer

1 2 4 5

continue ફક્ત round 3 નું printf skip કરે છે, અને loop 4 અને 5 સાથે આગળ ચાલે છે. 1 2 તો break આપે છે: break છોડી દે છે, continue skip કરે છે.

Practical

Nested loop ની અંદર break

#include <stdio.h>

int main() {
    int i, j;

    for (i = 1; i <= 3; i++) {
        for (j = 1; j <= 3; j++) {
            if (j == 2)
                break;
            printf("%d%d ", i, j);
        }
    }
    return 0;
}

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

Quiz

ઉપરનો program શું print કરે છે?

  1. 11 21 31
  2. 11
  3. 11 12 13 21 22 23 31 32 33
  4. 11 12
Show the answer

11 21 31

break ફક્ત અંદરનો loop છોડે છે, એટલે બહારના દરેક round માં ફક્ત એની j = 1 વાળી જોડી print થાય છે. જવાબ 11 માની લે છે કે break બંને loops છોડી દે છે; એવું ક્યારેય થતું નથી.

Think first

continue ની જાળ

અહીં શું થાય છે? તમારો જવાબ type કરો.

i = 0;

while (i < 5) {

if (i == 2) continue;

printf("%d ", i);

i++;

}

Show the answer

0 1, અને પછી એ હંમેશાં ચાલતો જ રહે છે: i = 2 પર continue i++ ચાલે તે પહેલાં જ પાછો ચકાસણી પર કૂદી જાય છે, એટલે i 2 જ રહે છે. for loop માં આ જોખમ નથી: એનું update તો પણ ચાલે છે.

Think first

Exam-style: break અને continue

break અને continue વચ્ચેનો તફાવત 3 points માં લખો, પછી સરખાવો.

Show the answer
  • અસર: break loop પૂરો કરી દે છે; continue ફક્ત આ round ને.
  • એના પછી: break loop ની બહાર જાય છે; continue પછીના round પર.
  • ક્યાં: break loops અને switch બંનેમાં ચાલે છે; continue ફક્ત loops માં.

બંનેનું એક example પણ લખો.

Summary

Key takeaways

  • break loop કે switch ને તરત છોડે છે, ફક્ત એક level.
  • continue આ round નો બાકીનો ભાગ skip કરે છે; loop ચાલતો રહે છે.
  • goto કોઈ label પર કૂદે છે: legal છે, પણ એને ટાળવામાં આવે છે.
  • યાદ રાખવાની યુક્તિ: break છોડે છે, continue skip કરે છે.

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

Jumping statements: goto, break, continue · Computer Programming and Programming Methodology (CPPM) · Gri-Learn