+ 8
Friends, help me please with this java question
Please, determine the value of variable "s" int s = 1; int k = 0; do{ s+=2; } while(s != 100); System.out.print(s); My answer was 101, but the correct one is infinite loop. Could anyone explain me why? /* maybe, it's a stupid question but I really can't figure out */
17 odpowiedzi
+ 11
You have 99 or 101 so s isn't ever 100 and your loop doesn't stop. Try while(s < 100)
+ 7
oh, really, I was very uncareful 😞I see now. thankyou so much
+ 7
I didn't notice that I have "!=" not "<", that makes a big difference. I understand now. Thankyou all guys!
Sololearn community is very helpful and it will become more popular than Stackoverflow one day 😉
+ 5
CipherFox done ✔
+ 3
Ulyana Sveleba LOL.. you wish to win me again??? 😭😂😂😂 Challenge Declined.. i'll come back Stronger and give you a good challenge 😊😊
+ 3
Have I already challenged you Jason? I don't remember, sorry
+ 3
You know, Jason, it was so funny... 😂 your profile photo with a f#ck gesture and words "Challenge Declined"...
+ 3
Ulyana Sveleba Thank you for all the likes and follow. I really appreciate 👍👍🙇🙇
+ 3
No need to thank! Be successful and let your dreams come true! Jason
+ 3
Vikaash cool and useful app. thanks Vikaash
+ 2
if the condition was s < 100 then your answer was correct but, s never be equals to 100 so the loop never ends, and continue forever
+ 2
Haha you challenged me once or twice i guess and you beat with a huge margin.. I want to have more wins than draws/loses.. Don't worry i'll give you a good challenge 😊😂😂😂👍
+ 2
Ulyana Sveleba thanks 😊
+ 1
The value of s will never be equal to 100 ... so the condition s!=100 never holds false or is always true. Because the initial value of s is 1 and incremental of it by 2 , will result 3,5,7...99,101 but never 100...You can change the condition to (s<100) or initialise s with 0 (s=0) .
Hope this helps you understand 😊
+ 1
because "s != 100" is always true...
+ 1
It goes in multiples of 3
such that : s1 = 3, 6,...,99,102
This means s is never equal to 100
So this result to an infinite loop where the condition is always true.