
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# OOP & Data Handling
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
C# - Nested Loops
C# Nested Loops
A nested loop in c# states that a loop inside another loop where the inner loop executes completely for each iteration of the outer loop. The following section shows a few examples to illustrate the nested loops −
Syntax
Following is the syntax of the C# nested for loop statement −
for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); }
Following is the syntax of the C# nested while loop statement −
while(condition) { while(condition) { statement(s); } statement(s); }
Following is the syntax of the C# nested do...while loop statement minus;
do { statement(s); do { statement(s); } while( condition ); } while( condition );
It is important to note that a loop nesting is that you can put any type of loop inside of any other type of loop. For example a for loop can be inside a while loop or vice versa.
Nested for Loop
The following program uses a nested for loop to find the prime numbers from 2 to 100 −
using System; namespace Loops { class Program { static void Main(string[] args) { /* local variable definition */ int i, j; for (i = 2; i < 100; i++) { for (j = 2; j <= (i / j); j++) if ((i % j) == 0) break; // if factor found, not prime if (j > (i / j)) Console.WriteLine("{0} is prime", i); } Console.ReadLine(); } } }
Output
When the above code is compiled and executed, it produces the following result −
2 is prime 3 is prime 5 is prime 7 is prime 11 is prime 13 is prime 17 is prime 19 is prime 23 is prime 29 is prime 31 is prime 37 is prime 41 is prime 43 is prime 47 is prime 53 is prime 59 is prime 61 is prime 67 is prime 71 is prime 73 is prime 79 is prime 83 is prime 89 is prime 97 is prime
Nested while Loops
The following program uses a nested while loop to find the prime numbers from 2 to 100 −
using System; namespace Loops { class Program { static void Main(string[] args) { int i = 2, j; // Outer loop while (i < 100) { j = 2; // Inner loop while (j <= (i / j)) { if ((i % j) == 0) // If factor found, not prime break; j++; } if (j > (i / j)) Console.WriteLine("{0} is prime", i); i++; } Console.ReadLine(); } } }
Output
The above code generates the following output −
2 is prime 3 is prime 5 is prime 7 is prime 11 is prime 13 is prime 17 is prime 19 is prime 23 is prime 29 is prime 31 is prime 37 is prime 41 is prime 43 is prime 47 is prime 53 is prime 59 is prime 61 is prime 67 is prime 71 is prime 73 is prime 79 is prime 83 is prime 89 is prime 97 is prime
Nested do while Loops
The following program uses a nested Do-while loop to find the prime numbers from 2 to 100 −
using System; namespace Loops { class Program { static void Main(string[] args) { int i = 2, j; // Outer loop do { j = 2; // Inner loop do { if ((i % j) == 0) break; // If factor found, not prime j++; } while (j <= (i / j)); if (j > (i / j)) Console.WriteLine("{0} is prime", i); i++; } while (i < 100); Console.ReadLine(); } } }
Output
Following is the output of the above code −
2 is prime 3 is prime 5 is prime 7 is prime 11 is prime 13 is prime 17 is prime 19 is prime 23 is prime 29 is prime 31 is prime 37 is prime 41 is prime 43 is prime 47 is prime 53 is prime 59 is prime 61 is prime 67 is prime 71 is prime 73 is prime 79 is prime 83 is prime 89 is prime 97 is prime