[ACCEPTED]-Difference between while(i=0) and while(i==0)-c++
No, it won't go into an infinite loop:
while (i=0) { /* this code is never run */ }
explanation:
i = 0;
evaluates 1 to 0
which is "false" in conditions.
while (i = 0)
will assign the value 0 to i
and then check whether 14 the value of the expression (which is the 13 value assigned, i.e. 0) is non-zero. In 12 other words, it won't execute the body of 11 the loop even once... it'll just set i
to 10 0. It'll also raise a warning on any decent 9 compiler, as it's a common typo.
Following 8 the same logic, while (i = 1)
would assign the value 7 1 to i
and always execute the loop body... only 6 a break (or exception) within the loop would 5 terminate it.
(Many other languages don't 4 have this issue as widely, as they require 3 an expression of a boolean type for conditions 2 such as while and if. Such languages still 1 often have a problem with while (b = false)
though.)
The program will not print anything out. The 5 loop
while(i=0)
is an assignment rather than a comparison, meaning 4 that it will assign i
the value zero, then 3 evaluate to the new value of i
(namely, zero). C++ interprets 2 zero values as false, and so the loop will 1 not execute at all.
The second loop will not print anything.
while( i = 0 )
will 2 work as
while( ( i = 0 ) != 0 )
and that will be equivalent to
i = 0;
while( i != 0 )
which 1 is clearly the same as
i = 0;
while( false )
i = 0
: assign value 0 to variable i
i == 0
: test the 5 variable i for the value 0
a lot of coders 4 recommend putting the value you're testing 3 for at the left hand side of the expression 2 to avoid this common mistake, e.g.:
while (0 == i)
would 1 evaluate, however while (0 = i)
would report an error.
while (i = 0)
: 0 will be assigned to i
, then the expression 7 will be evaluated. It's value is 0, so the 6 while loop will be terminated immediately. Sometimes 5 the compiler warns you about this.
while (i == 0)
: i
is 4 compared to 0 (without any assignment), and 3 when i
is not equal to 0, the while loop 2 is terminated.
To avoid this as an error, some 1 programmers write while (0 == i)
, then while (0 = i)
is invalid.
while (i=0)
will cause i
to be set to 0, and then will 3 check if i
is "true," i.e., nonzero. i
will 2 never be nonzero, so the condition is considered 1 false
and the loop skipped.
When compiling with optmization, the C++ compiler 2 will not even compile the body of the while
loop 1 as it knows that it will never be entered.
What will the output be? It depends on 12 the user input. If you input a string, I 11 believe it will throw an exception. It 10 may also throw an exception if the user 9 inputs a negative number for n
. In my test, I 8 got a segmentation fault: http://codepad.org/i1Z2iQFe
Otherwise, as 7 everyone else said, nothing will be output, since 6 the expression in the while loop always 5 evaluates to false. i = 0
always evaluates to 4 0, which in C++ always evaluates to false
.
Also 3 note that this code fails to delete A
, which 2 is a bad idea, and in a larger program would 1 create a memory leak.
For getting some extra points, write that 2 you can avoid such unintended assignments 1 using syntax like while (0 == i)
instead.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.