0
Why is the output coming 1
int a = 1; do { Console.WriteLine(a); a++; } while(a < 0);
6 Answers
+ 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