Why is the output coming 1 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is the output coming 1

int a = 1; do { Console.WriteLine(a); a++; } while(a < 0);

9th Jul 2016, 10:44 AM
Ankit
Ankit - avatar
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.
10th Jul 2016, 7:32 AM
VishnuPriya
+ 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 😁
9th Jul 2016, 2:01 PM
Erwin Mesias
Erwin Mesias - avatar
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.
9th Jul 2016, 11:39 AM
vivek rawat
vivek rawat - avatar
0
Nice explanation😀 @erwin
9th Jul 2016, 2:10 PM
Ankit
Ankit - avatar
0
An Up Vote would be highly appreciated @Ankit 😂😁
10th Jul 2016, 6:56 AM
Erwin Mesias
Erwin Mesias - avatar
0
hello I am an IT engineer @Google in USA so if u have related problems regarding any coding please ask me
5th Sep 2016, 1:14 PM
Ashwin Giri
Ashwin Giri - avatar