Control Structures: Conditional and Looping

The two building blocks that let a program make decisions and repeat actions.

Control Structures: Conditional and Looping

Programming में program का execution (निष्पादन) हमेशा एक ही क्रम में नहीं होता। कई बार हमें किसी condition (शर्त) के आधार पर अलग-अलग statements execute करनी होती हैं या किसी statement/block को बार-बार execute करना होता है। Program के execution flow को control करने के लिए Control Structures का उपयोग किया जाता है।

Control Structures: Program के execution flow को नियंत्रित करने वाली programming structures।

What is a Control Structure?

Control Structure programming में statements के execution के order और flow को control करने का तरीका है। इसके माध्यम से programmer यह निर्धारित कर सकता है कि कौन-सा statement कब execute होगा, कौन-सा statement condition के आधार पर execute होगा और कौन-सा statement कितनी बार execute होगा।

Control structures programming logic को अधिक flexible और powerful बनाते हैं। इनके बिना complex decision-making और repetitive tasks को efficiently implement करना कठिन हो सकता है।

Types of Control Structures

Programming में control structures को सामान्यतः निम्नलिखित categories में समझा जा सकता है:

  1. Sequential Control Structure
  2. Conditional Control Structure
  3. Looping or Iterative Control Structure
Control Structure Purpose Example
Sequential Statements को क्रम से execute करना Statement 1 → Statement 2 → Statement 3
Conditional Condition के आधार पर decision लेना if, if-else
Looping Statements को बार-बार execute करना for, while

Sequential Control Structure

Sequential Control Structure में statements जिस क्रम में लिखे गए हैं, सामान्यतः उसी क्रम में execute होते हैं। यह program execution का सबसे simple form है।

Flow: Statement 1 → Statement 2 → Statement 3 → Statement 4

उदाहरण के लिए, यदि program में पहले दो numbers input लिए जाते हैं, फिर उनका sum calculate किया जाता है और अंत में result display किया जाता है, तो ये statements sequential order में execute होंगे।

Conditional Control Structure

Conditional Control Structure का उपयोग condition के आधार पर decision लेने के लिए किया जाता है। इसमें program किसी condition को evaluate करता है और condition के result के आधार पर appropriate statement या block execute करता है।

Condition → True/False → Appropriate Action

उदाहरण के लिए, यदि किसी student के marks 40 या उससे अधिक हैं, तो उसे Pass display किया जा सकता है; अन्यथा Fail display किया जा सकता है।

Types of Conditional Statements

Programming languages में conditional statements के विभिन्न forms हो सकते हैं। सामान्य forms हैं:

  1. if Statement
  2. if-else Statement
  3. if-elif-else / else-if Ladder
  4. Nested if
  5. switch/case या match जैसी multi-way selection structures

1. if Statement

if statement का उपयोग तब किया जाता है जब किसी condition के true होने पर कोई statement या block execute करना हो। यदि condition false है, तो उस block को execute नहीं किया जाता।

Basic Logic:
IF condition is True → Execute the statement/block
IF condition is False → Skip the statement/block

Example: यदि temperature 35°C से अधिक है, तो program "Hot Day" display कर सकता है।

2. if-else Statement

if-else statement का उपयोग तब किया जाता है जब दो possible alternatives में से एक को choose करना हो। Condition true होने पर if block execute होता है और condition false होने पर else block execute होता है।

Condition → True → if block
Condition → False → else block

Example: यदि marks 40 या अधिक हैं तो "Pass" display करें, अन्यथा "Fail" display करें।

3. else-if / elif Ladder

जब multiple conditions को check करना हो, तब else-if ladder का उपयोग किया जा सकता है। विभिन्न programming languages में इसे else-if, elif या similar syntax से represent किया जा सकता है।

उदाहरण के लिए, marks के आधार पर grade निर्धारित करना:

Marks Grade
90 या अधिक A+
75–89 A
60–74 B
40–59 C
40 से कम F

ऐसी situation में multiple conditions को sequence में check किया जा सकता है।

4. Nested if

Nested if का अर्थ है एक if statement के अंदर दूसरा if statement रखना। इसका उपयोग तब किया जा सकता है जब एक decision के बाद किसी additional condition को check करना हो।

उदाहरण के लिए, पहले यह check करना कि student pass है या नहीं और उसके बाद pass students के लिए distinction की condition check करना।

First Condition → True → Second Condition → Appropriate Result

5. switch/case or Multi-way Selection

कुछ programming languages में switch/case जैसी control structure का उपयोग एक expression या value के आधार पर multiple alternatives में से एक selection करने के लिए किया जाता है। अन्य languages में इसके समान purpose के लिए match या अन्य constructs उपलब्ध हो सकते हैं।

