
- C# Basic Tutorial
- 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# - Decision Making
- C# - Loops
- 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# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to create nested while loop in C#?
For a nested while loop, we have two while loops.
The first loop checks for a condition and if the condition is true, it goes to the inner loop i.e. the nested loop.
Loop 1
while (a<25) { }
Loop 2 (inside loop1)
while (b<45){ }
To create a nested while loop, the following is the sample code.
Example
using System; namespace Program { class Demo { public static void Main(string[] args) { int a=20; while (a<25) { int b=40; while (b<45) { Console.Write("({0},{1}) ", a, b); b++; } a++; Console.WriteLine(); } } } }
Output
(20,40) (20,41) (20,42) (20,43) (20,44) (21,40) (21,41) (21,42) (21,43) (21,44) (22,40) (22,41) (22,42) (22,43) (22,44) (23,40) (23,41) (23,42) (23,43) (23,44) (24,40) (24,41) (24,42) (24,43) (24,44)
- Related Articles
- How to use nested while loop in JavaScript?
- How to use nested for loop in JavaScript?
- How to use ‘while loop’ in Java?
- Python - How to convert this while loop to for loop?
- How to convert a Python for loop to while loop?
- do…while loop vs. while loop in C/C++
- How to use ‘do while loop’ in Java?
- How to use C# do while loop?
- A nested loop puzzle?
- How to emulate a do-while loop in Python?
- Java while loop
- How to print a diamond using nested loop using C#?
- How does Python while loop work?
- How to create nested Python dictionary?
- What are the differences between while loop and do-while loop in Java?

Advertisements