0
Why is the output coming 1
int a = 1; do { Console.WriteLine(a); a++; } while(a < 0);
6 Antworten
+ 8
■ do-while loop always execute the set of code in the do block. then only it check the while condition.
in the give code..
●we initialize a=1.
●then it enter do block and print the value of a as 1. ( console.WriteLine(a))
●then we increment the value of a by one. (a++)
now value of a is 2.
●then it check while condition
while (a <0) that is
○while(2 <0 ) is false
●it exit the while loop
finally the output is 1.
+ 4
🅰 is equal to 1.
In your code,
You said, print 1.
So it printed 1.
Then you incremented 🅰 by 1.
So 🅰 now is equal to 2. ✔
Then you said,
while(a is less than 0)
repeat the code execution. ♻
But, 🅰 is equal to 2, and 2 is ❌NOT less than 0.
So the code execution stopped. 🚫
And the only number you have printed is 1. 😁
Hope that helps 😁
0
hello
you are getting 1 because in your "while" you wrote "while a is smaller then 0" and the numbers that are smaller then 0 are negative numbers. You need to change your while condition, set it to a higher number.
0
Nice explanation😀 @erwin
0
An Up Vote would be highly appreciated @Ankit 😂😁
0
hello I am an IT engineer @Google in USA so if u have related problems regarding any coding please ask me