Class 11-12

Class 11 Computer Science

BSEB 100-Day Study Plan
Notes, MCQs, and Solutions

Explore Class 11

Class 12 Computer Science

Advanced Topics & Practical Guide
Board Exam Preparation

Explore Class 12

Day 40: Conditional Statements in C++ – If, Else, and Switch | Class 11

Understand conditional statements in C++ in Day 40 of Class 11 Computer Science. Covers if, else, and switch with examples.

Day 40 – Chapter 5: Conditional Statements in C++

Class 11 Computer Science

What are Conditional Statements? (Conditional Statements क्या हैं?)

Conditional statements allow a program to make decisions based on certain conditions. These statements execute different blocks of code depending on whether the condition evaluates to true or false.
Conditional Statements प्रोग्राम को कुछ शर्तों के आधार पर निर्णय लेने की अनुमति देते हैं। ये शर्त सही या गलत होने पर विभिन्न कोड ब्लॉक निष्पादित करते हैं।

Types of Conditional Statements in C++ (C++ में Conditional Statements के प्रकार)

  • If Statement (If स्टेटमेंट): Executes a block of code if the condition is true.
    Syntax:
    
    if (condition) {
    
        // Code to execute if condition is true
    
    }
    
    
  • If-Else Statement (If-Else स्टेटमेंट): Executes one block of code if the condition is true, and another block if the condition is false.
    Syntax:
    
    if (condition) {
    
        // Code if condition is true
    
    } else {
    
        // Code if condition is false
    
    }
    
    
  • Else If Ladder (Else If सीढ़ी): Checks multiple conditions sequentially.
    Syntax:
    
    if (condition1) {
    
        // Code for condition1
    
    } else if (condition2) {
    
        // Code for condition2
    
    } else {
    
        // Code if none of the conditions are true
    
    }
    
    
  • Switch Statement (Switch स्टेटमेंट): Tests the value of a variable against multiple cases and executes the matching case.
    Syntax:
    
    switch (variable) {
    
        case value1:
    
            // Code for value1
    
            break;
    
        case value2:
    
            // Code for value2
    
            break;
    
        default:
    
            // Code if no case matches
    
    }
    
    

Examples of Conditional Statements (Conditional Statements के उदाहरण)

Example 1: Using If-Else


#include <iostream>

using namespace std;

int main() {

    int age;

    cout << "Enter your age: ";

    cin >> age;

    if (age >= 18) {

        cout << "You are eligible to vote." << endl;

    } else {

        cout << "You are not eligible to vote." << endl;

    }

    return 0;

}

Example 2: Using Switch


#include <iostream>

using namespace std;

int main() {

    int choice;

    cout << "Enter a number (1-3): ";

    cin >> choice;

    switch (choice) {

        case 1:

            cout << "You selected One.";

            break;

        case 2:

            cout << "You selected Two.";

            break;

        case 3:

            cout << "You selected Three.";

            break;

        default:

            cout << "Invalid choice.";

    }

    return 0;

}

Practice Questions

Multiple Choice Questions (MCQs)

  1. Which statement is used to execute a block of code based on a condition?
    (a) loop | (b) if | (c) else | (d) switch
  2. What will happen if no case in a switch statement matches?
    (a) The default case executes | (b) The program crashes | (c) No action is taken | (d) None
  3. What does the "break" statement do in a switch?
    (a) Exits the program | (b) Exits the switch block | (c) Continues to the next case | (d) None
  4. Which is used to test multiple conditions sequentially?
    (a) If-Else | (b) Else If Ladder | (c) Switch | (d) None
  5. What is the output of this code?
    
    int num = 5;
    
    if (num > 3) {
    
        cout << "Greater";
    
    } else {
    
        cout << "Smaller";
    
    }
    
            
    (a) Greater | (b) Smaller | (c) Error | (d) None
  6. What is mandatory in a switch statement?
    (a) Default | (b) Break | (c) Case | (d) None
  7. Which statement handles invalid cases in a switch?
    (a) If | (b) Else | (c) Default | (d) None
  8. What happens if the break statement is missing in a case?
    (a) Program crashes | (b) Fall-through occurs | (c) Error | (d) None
  9. What will this code output?
    
    int x = 10;
    
    if (x == 5) {
    
        cout << "Equal";
    
    } else {
    
        cout << "Not Equal";
    
    }
    
            
    (a) Equal | (b) Not Equal | (c) Error | (d) None
  10. Which statement allows you to test a variable's value against multiple cases?
    (a) If-Else | (b) Switch | (c) For | (d) None

Answers to MCQs:

1: (b), 2: (a), 3: (b), 4: (b), 5: (a), 6: (c), 7: (c), 8: (b), 9: (b), 10: (b)

Short Answer Questions

  1. What is the difference between If-Else and Switch statements?
    Answer: If-Else checks conditions sequentially, while Switch tests a variable's value against multiple cases.
  2. Write the syntax for a Switch statement in C++.
    Answer:
    
    switch (variable) {
    
        case value1:
    
            // Code for value1
    
            break;
    
        case value2:
    
            // Code for value2
    
            break;
    
        default:
    
            // Code if no case matches
    
    }
    
            
  3. What happens if the break statement is not used in a switch case?
    Answer: If the break statement is not used, fall-through occurs, and the execution continues to subsequent cases.
  4. What is the purpose of the default case in a switch statement?
    Answer: The default case handles situations when no case matches the variable's value.
  5. Write a program to check if a number is positive, negative, or zero using If-Else.
    Answer:
    
    #include <iostream>
    
    using namespace std;
    
    int main() {
    
        int num;
    
        cout << "Enter a number: ";
    
        cin >> num;
    
        if (num > 0) {
    
            cout << "Positive";
    
        } else if (num < 0) {
    
            cout << "Negative";
    
        } else {
    
            cout << "Zero";
    
        }
    
        return 0;
    
    }
    
            

Long Answer Questions

  1. Explain the types of conditional statements in C++ with examples.
  2. Write a program to demonstrate the use of If-Else and Switch statements.
  3. Discuss the importance of conditional statements in programming.

Post a Comment