Control structure: If...Else, switch..case; While, Do...While, For loop; break, continue

Control structures code के through path तय करती हैं: if/else और switch एक branch चुनते हैं, while / do-while / for एक block repeat करते हैं, और break एक loop को जल्दी रोकता है जबकि continue अगली बारी तक skip करता है, switch cases को fall through से बचने के लिए break चाहिए।

11 min read · 9 cards · 2 checks

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


Theory

Page को decisions लेने हैं

FestConnect को तय करना है: क्या यह student eligible है (year 2 या ऊपर)? इसे repeat करना है: 30 events में से हर एक print करना। इसे options में चुनना है: cash, card, या UPI। और कभी-कभी इसे जल्दी रुकना है: एक full event मिला, scanning बंद कीजिए।

अब तक code सीधा नीचे चला, top से bottom। Control structures उस flow को मोड़ती हैं: वे code को branch (एक path चुनने) और loop (एक path repeat करने) देती हैं। दो तरह की branch, तीन तरह के loops, और एक को interrupt करने के दो तरीक़े।

Theory

Forks और roundabouts

Code को driving की तरह पढ़ते हुए: एक if/else एक fork है, आप एक sign के आधार पर एक road लेते हैं। एक switch एक multi-exit roundabout है: आप उस exit से निकलते हैं जो आपकी destination से match करता है। एक loop एक roundabout है जिसे आप तब तक घुमाते रहते हैं जब तक एक condition आपको exit न दे। break एक emergency exit है जो तुरंत roundabout छोड़ देता है; continue इस lap को ख़त्म किए बिना सीधे अगले lap पर skip करता है।

Theory

पहले Branch, फिर loop

Branching:

  • if (cond) { } else if (cond2) { } else { }: उस पहले block को run कीजिए जिसकी condition true है।
  • switch (value) { case x: ...; break; default: ... }: एक value को कई के against match कीजिए। हर case को break चाहिए।

Looping:

  • while (cond) { }: पहले check कीजिए, फिर run कीजिए (zero बार भी चल सकता है)।
  • do { } while (cond): एक बार run कीजिए, फिर check कीजिए (कम से कम एक pass)।
  • for (init; cond; update) { }: counting loop।

Interrupt: break loop छोड़ देता है; continue इसकी अगली बारी तक skip करता है।

Practical

FestConnect decisions और loops

let year = 3;
if (year >= 2) {
  console.log("Eligible");
} else {
  console.log("First years cannot register");
}

// switch: each case needs break
let pay = "upi";
switch (pay) {
  case "cash": console.log("Pay at desk"); break;
  case "upi":  console.log("Scan QR");     break;
  default:     console.log("Unknown");
}

// for loop over 3 events, skip the full one, stop at 'gala'
let events = ["robotics", "full", "dance", "gala"];
for (let i = 0; i < events.length; i++) {
  if (events[i] === "full") continue;   // skip this turn
  if (events[i] === "gala") break;      // stop entirely
  console.log(events[i]);               // robotics, dance
}

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

Think first

while बनाम do-while

seatsLeft 0 है। while (seatsLeft > 0) { book(); } को do { book(); } while (seatsLeft > 0); से compare कीजिए। हर एक में book() कितनी बार run होता है, और यह FestConnect के लिए क्यों matter करता है?

Show the answer

while version book() को zero बार run करता है: यह पहले 0 > 0 check करता है, जो false है, तो यह कभी अंदर नहीं जाता। do-while version check करने से पहले book() को एक बार run करता है, एक ऐसी seat book करते हुए जो exist ही नहीं करती, फिर रुकता है। do-while हमेशा कम से कम एक बार run होता है, जो यहाँ एक bug है। Rule: do-while सिर्फ़ तभी इस्तेमाल कीजिए जब body को असल में चाहे कुछ भी हो कम से कम एक बार run होना ही है (जैसे repeat पूछने से पहले एक menu दिखाना)।

Quiz

एक switch में `case "cash": console.log("Desk"); case "upi": console.log("QR");` है बिना किसी break के। Value "cash" है। क्या print होता है?

  1. दोनों 'Desk' और 'QR', क्योंकि execution break के बिना fall through करता है
  2. सिर्फ़ 'Desk'
  3. सिर्फ़ 'QR'
  4. कुछ नहीं, क्योंकि एक case में जाने के लिए एक break चाहिए
Show the answer

दोनों 'Desk' और 'QR', क्योंकि execution break के बिना fall through करता है

break के बिना, एक बार एक case match होने पर, execution 'fall through' करता है और अगले cases भी चलाता रहता है, तो 'cash' दोनों 'Desk' और 'QR' print करता है (A)। यह पहले पर नहीं रुकता (B), और यह matching case में fine जाता है (C, D ग़लत हैं)। यह fall-through classic switch bug है: हर case के आख़िर में break रखिए जब तक आप जानबूझकर उनके बीच code share करना न चाहें।

Watch out

Control-flow traps

1. Switch में missing break: execution बाद के cases तक fall through करता है। हर case को break से ख़त्म कीजिए।

2. Infinite loop: एक while/for जिसकी condition कभी false नहीं होती (आप counter बदलना भूल गए) page freeze कर देता है। हमेशा exit की तरफ़ move कीजिए।

3. do-while एक बार run होता है जब नहीं होना चाहिए: check-first while इस्तेमाल कीजिए जब तक एक guaranteed pass intended न हो।

4. एक condition में =: if (year = 2) assign करता है, हमेशा truthy। === इस्तेमाल कीजिए।

Theory

Loops validation को power देते हैं

Unit 5 में, form validation हर required field पर loop करता है यह check करते हुए कि यह भरा है, बिल्कुल इसी for plus एक if इस्तेमाल करते हुए, और पहली empty field report करने के लिए break out करता है। यहाँ की eligibility if उस validation logic का बीज है। Branching और looping हर interactive feature की रीढ़ हैं जो आप बनाएँगे।

Summary

Key takeaways

  • if / else if / else (एक path चुनिए) और switch/case (एक value को कई के against match कीजिए) से branch कीजिए।
  • while (पहले check), do-while (पहले एक बार run), for (counting loop) से loop कीजिए।
  • break तुरंत एक loop छोड़ता है; continue loop की अगली बारी तक skip करता है।
  • हर switch case को break चाहिए वरना execution अगले case तक fall through करता है।
  • Infinite loops से बचिए (हमेशा condition को false की तरफ़ move कीजिए) और किसी condition में कभी = इस्तेमाल मत कीजिए।
  • Memory hook: forks (if/switch) और roundabouts (loops), break emergency exit के रूप में।

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 Overview of JavaScript

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

Control structure: If...Else, switch..case; While, Do...While, For loop; break, continue · Web Designing-1 (option A) · Gri-Learn