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

 Live Demo

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)
Updated on: 2020-06-23T12:06:18+05:30

380 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements