BCA / B.Tech 14 min read

Looping & Decision Making in Hindi

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: Operating System (OS) Notes in Hindi | ऑपरेटिंग सिस्टम नोट्स (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Looping & Decision Making in Hindi - General Topics
  • • अवलोकन: Quick Answer: Linux shell scripting में Decision Making का मतलब conditions के आधार पर फैसला लेना है (जैसे if, if-else, elif और case statements), जिससे ...
  • • मुख्य अवधारणाएं: Looping decision making, For while if else case Hindi, Break looping,Looping decision making, For while if else case, Break looping
Quick Answer: Linux shell scripting में Decision Making का मतलब conditions के आधार पर फैसला लेना है (जैसे if, if-else, elif और case statements), जिससे तय होता है कि कौन सा command block run होगा। Looping का मतलब किसी task या command block को बार-बार दोहराना है (जैसे for, while और until loops)। दोनों के combination से Linux terminal पर repetitive administrative tasks का automation किया जाता है।

Looping और Decision Making क्या है? (Meaning & Concept)

Linux administration और shell scripting में दो काम सबसे ज़्यादा होते हैं: पहला, किसी condition के आधार पर फैसला लेना और दूसरा, एक ही काम को कई files या data items पर बार-बार दोहराना। इन्हीं दोनों tasks के लिए programming और scripting में control structures बनाए गए हैं। Linux operating system के architecture को समझने के लिए Linux in Hindi पढ़ें।

Advertisement

Looping Hindi Meaning (लूपिंग का मतलब)

साधारण भाषा में Loop का मतलब होता है एक गोल घेरा या चक्कर। Programming और Linux shell scripting में Looping का मतलब है: किसी code block या commands के set को एक निश्चित condition पूरी होने तक बार-बार execute करना।

उदाहरण के लिए, अगर आपको 100 users के accounts बनाने हैं या 50 log files का backup लेना है, तो आप 100 अलग-अलग commands नहीं लिखते। आप एक loop चलाते हैं जो वही command 100 बार अपने आप execute कर देता है। इससे समय बचता है और human errors नहीं होते।

Decision Making in Hindi (निर्णय लेने का मतलब)

Decision Making का सीधा अर्थ है: दी गई condition की जांच करके सही रास्ता चुनना। Real life में जैसे हम तय करते हैं कि "अगर बारिश होगी तो छाता लेंगे, नहीं तो नहीं लेंगे", ठीक वैसे ही shell script में भी condition check होती है।

Linux में script यह check करती है कि कोई file exist करती है या नहीं, variable की value क्या है, या disk space 90% से ज़्यादा भरा है या नहीं। Condition के True या False होने पर script तय करती है कि आगे कौन सा command block run करना है।

Linux Test Command और Operators

Linux bash shell में condition test करने के लिए test command या square brackets [ condition ] का इस्तेमाल होता है। Brackets के दोनों तरफ space होना अनिवार्य है।

Operator Category मतलब Example
-eq Numeric Equal to (बराबर है) [ $a -eq $b ]
-ne Numeric Not equal to (बराबर नहीं है) [ $a -ne $b ]
-gt Numeric Greater than (बड़ा है) [ $a -gt $b ]
-lt Numeric Less than (छोटा है) [ $a -lt $b ]
-ge Numeric Greater than or equal to [ $a -ge $b ]
-le Numeric Less than or equal to [ $a -le $b ]
= String दो strings बराबर हैं [ "$str1" = "$str2" ]
!= String दो strings बराबर नहीं हैं [ "$str1" != "$str2" ]
-f File File मौजूद है और regular file है [ -f "/etc/passwd" ]
-d File Directory मौजूद है [ -d "/home/user" ]

Decision Making in Linux (Conditional Statements)

Linux shell scripting में decision making के लिए 4 मुख्य statements होते हैं: if, if-else, if-elif-else और case statement।

1. if statement

यह सबसे simple conditional statement है। अगर दी गई condition True होती है, तो then के बाद के commands run होते हैं। अगर condition False हो, तो script बिना कुछ किए सीधे fi के बाहर आ जाती है।

Syntax:

if [ condition ]
then
    commands
fi

Bash Script Example:

#!/bin/bash
marks=65

if [ $marks -ge 40 ]
then
    echo "Result: Pass"
fi

Output:

Result: Pass

2. if-else statement

जब आपके पास दो रास्ते हों और condition True होने पर एक काम और False होने पर दूसरा काम करना हो, तब if-else का इस्तेमाल करते हैं।

Syntax:

if [ condition ]
then
    commands_if_true
else
    commands_if_false
fi

Bash Script Example:

#!/bin/bash
number=7

if [ $((number % 2)) -eq 0 ]
then
    echo "$number is Even"
else
    echo "$number is Odd"
fi

Output:

7 is Odd

3. if-elif-else ladder

जब दो से ज़्यादा conditions check करनी हों, तब elif (else if का short form) का इस्तेमाल किया जाता है। पहली matching condition का block run होते ही control सीधे fi पर चला जाता है।

Syntax:

if [ condition1 ]
then
    commands1
elif [ condition2 ]
then
    commands2
else
    default_commands
fi

Bash Script Example:

#!/bin/bash
marks=78

if [ $marks -ge 75 ]
then
    echo "Grade: Distinction"
elif [ $marks -ge 60 ]
then
    echo "Grade: First Division"
elif [ $marks -ge 40 ]
then
    echo "Grade: Second Division"
else
    echo "Grade: Fail"
fi

Output:

Grade: Distinction

4. case statement (Switch Case in Linux)

जब एक ही variable की कई possible values की जांच करनी हो, तो if-elif लिखने के बजाय case statement आसान और साफ रहता है। C language के switch की तरह यह काम करता है। इसमें हर case pattern के अंत में दो semicolons (;;) लगते हैं और block का अंत esac से होता है (जो case का उल्टा spelling है)।

Syntax:

case $variable in
    pattern1)
        commands1
        ;;
    pattern2)
        commands2
        ;;
    *)
        default_commands
        ;;
