Statements and Control Structures : प्रकार, उदाहरण एवं उपयोग”

Statements (स्टेटमेंट्स)

एक Statement कंप्यूटर को दिया गया एक पूरा निर्देश (Complete Instruction) होता है। ये एक सेंटेंस की तरह होते हैं, पर प्रोग्रामिंग भाषा के ग्रामर (Syntax) के हिसाब से लिखे जाते हैं। हर स्टेटमेंट के बाद अक्सर एक सेमी-कोलन (;) लगता है, ताकि कंप्यूटर को पता चल जाए कि एक निर्देश खत्म हो गया है।

उदाहरण : 

a = 25;
print(“Hello World”);

  • age = 25;
    ये एक Assignment Statement है। इसका अर्थ है, age नामक वेरिएबल को 25 का मान असाइन करना।
  • print(“Hello, World!”);
    यह एक आउटपुट स्टेटमेंट है जो स्क्रीन पर “Hello, World!” प्रदर्शित करता है।
Types of Statements (प्रकार)

Statements कई तरह के हो सकते हैं। कुछ मुख्य प्रकार ये हैं:

  1. Declaration Statements 
      • जब आप कोई नया Variable, Function या Class बनाते हैं तो इसका उपयोग होता है। जैसे, C++ में int a; लिखना।
  2. Expression Statements 
      • जब आप कोई गणितीय ऑपरेशन करते हैं। जैसे, x + y; (यह अकेले बहुत उपयोगी नहीं है जब तक कि इसे किसी Variable में संग्रहीत न किया जाए)।
  3. Control Statements 
      • ये वो स्टेटमेंट होते हैं जो प्रोग्राम के फ्लो को कंट्रोल करते हैं, जैसे कि if, while, for आदि। 
  4. Compound Statements (Block Statements) 
      • जब हमें एक से ज्यादा Statements को एक साथ ग्रुप करना होता है, तो हम उन्हें कर्ली ब्रेसेस {} के अंदर रखते हैं। ये loops और conditions में विशेष रूप से उपयोगी होते हैं।

Control Structures (कंट्रोल स्ट्रक्चर)

Control Structures की। ये वो Tools हैं जो प्रोग्राम के Flow (प्रवाह) को Control करते हैं। ये बताते हैं कि कौन सा Statement कब और कितनी बार चलेगा। Control Structures प्रोग्राम को Logic और Order देते हैं। इनके बिना, प्रोग्राम के सभी Statements सिर्फ़ एक-एक करके चलेंगे, बिना किसी condition या loop के।

Categories of Control Structures (मुख्य श्रेणियाँ)
1. Selection/Conditional Structures

ये Structures हमें Programs में Decision-making (निर्णय लेने) की Power देते हैं। ये किसी condition को check करते हैं और उस condition के True या False होने पर अलग-अलग Statements को Execute करते हैं।

(a) if Statement

केवल तभी execute होगा जब condition true हो।

Syntax :

if(condition) {
// statements
}

(b) if-else Statement

यह सबसे Basic Conditional Structure है।

इसका Flow ऐसे होता है:

  • Condition check करो।
  • अगर Condition True है, तो if Block के अंदर के Statements चलाओ।
  • अगर Condition False है, तो else Block के अंदर के Statements चलाओ।

Syntax :

if(condition) {
// statements if condition is true
} else {
// statements if condition is false
}

(c) if-elif-else Statement
जब एक से ज्यादा कंडीशन हों, तो ये काम आता है। elif का मतलब है “else if”।

Syntax :

if (condition1) {
// statements जब condition1 true हो
}
else if (condition2) {
// statements जब condition2 true हो
}
else if (condition3) {
// statements जब condition3 true हो
}
else {
// statements जब ऊपर की कोई भी condition true न हो
}

(e) switch-case Statement
यह भी if-elif-else की तरह ही है, लेकिन इसका इस्तेमाल तब अच्छा होता है जब आपको किसी एक variable की वैल्यू को कई fixed options के साथ compare करना हो।

Syntax : 

switch(expression) {
case constant1:
// statements
break;
case constant2:
// statements
break;

default:
// statements
}

2. Iteration/Loop Structures

ये Structures हमें एक ही काम को बार-बार करने की सुविधा देते हैं, बिना Code को बार-बार लिखे।

(a) for Loop
for Loop का इस्तेमाल तब होता है जब हमें पहले से पता हो कि कोई काम कितनी बार करना है।

Flow:

  • Start: Loop कहाँ से शुरू होगा? (जैसे, i=0)
  • Condition: कब तक चलेगा? (जैसे, i < 10)
  • Step: हर बार कितना आगे बढ़ेगा? (जैसे, i++ या i–)

Syntax : 

for(initialization; condition; increment/decrement) {
// statements
}

(b) While loop
While loop का उपयोग तब किया जाता है जब हमें नहीं पता होता कि लूप कितनी बार चलेगा। यह तब तक चलता है जब तक Condition True रहती है।

Syntax : 

while(condition) {
// statements
}

(c) do-while Loop
यह while Loop की तरह ही है, पर एक महत्वपूर्ण चेतावनी है: do-while Loop का कोड कम से कम एक बार ज़रूर चलता है, चाहे कंडीशन सही हो या गलत।

Syntax : 

do {
// statements
} while(condition);

3. Jump/Transfer Statements

ये Statements प्रोग्राम के Flow को Normal Sequence से अलग जगह पर ले जाते हैं।

(a) break Statement
यह Loop या switch Statement से तुरंत बाहर आने के लिए उपयोग होता है।

Syntax : 

break; 

(b) continue Statement
यह Loop के अंदर वर्तमान Iteration को Skip करके अगली Iteration पर जाने के लिए उपयोगी होता है।

Syntax : 

continue;

 

 (c) return Statement

यह एक Function से वापस आने और एक Value लौटाने के लिए इस्तेमाल होता है।

Syntax : 

return value;

error: Content is protected !!