while Loop in C++ – Syntax, Working and Examples with Output | C++ में while लूप – सिंटैक्स एवं उदाहरण

A while loop is an entry-controlled loop in C++. It repeatedly executes a block of statements as long as the given condition is true. Unlike a for loop, a while loop contains only the condition in its header. Initialization is generally performed before the loop, and updation is written inside the loop body.

C++ में while loop एक Entry-Controlled Loop है। यह दिए गए statements को तब तक बार-बार execute करता है, जब तक दी गई condition सत्य होती है। for loop के विपरीत, while loop के header में केवल condition लिखी जाती है। Initialization सामान्यतः लूप से पहले तथा Updation लूप के अंदर किया जाता है।

A while loop is especially useful when the number of iterations is not known in advance and depends on a condition.

while loop का उपयोग विशेष रूप से तब किया जाता है, जब प्रोग्रामर को पहले से यह पता न हो कि लूप कितनी बार execute होगा और इसकी repetitions किसी condition पर निर्भर करती हों।

Syntax of while Loop (व्हाइल लूप का सिंटैक्स)

initialization;
while (condition)
{
statements;
updation;
}

Parts of while loop (व्हाइल लूप के भाग):

  1. Initialization: It initializes the loop control variable before the loop starts.
    Initialization: इसमें लूप शुरू होने से पहले loop control variable को प्रारंभिक value दी जाती है।

  2. Condition: It is checked before every iteration. If the condition is true, the loop body executes; otherwise, the loop terminates.
    Condition: इसे प्रत्येक iteration से पहले check किया जाता है। Condition true होने पर लूप execute होता है, अन्यथा लूप समाप्त हो जाता है।

  3. Updation: It changes the value of the loop control variable inside the loop body.
    Updation: इसमें लूप के अंदर loop control variable की value को बदला जाता है।

Working of while Loop (व्हाइल लूप की कार्यप्रणाली)

The execution of a while loop follows these steps:

while loop का execution निम्नलिखित चरणों में होता है:

  • Initialization is performed before the loop.

  • The condition is checked.

  • If the condition is true, the loop body is executed.

  • The updation statement is executed.

  • The condition is checked again. This process continues until the condition becomes false.

  • सबसे पहले लूप के बाहर Initialization किया जाता है।

  • इसके बाद Condition check होती है।

  • यदि Condition true है, तो लूप के statements execute होते हैं।

  • इसके बाद Updation statement execute होता है।

  • Condition दोबारा check की जाती है। यह प्रक्रिया Condition false होने तक चलती रहती है।

Example 1: C++ Program to Print the Table of a Number Using while Loop

इस उदाहरण में while loop का उपयोग करके किसी संख्या का पहाड़ा 1 से 10 तक print किया गया है।

#include <iostream>
using namespace std;
int main()
{
int i, n;
cout << "Enter a Number: ";
cin >> n;
cout << "Table of " << n << endl;
i = 1;
while (i <= 10)
{
cout << n << " x " << i << " = " << n * i << endl;
i++;
}
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 (व्याख्या):

  • i = 1 initializes the loop control variable.

  • i <= 10 checks the loop condition.

  • i++ increases the value of i by 1 after each iteration.

  • The loop terminates when i becomes 11.

  • i = 1 लूप control variable को initialize करता है।

  • i <= 10 लूप की condition check करता है।

  • i++ प्रत्येक iteration के बाद i की value को 1 बढ़ाता है।

  • जब i की value 11 हो जाती है, तब लूप समाप्त हो जाता है।

Example 2: C++ Program for Counting from 1 to 100 Using while Loop

इस उदाहरण में while loop की सहायता से 1 से 100 तक की counting print की गई है। setw() manipulator का उपयोग numbers को व्यवस्थित रूप से प्रदर्शित करने के लिए किया गया है।

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i = 1;
cout << "Counting from 1 to 100" << endl;
while (i <= 100)
{
cout << setw(3) << i << " ";
if (i % 10 == 0)
{
cout << endl;
}
i++;
}
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 (व्याख्या):

  • int i = 1 initializes the counting variable.

  • i <= 100 allows the loop to execute while i is less than or equal to 100.

  • setw(3) sets the minimum field width to 3 characters for the next output value.

  • i % 10 == 0 checks whether the current number is divisible by 10. If true, the next number is printed on a new line.

  • i++ increments the counting variable.

  • int i = 1 counting variable को initialize करता है।

  • i <= 100 condition के true रहने तक लूप execute होता है।

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

  • i % 10 == 0 check करता है कि वर्तमान संख्या 10 से divisible है या नहीं। यदि condition true होती है, तो अगली संख्या नई line में print होती है।

  • i++ counting variable की value को 1 बढ़ाता है।

Difference Between while and for Loop (while और for लूप में अंतर)

Basiswhile Loopfor Loop
StructureContains the condition in its header.Contains initialization, condition and updation in its header.
InitializationGenerally written before the loop.Generally written in the loop header.
UpdationGenerally written inside the loop body.Generally written in the loop header.
UseUseful when the number of iterations is not known in advance.Useful when the number of iterations is known in advance.


आधार

while Loop

for Loop
संरचनाHeader में केवल condition होती है।Header में initialization, condition और updation होते हैं।
Initializationसामान्यतः लूप से पहले लिखा जाता है।सामान्यतः लूप के header में लिखा जाता है।
Updationसामान्यतः लूप के अंदर लिखा जाता है।सामान्यतः लूप के header में लिखा जाता है।
उपयोगजब iterations की संख्या पहले से ज्ञात न हो।जब iterations की संख्या पहले से ज्ञात हो।

Infinite while Loop (अनंत व्हाइल लूप)

If the condition of a while loop always remains true, the loop continues indefinitely. Such a loop is called an infinite while loop.

यदि while loop की condition हमेशा true रहती है, तो लूप लगातार execute होता रहता है। ऐसे लूप को Infinite while Loop कहते हैं।

Example:

while (true)
{
cout << "Hello ";
}

This loop continuously prints Hello because its condition is always true. It can be terminated using an appropriate control mechanism, such as break.

यह लूप लगातार Hello print करता रहता है क्योंकि इसकी condition हमेशा true रहती है। इसे break जैसे control mechanism से terminate किया जा सकता है।

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)