How many times the loop will run pls explain it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How many times the loop will run pls explain it?

Int x=-5; while(x<0){ x++; }

23rd Apr 2022, 3:56 PM
saseentharan p
saseentharan p - avatar
17 Answers
+ 1
Thanks for ur explanation jayakrishna
23rd Apr 2022, 4:07 PM
saseentharan p
saseentharan p - avatar
+ 1
5 times that is x=-5,-4,-3,-2,-1 when it comes to 0 the condition (x<0 ) fails so the loop will run for 5 times
23rd Apr 2022, 4:32 PM
Vudayana Sandhya Rani
+ 1
Syntax error Int is not capital int is written in small letter always
24th Apr 2022, 6:09 AM
Gaurav Dixit🇮🇳
Gaurav Dixit🇮🇳 - avatar
0
saseentharan p 0 ( zero ) times, because the condition inside the while loop will evaluate to false because x = 5 which is more than 0 but the codition is asking for x < 0 then only it will run, if x = -1 then the while loop will run
23rd Apr 2022, 3:58 PM
NonStop CODING
NonStop CODING - avatar
0
But there was x=(minus)5 only so the condition was true only
23rd Apr 2022, 4:01 PM
saseentharan p
saseentharan p - avatar
0
when x = -5, -4, -3 , -2 , -1 0<0 is false so 5 times.. saseentharan p you can count like this,.. Take pen paper , wrote down. You can observe it clearly yourself.. NonStop CODING you may not observed, it's negative 5, x = -5 ..
23rd Apr 2022, 4:04 PM
Jayakrishna 🇮🇳
0
saseentharan p wooo man, did you just edited the code🤔🤔. if x = -5, then the loop will run 5 times only
23rd Apr 2022, 4:05 PM
NonStop CODING
NonStop CODING - avatar
0
Jayakrishna🇮🇳 i think he wrote x = 5 first then he edited the code later.
23rd Apr 2022, 4:06 PM
NonStop CODING
NonStop CODING - avatar
0
No, I'm not editing it, you not clearly saw
23rd Apr 2022, 4:06 PM
saseentharan p
saseentharan p - avatar
0
oohk let's end this discussion
23rd Apr 2022, 4:08 PM
NonStop CODING
NonStop CODING - avatar
0
Haa ok
23rd Apr 2022, 4:08 PM
saseentharan p
saseentharan p - avatar
0
It will run 4 times I think
23rd Apr 2022, 10:46 PM
madeline
madeline - avatar
0
Loop will not work bcz its a logical error
25th Apr 2022, 6:29 AM
Muhammad Rehan
Muhammad Rehan - avatar
0
First you need to change Int to int. After loop will run 5 times and condition will check 6 times.
25th Apr 2022, 7:10 AM
Thineshan Panchalingam
Thineshan Panchalingam - avatar
0
First change Int to int then loop will run 5 times
25th Apr 2022, 12:59 PM
Himanshu Sharma
Himanshu Sharma - avatar
0
5times
25th Apr 2022, 3:07 PM
L@rrySonP1
L@rrySonP1 - avatar
0
Its 4 iterations. -5 to -4 == 1 iteration -4 to -3 == 2 iteration -3 to -2 == 3 iteration -2 to -1 == 4 iteration Stop because -1 is less than 0
25th Apr 2022, 5:34 PM
madeline
madeline - avatar