+ 1

Explain me how plz

int x=10; int sum=0; while(x--); { sum+=x--; printf("%d",sum); output is -1...how plz tell me

16th Nov 2017, 12:54 AM
corejava
6 Antworten
+ 7
Notice the semicolon after the while loop. It subtracts x until -1.
16th Nov 2017, 1:01 AM
qwerty
qwerty - avatar
+ 6
@corejava When it reaches zero, It subtracts it by one then it stops.
16th Nov 2017, 1:16 AM
qwerty
qwerty - avatar
+ 3
The sum +=x--; happens outside the while loop. As qwerty said, there is a semicolon after the while statement. The question is tricky trying to make you think the sum variable is in the while loop.
16th Nov 2017, 3:57 AM
David Carroll
David Carroll - avatar
+ 3
Nice breakdown Gordie.
16th Nov 2017, 4:00 AM
David Carroll
David Carroll - avatar
+ 2
y until -1 I couldn't get it....if while has zero only na the loop fails
16th Nov 2017, 1:11 AM
corejava
+ 2
wen it while gets 0 it will not enter the loop right if it enters only sum will process..coz sum is with in while
16th Nov 2017, 1:28 AM
corejava