उदाहरण के लिए, menu में 1, 2, 3 और 4 अलग-अलग operations को represent कर सकते हैं और selected value के अनुसार corresponding operation execute किया जा सकता है।

Looping Control Structure

Looping या Iterative Control Structure का उपयोग किसी statement या block of statements को बार-बार execute करने के लिए किया जाता है। यह तब उपयोगी होता है जब एक ही प्रकार का task multiple times perform करना हो।

Loop: एक statement/block को निर्धारित condition के अनुसार repeatedly execute करना।

उदाहरण के लिए, 1 से 10 तक numbers display करने के लिए प्रत्येक number के लिए अलग-अलग statement लिखने के बजाय loop का उपयोग किया जा सकता है।

Types of Loops

Programming में सामान्यतः निम्नलिखित loops का उपयोग किया जाता है:

  1. for Loop
  2. while Loop
  3. do-while Loop

इनका exact syntax programming language के अनुसार अलग हो सकता है।

1. for Loop

for loop का उपयोग सामान्यतः तब किया जाता है जब repetition की संख्या या iteration range पहले से ज्ञात हो या किसी sequence/collection के elements पर iterate करना हो।

उदाहरण के लिए, 1 से 5 तक numbers display करना:

Iterations: 1 → 2 → 3 → 4 → 5

इस प्रकार के task में for loop code को concise और readable बना सकता है।

2. while Loop

while loop में statements तब तक repeatedly execute होते हैं जब तक specified condition true रहती है। Condition को सामान्यतः प्रत्येक iteration से पहले check किया जाता है।

Logic:
Condition True → Execute Loop Body → Check Condition Again
Condition False → Exit Loop

उदाहरण के लिए, जब तक user valid input नहीं देता, तब तक input मांगते रहना while loop से implement किया जा सकता है।

3. do-while Loop

do-while loop में loop body को पहले execute किया जाता है और उसके बाद condition check की जाती है। इसलिए यदि condition initially false भी हो, तो loop body कम-से-कम एक बार execute होती है।

Logic:
Execute Loop Body → Check Condition → True होने पर Repeat

do-while loop उन situations में उपयोगी हो सकता है जहाँ किसी action को कम-से-कम एक बार perform करना आवश्यक हो।

for, while and do-while – Comparison

Loop Condition Check Typical Use
for Loop iterations/range के अनुसार Known range या sequence पर iteration
while Body execute होने से पहले जब repetition condition पर depend करती हो
do-while Body execute होने के बाद जब body को कम-से-कम एक बार execute करना हो

Conditional vs Looping Control Structure

Conditional Looping
Decision लेने के लिए उपयोग होता है। Repetition के लिए उपयोग होता है।
Condition के आधार पर alternatives में से selection करता है। Condition के आधार पर statements को repeatedly execute करता है।
if और if-else इसके common examples हैं। for, while और do-while इसके common examples हैं।

Example of Conditional Control Structure

मान लीजिए किसी student के marks 65 हैं और हमें यह determine करना है कि student Pass है या Fail। यदि passing marks 40 हैं, तो condition इस प्रकार होगी:

Condition: Marks ≥ 40
Result: Pass

यदि marks 35 होते, तो condition false होती और result "Fail" हो सकता था।

Example of Looping Control Structure

मान लीजिए हमें numbers 1 से 5 तक display करने हैं। प्रत्येक number के लिए अलग statement लिखने के बजाय loop का उपयोग किया जा सकता है।

Output: 1, 2, 3, 4, 5

यहाँ एक loop एक ही प्रकार के operation को multiple iterations में perform करता है।

Nested Loops

Nested Loop का अर्थ है एक loop के अंदर दूसरा loop रखना। इसका उपयोग tables, patterns, matrices और multi-dimensional data processing जैसे tasks में किया जा सकता है।

उदाहरण के लिए, 3 × 3 matrix के प्रत्येक element को process करने के लिए एक outer loop rows के लिए और एक inner loop columns के लिए उपयोग किया जा सकता है।

Outer Loop → Inner Loop → Repeat Inner Loop → Next Outer Iteration

Nested Conditional Statements

एक conditional statement के अंदर दूसरा conditional statement रखने को nested conditional structure कहा जाता है। इसका उपयोग multiple levels of decision-making के लिए किया जा सकता है।

उदाहरण के लिए, पहले age check करना और फिर eligibility के आधार पर additional condition check करना।

Infinite Loop

Infinite Loop वह loop है जो समाप्त नहीं होता क्योंकि उसकी termination condition कभी false नहीं होती या loop को exit करने का कोई appropriate mechanism नहीं होता।

Infinite loop कई बार programming mistake के कारण उत्पन्न हो सकता है। इसलिए loops में condition और update mechanism को carefully design करना आवश्यक है।