esac

Bash Script Example:

#!/bin/bash
action="start"

case $action in
    start)
        echo "Service is starting..."
        ;;
    stop)
        echo "Service is stopping..."
        ;;
    restart)
        echo "Service is restarting..."
        ;;
    *)
        echo "Invalid action! Use start, stop, or restart."
        ;;
esac

Output:

Service is starting...

Looping in Linux (for, while, until Loops)

Linux में commands को बार-बार run करने के लिए तीन loops होते हैं। Shell commands की पूरी list देखने के लिए Commands of Linux in Hindi पढ़ें।

1. for loop in Linux

Linux में for loop का उपयोग तब किया जाता है जब आपको पहले से पता हो कि loop कितनी बार चलेगा, या आपको किसी list, range या file list के हर element पर बारी-बारी से काम करना हो।

Bash में for loop लिखने के मुख्य रूप से दो तरीके होते हैं:

तरीका A: List या Range पर for loop (Standard Shell Syntax)

for var in item1 item2 item3 ... itemN
do
    commands
done

Range syntax: {start..end} या {start..end..step}

#!/bin/bash
echo "=== Number Range ==="
for i in {1..5}
do
    echo "Number: $i"
done

Output:

=== Number Range ===
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

तरीका B: C-Style for loop

अगर आप C या C++ background से हैं, तो Bash double parentheses (( )) में C-style syntax भी support करता है:

#!/bin/bash
for ((i=1; i<=3; i++))
do
    echo "Iteration: $i"
done

Output:

Iteration: 1
Iteration: 2
Iteration: 3

2. while loop in Linux

while loop तब तक execute होता रहता है जब तक दी गई condition True रहती है। जैसे ही condition False होती है, loop रुक जाता है। इसमें variable का initialization loop से पहले और increment loop body के अंदर होना ज़रूरी है, नहीं तो infinite loop बन सकता है।

Syntax:

while [ condition ]
do
    commands
done

Bash Script Example:

#!/bin/bash
count=1

while [ $count -le 4 ]
do
    echo "Current Count: $count"
    count=$((count + 1))
done

Output:

Current Count: 1
Current Count: 2
Current Count: 3
Current Count: 4

3. until loop in Linux

until loop ठीक while loop का उल्टा होता है। यह तब तक चलता रहता है जब तक condition False रहती है। जैसे ही condition True होती है, loop तुरंत terminate हो जाता है। बहुत से छात्र exam में while और until में confuse होते हैं।

