Nested loops are those loops that are present inside another loop. In C#, nesting of for, while, and do-while loops are allowed and you can also put any nested loop inside any other type of loop like in a for loop you are allowed to put nested if loop.
for Loop: The functionality of for loop is quite similar to while loop. It is basically used when the number of times loop statements are to be executed is known beforehand. Nesting of for loop is allowed, which means you can use for loop inside another for loop.
Syntax:
for(variable initialization; testing condition; increment / decrement)
{
for(variable initialization; testing condition;
increment / decrement)
{
// Statements
}
}
Example:
C#
// C# program to illustrate nested for loop
using System;
class GFG{
public static void Main()
{
// for loop within another for loop
// printing GeeksforGeeks
for(int i = 0; i < 4; i++)
for(int j = 1; j < i; j++)
Console.WriteLine("GeeksforGeeks!!");
}
}
Output:
GeeksforGeeks!!
GeeksforGeeks!!
GeeksforGeeks!!
while Loop: In a while loop, the test condition is given at the beginning of the loop, and all statements are executed till the given boolean condition satisfies and when the condition becomes false, the control will be out from the while loop. Nesting of a while loop is allowed, which means you can use while loop inside another while loop. However, it is not recommended to use nested while loop because it is difficult to maintain and debug.
Syntax:
while(condition)
{
while(condition)
{
// Statements
}
// Statements
}
Example:
C#
// C# program to illustrate nested while loop
using System;
class GFG{
public static void Main()
{
int x = 1, y = 2;
while (x < 4)
{
Console.WriteLine("Outer loop = {0}", x);
x++;
while (y < 4)
{
Console.WriteLine("Inner loop = {0}", y);
y++;
}
}
}
}
Output:
Outer loop = 1
Inner loop = 2
Inner loop = 3
Outer loop = 2
Outer loop = 3
do-while Loop: In C#, the do-while loop is similar to the while loop with the only difference that is, it checks the condition after executing the statements. Nesting of a do-while loop is allowed, which means you can use a do-while loop inside another do-while loop.
Syntax:
do
{
// Statements
do
{
// Statements
}
while(condition);
}
while(condition);
Example:
C#
// C# program to illustrate nested do-while loop
using System;
class GFG{
public static void Main()
{
int x = 1;
do
{
Console.WriteLine("Outer loop = {0}", x);
int y = x;
x++;
do
{
Console.WriteLine("Inner loop: {0}", y);
y++;
} while (y < 4);
} while (x < 4);
}
}
Output:
Outer loop = 1
Inner loop: 1
Inner loop: 2
Inner loop: 3
Outer loop = 2
Inner loop: 2
Inner loop: 3
Outer loop = 3
Inner loop: 3
Similar Reads
C# Loops Looping in a programming language is a way to execute a statement or a set of statements multiple times, depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops.Types of Loops in C#Loops are mainly divided
4 min read
Nested Loops in C with Examples A nested loop means a loop statement inside another loop statement. That is why nested loops are also called "loop inside loops". We can define any number of loops inside another loop.For a nested loop, the inner loop performs all of its iterations for each iteration of the outer loop. Example: If t
6 min read
Output of C programs | Set 61 (Loops) Prerequisite : Loops in C Q.1 What is the output of this program? CPP #include <iostream> using namespace std; int main() { int i, j, var = 'A'; for (i = 5; i >= 1; i--) { for (j = 0; j < i; j++) printf("%c ", (var + j)); printf("\n"); } return 0; } Options a)A B C D
3 min read
Entry Controlled Loops in Programming Entry controlled loops in programming languages allow repetitive execution of a block of code based on a condition that is checked before entering the loop. In this article, we will learn about entry controlled loops, their types, syntax, and usage across various popular programming languages. What
6 min read
C for Loop In C programming, the 'for' loop is a control flow statement that is used to repeatedly execute a block of code as many times as instructed. It uses a variable (loop variable) whose value is used to decide the number of repetitions. It is commonly used to iterate over a sequence such as an array or
4 min read