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)
- Which loop guarantees execution at least once?
(a) For | (b) While | (c) Do-While | (d) None - 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 - 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 - 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 - 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 - Which of these loops is entry-controlled?
(a) For Loop | (b) While Loop | (c) Both (a) and (b) | (d) Do-While Loop - 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 - 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 - 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 - 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
- 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. - Write the syntax for a For loop in C++.
Answer:for (initialization; condition; increment/decrement) { // Code to execute } - 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. - 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. - 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
- Explain the types of loops in C++ with examples.
- Write a program to print the multiplication table of a number using a loop.
- Discuss the importance of loops in programming with practical examples.
Post a Comment