+ 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 */

2nd Apr 2018, 8:51 PM
Ulyana Sveleba
Ulyana Sveleba - avatar
17 Antworten
+ 11
You have 99 or 101 so s isn't ever 100 and your loop doesn't stop. Try while(s < 100)
2nd Apr 2018, 8:58 PM
John Wells
John Wells - avatar
+ 7
oh, really, I was very uncareful 😞I see now. thankyou so much
2nd Apr 2018, 9:00 PM
Ulyana Sveleba
Ulyana Sveleba - avatar
+ 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 😉
2nd Apr 2018, 9:03 PM
Ulyana Sveleba
Ulyana Sveleba - avatar
+ 5
CipherFox done ✔
5th Apr 2018, 10:59 PM
Rex
Rex - avatar
+ 3
Ulyana Sveleba LOL.. you wish to win me again??? 😭😂😂😂 Challenge Declined.. i'll come back Stronger and give you a good challenge 😊😊
5th Apr 2018, 10:13 PM
Rex
Rex - avatar
+ 3
Have I already challenged you Jason? I don't remember, sorry
5th Apr 2018, 10:15 PM
Ulyana Sveleba
Ulyana Sveleba - avatar
+ 3
You know, Jason, it was so funny... 😂 your profile photo with a f#ck gesture and words "Challenge Declined"...
5th Apr 2018, 10:16 PM
Ulyana Sveleba
Ulyana Sveleba - avatar
+ 3
Ulyana Sveleba Thank you for all the likes and follow. I really appreciate 👍👍🙇🙇
5th Apr 2018, 10:23 PM
Rex
Rex - avatar
+ 3
No need to thank! Be successful and let your dreams come true! Jason
5th Apr 2018, 10:34 PM
Ulyana Sveleba
Ulyana Sveleba - avatar
+ 3
Vikaash cool and useful app. thanks Vikaash
7th Apr 2018, 1:15 PM
Ulyana Sveleba
Ulyana Sveleba - avatar
+ 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
2nd Apr 2018, 9:00 PM
🇮🇷 M N
+ 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 😊😂😂😂👍
5th Apr 2018, 10:20 PM
Rex
Rex - avatar
+ 2
Ulyana Sveleba thanks 😊
7th Apr 2018, 2:29 PM
Vikaash
Vikaash - avatar
+ 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 😊
2nd Apr 2018, 9:10 PM
SatyaJit Pradhan
SatyaJit Pradhan - avatar
+ 1
because "s != 100" is always true...
2nd Apr 2018, 10:50 PM
Heji Palaña
Heji Palaña - avatar
+ 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.
8th Apr 2018, 6:31 PM
Harmony Moyo
Harmony Moyo - avatar