Which part of this code is executed once? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Which part of this code is executed once?

I'm looking at this do loop and I don't understand which part of the code is executed once. Or how the logic flowed. Can someone help me out? int a = 0; do { Console.WriteLine(a); a++; } while(a < 5); /* Outputs 0 1 2 3 4 */

11th Jan 2020, 3:25 AM
Pang Ren Tan
Pang Ren Tan - avatar
1 Answer
+ 3
Pang Ren Tan , You probably misread from lesson. It goes like this:For a do while loop if condition is false then also it'll run at least once. see this example , you need to compare while and do-while: while(1!=1){ Console.WriteLine("Yes!"); } as condition is false loop won't be executed. not even single time. do{ Console.WriteLine("Yes! "); }while(1!=1); Though condition is false it'll be executed at least once. Why? because condition is at end of loop body. It first executes statements within body and then proceed to check if condition is true or false. You can try experimenting with your provided code by changing conditions and loop type. Search on net for details. Sololearn lessons are not enough to understand if you are absolute beginner.
11th Jan 2020, 5:11 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar