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

A nested loop in C++ means using one loop inside another loop. The loop written inside another loop is called the inner loop, while the loop that contains it is called the outer loop. Nested loops are commonly used to perform repeated operations involving rows and columns, display patterns, generate tables, and work with multidimensional arrays.

C++ में Nested Loop का अर्थ है एक loop के अंदर दूसरे loop का उपयोग करना। किसी loop के अंदर लिखे गए loop को Inner Loop तथा जिस loop के अंदर दूसरा loop लिखा जाता है, उसे Outer Loop कहते हैं। Nested Loops का उपयोग सामान्यतः rows और columns से संबंधित operations, patterns print करने, tables बनाने तथा multidimensional arrays पर कार्य करने के लिए किया जाता है।

Syntax of Nested Loops (नेस्टेड लूप का सिंटैक्स)

A nested loop can be created using any combination of for, while, and do-while loops.

C++ में for, while और do-while loops के किसी भी combination का उपयोग करके Nested Loop बनाया जा सकता है।

Syntax using for loop:

for (initialization1; condition1; updation1)
{
// Outer loop statements
for (initialization2; condition2; updation2)
{
// Inner loop statements
}
}

Working of Nested Loops (नेस्टेड लूप की कार्यप्रणाली)

The working of nested loops follows these steps:

Nested Loops की कार्यप्रणाली निम्नलिखित चरणों पर आधारित होती है:

  • The outer loop starts its execution.

  • When the outer loop enters its body, the inner loop starts executing.

  • The inner loop completes all its iterations for the current outer loop iteration.

  • After the inner loop finishes, the outer loop moves to its next iteration.

  • The inner loop executes again for each new iteration of the outer loop.

  • सबसे पहले Outer Loop का execution शुरू होता है।

  • जब Outer Loop अपने body में प्रवेश करता है, तब Inner Loop execute होना शुरू होता है।

  • Inner Loop, Outer Loop की वर्तमान iteration के लिए अपनी सभी iterations पूरी करता है।

  • Inner Loop समाप्त होने के बाद Outer Loop की अगली iteration शुरू होती है।

  • Outer Loop की प्रत्येक नई iteration के लिए Inner Loop दोबारा execute होता है।

Number of iterations: If the outer loop executes m times and the inner loop executes n times during each outer iteration, the inner loop body executes a total of m × n times.

Iterations की संख्या: यदि Outer Loop m बार और प्रत्येक Outer Loop iteration के दौरान Inner Loop n बार execute होता है, तो Inner Loop का body कुल m × n बार execute होता है।

Example 1: C++ Program to Print a Rectangle Using Nested for Loop

इस उदाहरण में Nested for Loop का उपयोग करके 4 rows और 5 columns का rectangle pattern print किया गया है।

#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 5; j++)
{
cout << "* ";
}
cout << endl;
}
return 0;
}

Output:

* * * * *
* * * * *
* * * * *
* * * * *

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

  • The outer loop variable i controls the rows and executes 4 times.

  • The inner loop variable j controls the columns and executes 5 times for each outer iteration.

  • cout << endl moves the output to the next line after each row.

  • Therefore, a rectangle of 4 rows and 5 columns is displayed.

  • Outer Loop का variable i rows को control करता है और 4 बार execute होता है।

  • Inner Loop का variable j columns को control करता है और प्रत्येक Outer Loop iteration के लिए 5 बार execute होता है।

  • cout << endl प्रत्येक row के बाद output को अगली line में ले जाता है।

  • इस प्रकार 4 rows और 5 columns का rectangle प्रदर्शित होता है।

Example 2: C++ Program to Print a Right-Angled Triangle Using Nested Loop

इस उदाहरण में Nested for Loop की सहायता से right-angled triangle pattern print किया गया है।

#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
cout << "* ";
}
cout << endl;
}
return 0;
}

Output:

*
* *
* * *
* * * *
* * * * *

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

  • The outer loop controls the number of rows.

  • The inner loop executes according to the current value of i.

  • In each new row, the number of stars increases by one.

  • Outer Loop rows की संख्या को control करता है।

  • Inner Loop, i की वर्तमान value के अनुसार execute होता है।

  • प्रत्येक नई row में stars की संख्या एक बढ़ जाती है।

Example 3: C++ Program to Print Multiplication Tables Using Nested Loops

इस उदाहरण में Nested for Loop का उपयोग करके 1 से 3 तक की संख्याओं के multiplication tables print किए गए हैं।

#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 3; i++)
{
cout << "Table of " << i << endl;
for (int j = 1; j <= 10; j++)
{
cout << i << " x " << j << " = " << i * j << endl;
}
cout << endl;
}
return 0;
}

Output:

Table of 1
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10

Table of 2
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

Table of 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30

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

  • The outer loop selects the number whose table is to be printed.

  • The inner loop multiplies that number by values from 1 to 10.

  • The outer loop repeats the process for 1, 2, and 3.

  • Outer Loop उस संख्या का चयन करता है जिसका पहाड़ा print करना है।

  • Inner Loop उस संख्या को 1 से 10 तक की संख्याओं से multiply करता है।

  • Outer Loop यही प्रक्रिया 1, 2 और 3 के लिए दोहराता है।

Types of Nested Loops (नेस्टेड लूप के प्रकार)

Nested loops can be created using different combinations of loops:

Nested Loops को विभिन्न प्रकार के loops के combination से बनाया जा सकता है:

  1. Nested for Loop: A for loop inside another for loop.
    Nested for Loop: एक for loop के अंदर दूसरा for loop।

  2. Nested while Loop: A while loop inside another while loop.
    Nested while Loop: एक while loop के अंदर दूसरा while loop।

  3. Nested do-while Loop: A do-while loop inside another do-while loop.
    Nested do-while Loop: एक do-while loop के अंदर दूसरा do-while loop।

  4. Mixed Nested Loops: Different types of loops used inside one another, such as a for loop inside a while loop.
    Mixed Nested Loops: एक loop के अंदर दूसरे प्रकार के loop का उपयोग, जैसे while loop के अंदर for loop।

Nested loops are useful when a programming task requires repeated execution within another repeated operation. They are widely used in pattern printing, multiplication tables, matrix operations, and multidimensional array processing.

Nested Loops तब उपयोगी होते हैं जब किसी programming task में एक repetition के अंदर दूसरी repetition की आवश्यकता होती है। इनका उपयोग pattern printing, multiplication tables, matrix operations तथा multidimensional arrays पर कार्य करने में किया जाता है।

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)