Syntax:

until [ condition ]
do
    commands
done

Bash Script Example:

#!/bin/bash
num=1

until [ $num -gt 4 ]
do
    echo "Number value: $num"
    num=$((num + 1))
done

Output:

Number value: 1
Number value: 2
Number value: 3
Number value: 4

Loop Control Statements: break और continue

कई बार loop के सामान्य execution flow को बीच में ही बदलने की ज़रूरत पड़ती है। इसके लिए दो special commands उपयोग होते हैं:

  • break: यह command चलते हुए loop को तुरंत terminate कर देता है और control सीधे done के बाद वाले line पर पहुँच जाता है।
  • continue: यह current iteration के बचे हुए commands को छोड़ देता है और तुरंत अगले iteration पर चला जाता है।

Example:

#!/bin/bash
for n in 1 2 3 4 5
do
    if [ $n -eq 3 ]
    then
        continue    # 3 को skip करेगा
    fi
    if [ $n -eq 5 ]
    then
        break       # 5 पर loop रोक देगा
    fi
    echo "Value: $n"
done

Output:

Value: 1
Value: 2
Value: 4

Comparison: while Loop vs until Loop vs for Loop

नीचे दी गई table तीनों loops के technical अंतर को स्पष्ट करती है:

Feature for loop while loop until loop
Condition Check List के elements या counter condition पर Condition True होने पर चलता है Condition False होने पर चलता है
कब रुकता है? जब list के सभी items ख़त्म हो जाएं जैसे ही condition False हो जाए जैसे ही condition True हो जाए
Use Case Known number of iterations या fixed item list जब iteration count अज्ञात हो और True condition पर काम करना हो जब किसी event या True condition आने तक इंतज़ार करना हो
Loop Keyword for ... in ... do ... done while [ cond ] do ... done until [ cond ] do ... done
C-Style Variant Available: ((i=0; i<n; i++)) Available: while ((i < n)) Not standard in C
Advertisement

Decision Making and Looping in C vs Linux Shell Scripting

BCA और B.Tech के छात्र अक्सर C language और Linux Shell Scripting के syntax में भ्रमित हो जाते हैं। दोनों systems के syntax का comparison नीचे दिया गया है। C और Linux के interaction को गहराई से जानने के लिए C Interface with Linux in Hindi पढ़ें।

Feature C Language Linux Shell (Bash)
Condition syntax if (x > 0) { ... } if [ $x -gt 0 ]; then ... fi
Comparison operator ==, !=, >, <= -eq, -ne, -gt, -le
Blocks start/end Curly braces { } Keywords: then/fi, do/done, case/esac
Variable access Direct variable name: x Dollar sign ज़रूरी: $x
Switch/Case switch(x) { case 1: ... break; } case $x in 1) ... ;; esac
Boolean True/False 0 = False, Non-zero = True Exit code 0 = Success (True), Non-zero = Error (False)

Solved Example: Trace Table के साथ Complete Shell Script

यहाँ एक practical menu-driven program दिया गया है जो decision making और looping दोनों का इस्तेमाल करता है। यह script 1 से 3 तक का factorial calculate करती है और हर step का trace दिखाती है:

#!/bin/bash
echo "=== Factorial Trace Demo ==="

for num in 1 2 3
do
    fact=1
    i=1
    while [ $i -le $num ]
    do
        fact=$((fact * i))
        i=$((i + 1))
    done
    echo "Number: $num | Factorial: $fact"
done

Step-by-Step Execution Trace:

Step Outer Loop (num) Inner Loop (i) Condition [ $i -le $num ] fact calculation New fact Value
1 1 1 1 <= 1 (True) 1 * 1 1
2 1 2 2 <= 1 (False) Inner loop ends Output: 1
3 2 1 1 <= 2 (True) 1 * 1 1
4 2 2 2 <= 2 (True) 1 * 2 2
5 2 3 3 <= 2 (False) Inner loop ends Output: 2
6 3 1 1 <= 3 (True) 1 * 1 1
7 3 2 2 <= 3 (True) 1 * 2 2
8 3 3 3 <= 3 (True) 2 * 3 6
9 3 4 4 <= 3 (False) Inner loop ends Output: 6