Important: Loop को terminate करने के लिए condition eventually false होनी चाहिए, जब तक कि intentional infinite loop की आवश्यकता न हो।

Loop Control Statements

कई programming languages में loop के execution को अतिरिक्त रूप से control करने के लिए special statements उपलब्ध होते हैं। इनके examples हैं:

  • break: Loop या control structure से बाहर निकलने के लिए।
  • continue: Current iteration के remaining statements को skip करके next iteration पर जाने के लिए।

इन statements का exact behavior और syntax programming language के अनुसार अलग हो सकता है।

Importance of Control Structures

Control structures programming में बहुत महत्वपूर्ण हैं क्योंकि इनके माध्यम से program complex decisions और repetitive tasks को efficiently handle कर सकता है। इनके उपयोग से code को अधिक structured, flexible और reusable बनाया जा सकता है।

  • Decision making को implement किया जा सकता है।
  • Repeated tasks को efficiently perform किया जा सकता है।
  • Code duplication कम किया जा सकता है।
  • Complex logic को organize किया जा सकता है।
  • Program को dynamic और flexible बनाया जा सकता है।

Common Mistakes in Control Structures

  • Condition गलत लिखना।
  • if और else blocks का incorrect arrangement करना।
  • Loop termination condition को गलत रखना।
  • Loop variable को properly update न करना।
  • Unintended infinite loop बन जाना।
  • Nested structures को unnecessarily complex बनाना।
  • break और continue का गलत उपयोग करना।
  • Boundary conditions को ignore करना।

Important Points

  • Control Structures program के execution flow को control करती हैं।
  • Sequential structure statements को क्रम से execute करती है।
  • Conditional structures conditions के आधार पर decision लेने के लिए उपयोग होती हैं।
  • if और if-else common conditional statements हैं।
  • Multiple conditions के लिए else-if/elif ladder का उपयोग किया जा सकता है।
  • Nested if में एक conditional statement दूसरे के अंदर होता है।
  • Looping structures statements को repeatedly execute करती हैं।
  • for, while और do-while common loop structures हैं।
  • for loop known range या sequence पर iteration के लिए उपयोगी है।
  • while loop में condition सामान्यतः body से पहले check होती है।
  • do-while loop में body कम-से-कम एक बार execute होती है।
  • Nested loops में एक loop दूसरे loop के अंदर होता है।
  • Infinite loop तब होता है जब loop terminate नहीं होता।
  • break और continue जैसे statements loop execution को control कर सकते हैं।

Board Focus

Exam के लिए याद रखें:
Control Structure → Program के execution flow को control करना
Sequential → Statements को क्रम से execute करना
Conditional → Condition के आधार पर decision लेना
if → Condition true होने पर statement execute करना
if-else → दो alternatives में से selection करना
Looping → Statements को repeatedly execute करना
for → Known range/sequence के लिए useful
while → Condition true रहने तक repeat करना
do-while → Body को कम-से-कम एक बार execute करना
break → Loop से बाहर निकलना
continue → Current iteration skip करके next iteration पर जाना

Board Important Questions

Very Short Answer Questions

Q1. Control Structure क्या है?

Answer: Program के execution flow को control करने वाली programming structure को Control Structure कहते हैं।

Q2. Conditional Control Structure क्या है?

Answer: Condition के आधार पर decision लेने वाली control structure को Conditional Control Structure कहते हैं।

Q3. Looping क्या है?

Answer: किसी statement या block को condition के अनुसार बार-बार execute करने की process Looping कहलाती है।

Q4. दो conditional statements के नाम लिखिए।

Answer: if और if-else।

Q5. दो looping statements के नाम लिखिए।

Answer: for और while।

Q6. कौन-सा loop body को कम-से-कम एक बार execute करता है?

Answer: do-while loop।

Q7. Infinite Loop क्या है?

Answer: ऐसा loop जो terminate नहीं होता और लगातार execute होता रहता है, Infinite Loop कहलाता है।

Q8. break statement का क्या उपयोग है?

Answer: break statement का उपयोग loop या संबंधित control structure से बाहर निकलने के लिए किया जाता है।

Q9. continue statement का क्या उपयोग है?

Answer: continue statement current iteration के remaining statements को skip करके next iteration पर जाने के लिए उपयोग किया जाता है।

Q10. Sequential Control Structure क्या है?

Answer: जिसमें statements सामान्यतः उसी क्रम में execute होते हैं जिस क्रम में वे लिखे गए हैं, उसे Sequential Control Structure कहते हैं।

Short Answer Questions

Q11. Conditional और Looping Control Structures में अंतर बताइए।

