Conditional Statements

Introduction

Programming में कई बार हमें decision लेना होता है—जैसे अगर condition सही हो तो एक काम करें, और गलत हो तो दूसरा।

सोचिए—अगर student के marks 40 से ज्यादा हों तो pass, नहीं तो fail। यह decision कैसे होगा?

यहीं पर Conditional Statements काम आते हैं। ये conditions के आधार पर program का flow control करते हैं।

Web Development में form validation, login system, result check आदि में इनका बहुत उपयोग होता है।

Definition

Conditional Statements ऐसे statements होते हैं जो किसी condition के आधार पर अलग-अलग actions perform करते हैं।

Concept

1. if Statement

जब condition true होती है, तभी code execute होता है।

<script>
let age = 18;

if(age >= 18) {
console.log("You are eligible to vote");
}
</script>

Output:
You are eligible to vote

2. if…else Statement

जब condition true हो तो एक block चलेगा, वरना दूसरा।

<script>
let marks = 35;

if(marks >= 40) {
console.log("Pass");
} else {
console.log("Fail");
}
</script>

Output:
Fail

3. if…else if…else

जब multiple conditions check करनी हों।

<script>
let marks = 75;

if(marks >= 80) {
console.log("Grade A");
}
else if(marks >= 60) {
console.log("Grade B");
}
else {
console.log("Grade C");
}
</script>

Output:
Grade B

4. Nested if

जब एक if के अंदर दूसरा if हो।

<script>
let age = 20;
let hasID = true;

if(age >= 18) {
if(hasID) {
console.log("Entry Allowed");
}
}
</script>

Output:
Entry Allowed

5. Switch Statement

जब multiple cases हों, तब switch use होता है।

<script>
let day = 2;

switch(day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
default:
console.log("Invalid Day");
}
</script>

Output:
Tuesday

Real-Life Example

मान लीजिए:

अगर user सही password डालता है → login
अगर गलत डालता है → error

ठीक इसी तरह conditional statements decision लेने में मदद करते हैं।

Important Points

Conditional statements decision लेने के लिए use होते हैं
if, else, else if सबसे common हैं
Switch multiple conditions के लिए use होता है
Nested if complex logic में use होता है
Conditions true/false पर आधारित होती हैं

निष्कर्ष

Conditional Statements JavaScript का एक महत्वपूर्ण हिस्सा हैं, जो program को intelligent बनाते हैं। इनके माध्यम से हम conditions के आधार पर अलग-अलग actions perform कर सकते हैं, जो real-world applications में बहुत उपयोगी है।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top