C Sharp -Nested Loops

+2 Muamet Shasivari · July 26, 2015
Can anyone explain me how Nested Loops are executed in C Sharp?
Which one executes first ?
Please give me some examples with explanation .
Thank you.

Post a Reply


Oldest  Newest  Rating
+1 Alan Johnson · July 26, 2015

// This loop executes first, its the outer loop
for (var x = 0; x < 5; x++)
// This is a nested loop (loop inside a loop)
for (var y = 0; y < 5; y++)

+1 Muamet Shasivari · August 3, 2015
OK.From this example if X<5 then gives somekind of green light to go to other loop inside that one,but what happens if the inside loop y<5 is not true,if y =6.What is next?
+1 Sha Kal · August 3, 2015
Look. " X " loop is going to start 5 times, bot for each pass of loop " X ", loop " Y " is going to complete 5 times, because you set it that way. Look at it this way, it starts with loop " X " and is going to complete everything in it 5 times because X<5, thats 5 x 5 for 
" Y " loop. You asked if " Y " is 6, it means it would complete 6 times for each time " X " completes. (5x6);
+1 nmelssx nmelssx · August 11, 2015
If the inside loop is not true, then the inside loop exits, and it goes back to the outer loop. The outer loop with see if it's true, and if it is, it'll run the inner loop again. And so on and so forth until the outer loop is false. Or if it'll never reach false, it'll keep on going indefinitely, or until you run out of memory. 
  • 1



Server-side, HTML embedded scripting language used to create dynamic Web pages.