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 (फॉर लूप के तीन भाग):

  1. Initialization: It initializes the loop control variable. It is executed only once at the beginning of the loop.
    Initialization: इसमें loop control variable को प्रारंभिक value दी जाती है। यह लूप के शुरू में केवल एक बार execute होता है।

  2. 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 होते हैं, अन्यथा लूप समाप्त हो जाता है।

  3. 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: 5
Table of 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Explanation (व्याख्या):

  • int i = 1 initializes the loop variable.

  • i <= 10 checks the loop condition.

  • i++ increases the value of i by 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 100
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 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 == 0 checks whether the number is divisible by 10.

  • When the condition is true, endl moves the output to the next line.

  • setw(3) अगले output value के लिए minimum field width 3 characters निर्धारित करता है।

  • i % 10 == 0 check करता है कि संख्या 10 से divisible है या नहीं।

  • Condition true होने पर endl output को अगली 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 5

In 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

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)