🟡 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.

Frequently Asked Questions

What is an if statement in C++?
An if statement runs a block of code only when a condition is true. You write if (condition) { ... }, and the code inside the braces runs if the condition evaluates to true; otherwise it is skipped.
What is the difference between if and if-else in C++?
A plain if runs its block when the condition is true and does nothing otherwise. An if-else adds an else block that runs when the condition is false, so exactly one of the two blocks always runs.
What is an else-if ladder in C++?
An else-if ladder chains several conditions with else if, checking each in order until one is true, then running that block and skipping the rest. An optional final else handles the case where none matched. It is ideal for grading or range checks.
What is a nested if in C++?
A nested if is an if statement placed inside another if or else block. It lets you check a second condition only after the first has been satisfied, which is useful for decisions that depend on more than one factor.
Why should I use braces with if in C++?
Without braces an if controls only the single next statement, which can lead to bugs when you later add another line expecting it to be part of the if. Always using braces { } makes the grouping explicit and prevents these mistakes.

निर्णय लेना

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 इस्तेमाल करें, और तुलना को == (न कि =)।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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