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 41: Loops in C++ – For, While, and Do-While Loops | Class 11

Learn about loops in C++ in Day 41 of Class 11 Computer Science. Covers for, while, and do-while loops with examples.

Day 41 – Chapter 5: Loops in C++

Class 11 Computer Science

What are Loops in C++? (C++ में Loops क्या हैं?)

Loops in C++ are used to repeatedly execute a block of code as long as a specified condition is true. They help avoid writing redundant code.
C++ में Loops का उपयोग तब तक कोड के ब्लॉक को बार-बार निष्पादित करने के लिए किया जाता है जब तक कि निर्दिष्ट शर्त सही हो। ये बार-बार कोड लिखने से बचाते हैं।

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

  • For Loop (For लूप): Used when the number of iterations is known.
    Syntax:
    for (initialization; condition; increment/decrement) {
        // Code to execute
    }
    
    Example:
    for (int i = 0; i < 5; i++) {
        cout << i << endl;
    }
    
  • While Loop (While लूप): Used when the condition is checked before each iteration.
    Syntax:
    while (condition) {
        // Code to execute
    }
    
    Example:
    int i = 0;
    while (i < 5) {
        cout << i << endl;
        i++;
    }
    
  • Do-While Loop (Do-While लूप): Used when the condition is checked after executing the loop body at least once.
    Syntax:
    do {
        // Code to execute
    } while (condition);
    
    Example:
    int i = 0;
    do {
        cout << i << endl;
        i++;
    } while (i < 5);
    

Key Differences Between Loops (Loops के बीच मुख्य अंतर)

Feature For Loop While Loop Do-While Loop
Condition Check Before each iteration Before each iteration After executing the loop body
Execution Executes 0 or more times Executes 0 or more times Executes at least once
Usage Used when iterations are fixed Used when iterations depend on a condition Used when loop body must execute at least once

Examples of Using Loops in C++ (Loops के उपयोग के उदाहरण)

Example 1: Sum of First N Numbers Using For Loop

#include <iostream>
using namespace std;

int main() {
    int n, sum = 0;
    cout << "Enter a number: ";
    cin >> n;

    for (int i = 1; i <= n; i++) {
        sum += i;
    }

    cout << "Sum of first " << n << " numbers is " << sum << endl;
    return 0;
}

Example 2: Print Numbers Using While Loop

#include <iostream>
using namespace std;

int main() {
    int i = 1, n;
    cout << "Enter a number: ";
    cin >> n;

    while (i <= n) {
        cout << i << " ";
        i++;
    }

    return 0;
}

Example 3: Display Menu Using Do-While Loop

#include <iostream>
using namespace std;

int main() {
    int choice;
    do {
        cout << "1. Add" << endl;
        cout << "2. Subtract" << endl;
        cout << "3. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;
    } while (choice != 3);

    cout << "Exiting program..." << endl;
    return 0;
}

Practice Questions

Multiple Choice Questions (MCQs)

  1. Which loop guarantees execution at least once?
    (a) For | (b) While | (c) Do-While | (d) None
  2. What is the output of the following code?
    for (int i = 0; i < 3; i++) {
        cout << i;
    }
            
    (a) 0 1 2 | (b) 1 2 3 | (c) 0 1 2 3 | (d) None
  3. Which of the following checks the condition before executing the loop body?
    (a) For Loop | (b) While Loop | (c) Both (a) and (b) | (d) None
  4. What happens if the condition in a while loop is never true?
    (a) Loop executes infinitely | (b) Loop doesn't execute | (c) Error | (d) None
  5. What is the output of the following code?
    int i = 0;
    do {
        cout << i << endl;
        i++;
    } while (i < 3);
            
    (a) 0 1 2 | (b) 1 2 3 | (c) 0 1 2 3 | (d) None
  6. Which of these loops is entry-controlled?
    (a) For Loop | (b) While Loop | (c) Both (a) and (b) | (d) Do-While Loop
  7. What is the correct syntax for a for loop?
    (a) for (initialization; condition; update) | (b) for (condition; update; initialization) | (c) for (initialization; update; condition) | (d) None
  8. What does the break statement do in a loop?
    (a) Ends the loop immediately | (b) Skips the current iteration | (c) Executes the loop body again | (d) None
  9. What is the purpose of the continue statement?
    (a) Skips to the next iteration | (b) Ends the loop immediately | (c) Executes the loop body again | (d) None
  10. Which loop is used for fixed iterations?
    (a) For | (b) While | (c) Do-While | (d) None

Answers to MCQs:

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

Short Answer Questions

  1. What is the difference between a While loop and a Do-While loop?
    Answer: While checks the condition before the loop body executes, while Do-While checks it after the loop body executes at least once.
  2. Write the syntax for a For loop in C++.
    Answer:
    for (initialization; condition; increment/decrement) {
        // Code to execute
    }
            
  3. What is the purpose of the break statement in a loop?
    Answer: The break statement immediately terminates the loop execution and exits the loop body.
  4. Explain the use of the continue statement in a loop.
    Answer: The continue statement skips the current iteration and proceeds to the next iteration of the loop.
  5. Write a program to calculate the sum of the first 10 natural numbers using a For loop.
    Answer:
    #include <iostream>
    using namespace std;
    
    int main() {
        int sum = 0;
        for (int i = 1; i <= 10; i++) {
            sum += i;
        }
        cout << "Sum of first 10 natural numbers is: " << sum << endl;
        return 0;
    }
            

Long Answer Questions

  1. Explain the types of loops in C++ with examples.
  2. Write a program to print the multiplication table of a number using a loop.
  3. Discuss the importance of loops in programming with practical examples.

Post a Comment