Script का Actual Output:

=== Factorial Trace Demo ===
Number: 1 | Factorial: 1
Number: 2 | Factorial: 2
Number: 3 | Factorial: 6

Students की Common Mistakes (सावधानियां)

Linux exams और practical lab viva में छात्र अक्सर ये गलतियां करते हैं:

  1. Brackets के अंदर space न छोड़ना: Shell script में if [$a -eq $b] लिखना syntax error देता है। सही तरीका if [ $a -eq $b ] है, यानी brackets के दोनों तरफ space होना अनिवार्य है।
  2. Numbers की तुलना में == का उपयोग: Numeric values के लिए == या > का इस्तेमाल shell script के standard test command में नहीं होता। Numbers के लिए हमेशा -eq, -gt, -lt का प्रयोग करें।
  3. Closing tags भूलना: if शुरू करके fi से बंद करना, case को esac से बंद करना और loops (for, while, until) को do से शुरू करके done से बंद करना भूल जाना।
  4. Variable में Dollar Sign ($) न लगाना: Variable में value assign करते समय x=10 लिखा जाता है, लेकिन value read करते समय $x लिखना ज़रूरी है। Condition में [ x -gt 5 ] लिखने से string literal 'x' compare होने लगता है।
  5. Case statement में डबल सेमीकोलन (;;) छोड़ना: हर case option के command block के बाद ;; लगाना ज़रूरी है, नहीं तो script अगला pattern भी check करने की कोशिश में syntax error देगी।

Exam में कैसे लिखें (5 Marks Answer)

यूनिवर्सिटी परीक्षा (MDSU / RTU / RU) में "Explain Decision Making and Looping in Linux Shell Scripting" सवाल आने पर उत्तर इस structure में लिखें:

  • Definition (1 mark): Linux shell scripting में program के flow को control करने के लिए decision making (if, else, case) और looping (for, while, until) structures का उपयोग किया जाता है।
  • Decision Making Statements (2 marks):
    • if-then-fi: Single condition test के लिए।
    • if-then-else-fi: True और False दोनों outcomes handle करने के लिए।
    • elif: Multiple branching conditions के लिए।
    • case-in-esac: Menu selection और multi-value pattern match के लिए।
  • Looping Statements (2 marks):
    • for loop: Fixed list, range या file collection पर iteration के लिए। Block: do ... done।
    • while loop: जब तक test condition True रहे तब तक commands execute करने के लिए।
    • until loop: जब तक test condition False रहे तब तक commands execute करने के लिए (True होते ही exit)।
  • Syntax Example: एक छोटा code snippet (जैसे for i in {1..5}; do echo $i; done) ज़रूर बनाएं।

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. Linux में while और until loop में मुख्य अंतर क्या है? (while loop condition True रहने पर चलता है, जबकि until loop condition False रहने तक चलता है और True होते ही रुक जाता है)
  2. Linux script में numeric comparison के लिए -eq और -ne का क्या मतलब है? (-eq = equal to, -ne = not equal to)
  3. Bash scripting में case statement किस keyword से बंद होता है? (esac keyword से)
  4. Shell scripting में break और continue commands का क्या काम है? (break loop को तुरंत समाप्त करता है, continue current iteration छोड़ कर अगले पर जाता है)
  5. Bracket condition [ $a -gt 10 ] में spaces का क्या महत्व है? (Square bracket वास्तव में test command का alias है, इसलिए brackets और arguments के बीच space होना अनिवार्य है)

Long Questions (5–10 marks)

  1. Linux shell scripting में conditional statements क्या हैं? if, if-else, elif और case statements को syntax और code example के साथ detail में समझाइए।
  2. Linux में for, while और until loops के काम करने के तरीके (working) की तुलना कीजिए। किसी directory की सभी text files को read करने के लिए एक bash script लिखिए।
  3. Shell script में operators कितने प्रकार के होते हैं? File test operators, relational operators और string operators को उदाहरण के साथ समझाइए।

FAQs

Looping hindi meaning क्या है?

Looping का हिंदी अर्थ है किसी काम को बार-बार दोहराना (पुनरावृत्ति)। Programming और Linux में एक ही command set को condition पूरी होने तक लगातार चलाना looping कहलाता है।

Decision making in Hindi क्या होता है?

Decision making का अर्थ है निर्णय लेना। Shell script में जब script किसी दी गई शर्त (condition) की जांच करके यह तय करती है कि कौन सा block execute करना है, तो उसे decision making कहते हैं।

for loop in Linux को range के साथ कैसे चलाते हैं?

Bash में range के लिए curly braces syntax {start..end} इस्तेमाल किया जाता है। जैसे for i in {1..10}; do echo $i; done लिखने से 1 से 10 तक संख्याएं print हो जाती हैं।

Linux shell script में infinite loop कैसे बनाया जाता है?

Infinite loop बनाने के लिए while loop में : (null command जो हमेशा 0 return करता है) या true दिया जाता है, जैसे: while :; do echo "Running"; sleep 1; done। इसे रोकने के लिए terminal पर Ctrl + C दबाते हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: Linux shell scripting में Decision Making का मतलब conditions के आधार पर फैसला लेना है (जैसे if, if-else, elif और case statements), जिससे तय होता है कि कौन सा command block run होगा।
Operating System (OS) Notes in Hindi | ऑपरेटिंग सिस्टम नोट्स के पाठ्यक्रम में Looping & Decision Making in Hindi एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Looping & Decision Making in Hindi के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement

In this Chapter

Looping & Decision Making in Hindi
Distributed System in Hindi
Real Time System in Hindi
System Calls in Hindi
System Program in Hindi
Structure of Operating System in Hindi
Layers Design of Structure in Hindi
Unix in Hindi
Virtual Machine in Hindi
Kernel-Based Operating System in Hindi
Process in Hindi – OS में Process क्या है
Interacting Processes in Hindi
Threads in Hindi
Fundamental of Schedulling in Hindi
Scheduling Criteria in Hindi
Long Medium Short Term Scheduling in Hindi
Scheduling Algorithms in Hindi
Structure of Concurrent System in Hindi
Critical Region in Hindi
Critical Section in Hindi
Inter process Communication in Hindi
Monitor in Hindi
Semaphore in OS in Hindi – Counting और Binary Semaphore
Semaphores Implementation & Uses in Hindi
Logical and Physical Address in Hindi
Swapping in Hindi
Contiguous Allocation in Hindi
Segmentation in Hindi
Paging in OS in Hindi – Paging और Segmentation
Paging with Segmentation in Hindi
kernel Memory Allocation in Hindi
Page Replacement Algorithms in Hindi
Virtual Memory in Hindi
Virtual Memory with demand Paging in Hindi
Deadlock & Characterization in Hindi
Methods for Handling Deadlocks in Hindi
Deadlock Thrashing in Hindi
Allocation of Frames in Hindi
Virtual Memory using Segmentation in Hindi
Interprocess Communication protocol in Hindi
Network Operating System in Hindi
Design Issues in Distributed File System in Hindi
Network Structure in Hindi
Structure Distributed System in Hindi
File System and Coordination in Hindi
Linux in Hindi
Commands of Linux in Hindi | लिनक्स के कमांड्स हिंदी में
Programmer Interface & File manipulation in Hindi
Process control in Linux in Hindi
Linux Kernel & Signals in Hindi
File System in Linux in Hindi
block & inodes in Hindi
System Editor in Hindi
Character Transliteration in Hindi
ED in Hindi
Vi Editor & There Commands in Hindi
Shell Script in Hindi
Variables & File name Expansion in Hindi
Array in Hindi OS
Sub Program in Hindi
C interface with Linux in Hindi
Simpel Shell Programs in Hindi
BCA Part-1 | Operating Systems | Semester-I | MDSU Exam Paper 2023 (Held in 2024)
What is Operating System (OS) in Hindi
Operating System (OS) All Important Questions and Answers in Hindi (MDSU)
BCA || Operating System 2025 Paper || MDSU Exam Paper
BCA | OS(Operating System) 2023 Paper | MDSU Exam Paper
Type of Operating System in Hindi
Goals of Operating System in Hindi
Operation of Operating system in Hindi
Resource Allocation & Functions in Hindi
class of Operating system in Hindi
Batch Processing in Hindi
Multi Processing in Hindi
Time Sharing in Hindi