Conditional Looping
Decision लेने के लिए उपयोग होता है। Repetition के लिए उपयोग होता है।
Condition के आधार पर statement/block का selection करता है। Condition के आधार पर statement/block को repeatedly execute करता है।
if और if-else examples हैं। for और while examples हैं।

Q12. if और if-else statement में क्या अंतर है?

Answer: if statement में condition true होने पर statement या block execute होता है और false होने पर उसे skip किया जाता है। if-else statement में condition true होने पर if block और false होने पर else block execute होता है। इसलिए if-else दो alternative actions में से एक को select करने के लिए उपयोगी है।

Q13. for और while loop में अंतर बताइए।

for Loop while Loop
Known range या sequence पर iteration के लिए commonly उपयोग होता है। Condition के true रहने तक repetition के लिए commonly उपयोग होता है।
Iterations की संख्या या range अक्सर पहले से निर्धारित होती है। Iterations की संख्या condition और data पर depend कर सकती है।

Q14. while और do-while loop में अंतर बताइए।

while do-while
Condition सामान्यतः loop body से पहले check होती है। Condition loop body execute होने के बाद check होती है।
Condition initially false होने पर body execute नहीं हो सकती। Body कम-से-कम एक बार execute होती है।

Q15. Infinite Loop क्या है? इसे avoid कैसे किया जा सकता है?

Answer: ऐसा loop जो terminate नहीं होता, Infinite Loop कहलाता है। इसे avoid करने के लिए loop की termination condition को सही तरीके से define करना, loop control variable को उचित रूप से update करना और boundary conditions को carefully check करना चाहिए।

Long Answer Questions

Q16. Control Structures के विभिन्न प्रकारों को समझाइए।

Answer: Control Structures program के execution flow को control करती हैं। Sequential Control Structure में statements सामान्यतः अपने लिखे हुए क्रम में execute होते हैं। Conditional Control Structure condition के आधार पर decision लेने के लिए उपयोग होती है, जैसे if और if-else। Looping Control Structure किसी statement या block को बार-बार execute करने के लिए उपयोग होती है, जैसे for, while और do-while। इन structures की सहायता से complex program logic को efficiently implement किया जा सकता है।

Q17. Looping Control Structure के विभिन्न प्रकारों को समझाइए।

Answer: Looping Control Structure का उपयोग statements को repeatedly execute करने के लिए किया जाता है। for loop सामान्यतः known range या sequence के लिए उपयोग किया जाता है। while loop में condition को सामान्यतः body से पहले check किया जाता है और condition true रहने तक loop चलता है। do-while loop में body पहले execute होती है और condition बाद में check होती है, इसलिए body कम-से-कम एक बार execute होती है।

Q18. Conditional Control Structure को उदाहरण सहित समझाइए।

Answer: Conditional Control Structure का उपयोग condition के आधार पर decision लेने के लिए किया जाता है। यदि किसी student के marks 40 या उससे अधिक हैं तो Pass और अन्यथा Fail display करने के लिए if-else statement का उपयोग किया जा सकता है। Multiple conditions के लिए else-if/elif ladder और complex decision-making के लिए nested if का उपयोग किया जा सकता है।

Quick Revision

  • Control Structure: Program के execution flow को control करना।
  • Sequential: Statements को क्रम से execute करना।
  • Conditional: Condition के आधार पर decision लेना।
  • if: Condition true होने पर block execute करना।
  • if-else: दो alternatives में से एक select करना।
  • Looping: Statements को repeatedly execute करना।
  • for: Range या sequence पर iteration करना।
  • while: Condition true रहने तक repeat करना।
  • do-while: Body को कम-से-कम एक बार execute करना।
  • break: Loop से बाहर निकलना।
  • continue: Current iteration skip करना।
  • Infinite Loop: ऐसा loop जो terminate नहीं होता।
One-Line Revision: Control Structures program के execution flow को नियंत्रित करती हैं; Conditional structures decision लेने के लिए और Looping structures statements को repeatedly execute करने के लिए उपयोग की जाती हैं।

Practice Questions

  1. Control Structure क्या है?
  2. Control Structures के विभिन्न प्रकारों के नाम लिखिए।
  3. Sequential Control Structure को समझाइए।
  4. Conditional Control Structure क्या है?
  5. if और if-else में अंतर बताइए।
  6. else-if/elif ladder क्या है?
  7. Nested if क्या है?
  8. Looping Control Structure क्या है?
  9. for, while और do-while loops को समझाइए।
  10. for और while loop में अंतर बताइए।
  11. while और do-while loop में अंतर बताइए।
  12. Infinite Loop क्या है?
  13. break और continue statements का उपयोग समझाइए।
  14. Conditional और Looping Control Structures में अंतर बताइए।
  15. Control Structures के महत्व को उदाहरण सहित समझाइए।
Lesson 15 of 39
On This Page