🟡 Control Flow  ·  Lesson 15

if, else-if and else Statements

Making decisions

Programs constantly need to choose what to do based on conditions — is the user an adult, did they pass, is the number positive? C++ handles this with the if family of statements. A condition is any expression that is true or false.

The if statement

The simplest form runs a block only when the condition is true:

C++
int age = 20;
if (age >= 18) {
    cout << "You are an adult.\n";
}
Output:
You are an adult.

if-else

Add an else to handle the false case, so exactly one block always runs:

C++
int n = 7;
if (n % 2 == 0) {
    cout << "Even\n";
} else {
    cout << "Odd\n";
}
Output:
Odd

The else-if ladder

To pick among several ranges, chain conditions with else if. C++ checks them top to bottom and stops at the first true one:

C++
int marks = 82;
if (marks >= 90) {
    cout << "Grade A\n";
} else if (marks >= 75) {
    cout << "Grade B\n";
} else if (marks >= 50) {
    cout << "Grade C\n";
} else {
    cout << "Fail\n";
}
Output:
Grade B

Nested if

You can put an if inside another to check a second condition only when the first holds:

C++
if (loggedIn) {
    if (isAdmin) {
        cout << "Welcome, admin\n";
    } else {
        cout << "Welcome, user\n";
    }
}

Often a nested check can be written more cleanly with &&: if (loggedIn && isAdmin).

A quick note on the ternary

For a simple either/or that produces a value, the ternary operator is shorter:

C++
string result = (score >= 50) ? "Pass" : "Fail";

See the operators lesson for more on the ternary.

Common mistakes

  • Using = (assign) instead of == (compare) in the condition.
  • Forgetting braces, so only the first line belongs to the if.
  • Putting a stray semicolon after if (x)if (x); does nothing.
  • Ordering an else-if ladder wrongly so a broad condition hides narrower ones.
🏋️ Practice

Read a number and print whether it is positive, negative or zero using an if / else-if / else chain. Then read a year's marks and print a grade with the ladder above.

Summary

  • if runs a block when a condition is true.
  • if-else chooses between two blocks.
  • An else-if ladder picks among several ranges, top to bottom.
  • Nested if checks a second condition inside the first.
  • Always use braces, and use == (not =) to compare.

निर्णय लेना

Programs को लगातार conditions के आधार पर चुनना पड़ता है कि क्या करें — क्या user वयस्क है, क्या वह पास हुआ, क्या number धनात्मक है? C++ इसे if statements के परिवार से संभालता है। Condition कोई भी expression है जो true या false हो।

if statement

सबसे सरल रूप एक block को केवल तब चलाता है जब condition true हो:

C++
int age = 20;
if (age >= 18) {
    cout << "You are an adult.\n";
}
Output:
You are an adult.

if-else

False स्थिति संभालने को एक else जोड़ें, तो दोनों में से ठीक एक block हमेशा चलता है:

C++
int n = 7;
if (n % 2 == 0) {
    cout << "Even\n";
} else {
    cout << "Odd\n";
}
Output:
Odd

else-if ladder

कई ranges में से चुनने को conditions को else if से जोड़ें। C++ इन्हें ऊपर से नीचे जाँचता है और पहली true पर रुक जाता है:

C++
int marks = 82;
if (marks >= 90) {
    cout << "Grade A\n";
} else if (marks >= 75) {
    cout << "Grade B\n";
} else if (marks >= 50) {
    cout << "Grade C\n";
} else {
    cout << "Fail\n";
}
Output:
Grade B

Nested if

आप एक if को दूसरे के अंदर रख सकते हैं ताकि दूसरी condition केवल तब जाँची जाए जब पहली सही हो:

C++
if (loggedIn) {
    if (isAdmin) {
        cout << "Welcome, admin\n";
    } else {
        cout << "Welcome, user\n";
    }
}

अक्सर nested जाँच && से ज़्यादा साफ़ लिखी जा सकती है: if (loggedIn && isAdmin)

Ternary पर एक त्वरित नोट

एक सरल या/अथवा के लिए जो value देता है, ternary operator छोटा है:

C++
string result = (score >= 50) ? "Pass" : "Fail";

Ternary पर अधिक के लिए operators lesson देखें।

आम गलतियाँ

  • Condition में == (तुलना) की जगह = (assign) इस्तेमाल करना।
  • Braces भूलना, तो केवल पहली line if की होती है।
  • if (x) के बाद भटका semicolon रखना — if (x); कुछ नहीं करता।
  • Else-if ladder गलत क्रम में लगाना ताकि एक व्यापक condition संकरी वालों को छिपा दे।
🏋️ अभ्यास

एक number पढ़ें और if / else-if / else श्रृंखला से print करें कि वह धनात्मक, ऋणात्मक या शून्य है। फिर एक वर्ष के marks पढ़ें और ऊपर की ladder से grade print करें।

सारांश

  • if condition true होने पर एक block चलाता है।
  • if-else दो blocks के बीच चुनता है।
  • Else-if ladder कई ranges में से चुनती है, ऊपर से नीचे।
  • Nested if पहली के अंदर दूसरी condition जाँचता है।
  • हमेशा braces इस्तेमाल करें, और तुलना को == (न कि =)।

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

C++ में if statement क्या है?
if statement किसी code block को केवल तब चलाता है जब कोई condition true हो। आप if (condition) { ... } लिखते हैं, और braces के अंदर का code तब चलता है जब condition true हो; वरना छोड़ दिया जाता है।
C++ में if और if-else में क्या अंतर है?
सादा if अपना block तब चलाता है जब condition true हो और वरना कुछ नहीं करता। if-else एक else block जोड़ता है जो condition false होने पर चलता है, तो दोनों में से ठीक एक block हमेशा चलता है।
C++ में else-if ladder क्या है?
Else-if ladder कई conditions को else if से जोड़ता है, हर एक को क्रम में जाँचते हुए जब तक एक true न हो, फिर वह block चलाकर बाकी छोड़ देता है। एक वैकल्पिक अंतिम else उस स्थिति को संभालता है जहाँ कोई मेल न खाए। यह grading या range जाँच के लिए आदर्श है।
C++ में nested if क्या है?
Nested if एक if statement है जो किसी दूसरे if या else block के अंदर रखा जाता है। यह आपको दूसरी condition केवल पहली के संतुष्ट होने के बाद जाँचने देता है, जो एक से ज़्यादा कारक पर निर्भर निर्णयों के लिए उपयोगी है।
C++ में if के साथ braces क्यों इस्तेमाल करूँ?
Braces के बिना if केवल अगले एक statement को नियंत्रित करता है, जो बाद में एक और line जोड़ने पर bugs ला सकता है जब आप उसे if का हिस्सा मानें। हमेशा braces { } इस्तेमाल करना grouping स्पष्ट बनाता है और ये गलतियाँ रोकता है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।