开发者

Difference between while(i=0) and while(i==0)

In my exam the following problem appeared in C++

Code:

int i, n;
int* A
cin >> n;
A = new int[n]
for(int i = 0; i < n; i++) cin >> 开发者_Python百科A[i];

while(i = 0){
   cout << A[i] << endl;
   i--;
}
}

What will be the output? I think that it should go into indefinite loop!


while (i = 0) will assign the value 0 to i and then check whether the value of the expression (which is the value assigned, i.e. 0) is non-zero. In other words, it won't execute the body of the loop even once... it'll just set i to 0. It'll also raise a warning on any decent compiler, as it's a common typo.

Following the same logic, while (i = 1) would assign the value 1 to i and always execute the loop body... only a break (or exception) within the loop would terminate it.

(Many other languages don't have this issue as widely, as they require an expression of a boolean type for conditions such as while and if. Such languages still often have a problem with while (b = false) though.)


No, it won't go into an infinite loop:

while (i=0) { /* this code is never run */ }

explanation:

i = 0;

evaluates to 0 which is "false" in conditions.


The program will not print anything out. The loop

while(i=0)

is an assignment rather than a comparison, meaning that it will assign i the value zero, then evaluate to the new value of i (namely, zero). C++ interprets zero values as false, and so the loop will not execute at all.


The second loop will not print anything.

while( i = 0 )

will work as

while( ( i = 0 ) != 0 )

and that will be equivalent to

i = 0;
while( i != 0 )

which is clearly the same as

i = 0;
while( false )


i = 0 : assign value 0 to variable i

i == 0 : test the variable i for the value 0

a lot of coders recommend putting the value you're testing for at the left hand side of the expression to avoid this common mistake, e.g.:

while (0 == i) would evaluate, however while (0 = i) would report an error.


while (i = 0): 0 will be assigned to i, then the expression will be evaluated. It's value is 0, so the while loop will be terminated immediately. Sometimes the compiler warns you about this.

while (i == 0): i is compared to 0 (without any assignment), and when i is not equal to 0, the while loop is terminated.

To avoid this as an error, some 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 check if i is "true," i.e., nonzero. i will never be nonzero, so the condition is considered false and the loop skipped.


When compiling with optmization, the C++ compiler will not even compile the body of the while loop as it knows that it will never be entered.


What will the output be? It depends on the user input. If you input a string, I believe it will throw an exception. It may also throw an exception if the user inputs a negative number for n. In my test, I got a segmentation fault: http://codepad.org/i1Z2iQFe

Otherwise, as everyone else said, nothing will be output, since the expression in the while loop always evaluates to false. i = 0 always evaluates to 0, which in C++ always evaluates to false.

Also note that this code fails to delete A, which is a bad idea, and in a larger program would create a memory leak.


For getting some extra points, write that you can avoid such unintended assignments using syntax like while (0 == i) instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