for Loop in C++ – Syntax, Working and Examples with Output | C++ में for लूप – सिंटैक्स एवं उदाहरण
A for loop is an entry-controlled loop in C++. It is used to execute a block of statements repeatedly as long as the given condition is true. It contains three main parts: initialization, condition, and updation, which are written together and separated by semicolons (;).
C++ में for loop एक Entry-Controlled Loop है। इसका उपयोग किसी statement या statements के समूह को तब तक बार-बार execute करने के लिए किया जाता है, जब तक दी गई condition सत्य होती है। इसमें लूप के तीन मुख्य भाग होते हैं: Initialization, Condition और Updation, जिन्हें एक ही स्थान पर semicolon (;) द्वारा अलग करके लिखा जाता है।
A for loop is particularly useful when the number of iterations is known in advance. If its initialization and updation expressions are omitted, it can also work like a while loop.
जब प्रोग्रामर को पहले से पता हो कि लूप कितनी बार execute करना है, तब for loop विशेष रूप से उपयोगी होता है। यदि इसके initialization और updation expressions को हटा दिया जाए, तो यह while loop की तरह भी कार्य कर सकता है।
Syntax of for Loop (फॉर लूप का सिंटैक्स)
for (initialization; condition; updation){statements;}
Three parts of for loop (फॉर लूप के तीन भाग):
- Initialization: It initializes the loop control variable. It is executed only once at the beginning of the loop.Initialization: इसमें loop control variable को प्रारंभिक value दी जाती है। यह लूप के शुरू में केवल एक बार execute होता है।
- Condition: It checks whether the loop should continue. If the condition is true, the loop body executes; otherwise, the loop terminates.Condition: यह check करता है कि लूप continue होना चाहिए या नहीं। Condition true होने पर लूप के statements execute होते हैं, अन्यथा लूप समाप्त हो जाता है।
- Updation: It changes the value of the loop control variable after each iteration.Updation: यह प्रत्येक iteration के बाद loop control variable की value को बदलता है।
Working of for Loop (फॉर लूप की कार्यप्रणाली)
The execution of a for loop follows these steps:
for loop का execution निम्नलिखित चरणों में होता है:
Initialization is performed once.
The condition is checked.
If the condition is true, the loop body is executed.
The updation expression is executed.
The condition is checked again, and the process continues until the condition becomes false.
सबसे पहले Initialization execute होता है।
इसके बाद Condition check होती है।
यदि Condition true है, तो लूप के statements execute होते हैं।
इसके बाद Updation execute होता है।
फिर Condition दोबारा check होती है। यह प्रक्रिया Condition false होने तक चलती रहती है।
Example 1: C++ Program to Print the Table of a Number Using for Loop
इस उदाहरण में for loop का उपयोग करके किसी संख्या का पहाड़ा 1 से 10 तक print किया गया है।
#include <iostream>using namespace std;int main(){int n;cout << "Enter a Number: ";cin >> n;cout << "Table of " << n << endl;for (int i = 1; i <= 10; i++){cout << n << " x " << i << " = " << n * i << endl;}return 0;}
Output:
Enter a Number: 5Table of 55 x 1 = 55 x 2 = 105 x 3 = 155 x 4 = 205 x 5 = 255 x 6 = 305 x 7 = 355 x 8 = 405 x 9 = 455 x 10 = 50
Explanation (व्याख्या):
int i = 1initializes the loop variable.i <= 10checks the loop condition.i++increases the value ofiby 1 after each iteration.The loop executes 10 times and prints the multiplication table.
int i = 1लूप variable को initialize करता है।i <= 10लूप की condition check करता है।i++प्रत्येक iteration के बादiकी value को 1 बढ़ाता है।यह लूप 10 बार execute होकर पहाड़ा print करता है।
Example 2: C++ Program for Counting from 1 to 100 Using for Loop
इस उदाहरण में for loop की सहायता से 1 से 100 तक की counting print की गई है। setw() manipulator का उपयोग numbers को व्यवस्थित रूप से प्रदर्शित करने के लिए किया गया है।
#include <iostream>#include <iomanip>using namespace std;int main(){cout << "Counting from 1 to 100" << endl;for (int i = 1; i <= 100; i++){cout << setw(3) << i << " ";if (i % 10 == 0){cout << endl;}}return 0;}
Output:
Counting from 1 to 1001 2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 2021 22 23 24 25 26 27 28 29 3031 32 33 34 35 36 37 38 39 4041 42 43 44 45 46 47 48 49 5051 52 53 54 55 56 57 58 59 6061 62 63 64 65 66 67 68 69 7071 72 73 74 75 76 77 78 79 8081 82 83 84 85 86 87 88 89 9091 92 93 94 95 96 97 98 99 100
Explanation (व्याख्या):
setw(3)sets the minimum field width to 3 characters for the next output value.i % 10 == 0checks whether the number is divisible by 10.When the condition is true,
endlmoves the output to the next line.setw(3)अगले output value के लिए minimum field width 3 characters निर्धारित करता है।i % 10 == 0check करता है कि संख्या 10 से divisible है या नहीं।Condition true होने पर
endloutput को अगली line में ले जाता है।
Omitting Parts of for Loop (फॉर लूप के भागों को हटाना)
In C++, the initialization, condition, and updation expressions can be omitted individually from a for loop. However, the two semicolons must remain.
C++ में for loop के Initialization, Condition और Updation expressions को अलग-अलग हटाया जा सकता है। लेकिन दोनों semicolons (;) लिखना आवश्यक है।
Example:
int i = 1;for (; i <= 5; ){cout << i << " ";i++;}
Output:
1 2 3 4 5In this example, initialization is performed before the loop and updation is written inside the loop body. The loop works similarly to a while loop.
इस उदाहरण में Initialization को लूप से पहले और Updation को लूप के अंदर लिखा गया है। इस प्रकार यह while loop की तरह कार्य करता है।
Infinite for Loop (अनंत फॉर लूप)
If the condition is omitted from a for loop, it is treated as true. Therefore, the loop continues indefinitely unless it is terminated using a break statement or another control mechanism.
यदि for loop में condition को छोड़ दिया जाता है, तो उसे true माना जाता है। इसलिए लूप लगातार execute होता रहता है, जब तक उसे break statement या किसी अन्य control mechanism से terminate न किया जाए।
Syntax:
for (;;){// Statements}
Comments
Post a Comment