-2

This is a question from a past paper that I am having issues with, the question and output is displayed below but I don't understand how this is achieved. Can someone please explain.

int main ()
{
    int a[5] = { 1 }, b[] = { 3, -1, 2, 0, 4 };
    for (int i = 0; i<5; i++)
    {
        if (!(a[i] = b[i])) // note: = not ==
            break;
        cout << a[i] << endl;
    }
}

Output:

 3
-1
 2
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
david98
  • 1
  • 3

3 Answers3

3

The loop runs at most five times and, each time through the loop, it copies b[i] to a[i](a). If that copy resulted in a zero being placed into a[i], the condition of the if statement will be true and the loop will break. That's because the result of the expression x = y is the final value of x.

In your case, (a[i] = b[i]) will be zero or non-zero depending on b[i]. If the former, the ! turns that into a true value and the if body runs (the break happens). If the latter, you get false from the ! and the loop continues to run.

That break happens with the fourth element, which is why you only see three output lines.


(a) You should be aware that this is valid since the size of b is implicitly five (you don't explicitly set the size but it's initialised with five elements). The size of a is also five because you specified its size with a[5], despite the fact you only explicitly initialised the first element to 1 (the others are implicitly initialised to zero).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Does this mean that you could output `cout< – Joel Trauger May 31 '16 at 01:02
  • @JoelTrauger: Almost. I think it that case, precedence would evaluate `(expression) < – paxdiablo May 31 '16 at 01:09
  • Probably worth an edit to explain to OP what `int a[5] = { 1 }` initializes `a` to since this is the reason for the early exit and it's not immediately apparent to the uninitiated what's going on with that brace initializer. – user4581301 May 31 '16 at 01:41
  • @user4581301, I'll explain it but it's not the reason for the early exit. That's caused solely by the `0` value in `b`. – paxdiablo May 31 '16 at 01:52
  • Duhhhr.. You're right. Even with the expletive deleted comment I didn't see the single =. – user4581301 May 31 '16 at 01:53
0

I think the main thing you need to know to understand that piece of code is that (a[i] = b[i]) will be evaluated as whatever that's assigned to a[i], so the break will terminate your program when i is 3, which is when b[i] is 0. This is also supported by the output you pasted.

bantmen
  • 748
  • 6
  • 17
0

First you have to understand the type casting in C++, basically 0 is false when converted to boolean type (You can see a reference from here: c++ bool question)

Now the program does a very simple thing for each iteration in the loop:

Assign b[i] to a[i] and try to cast this int to boolean type (implicit conversion), and see if it is true of false, it prints the integer if it is true, break the loop otherwise

Thus, you can see why the first 3 integer is printed out, as they are assigned to a[i], being covnerted to boolean, and turns out they became true (yes, -1 became true as well, all non zero integer will became true when converted to boolean)

The 4th number is 0, and similarily it is cast to boolean and became false! So the execution breaks out the loop, no more looping, no more printing. (Even if the 5th number is non-zero, as the execution leaves the loop already, it won't be printed)

Community
  • 1
  • 1
shole
  • 4,046
  • 2
  • 29
  • 69
  • Just one nitpick - I think the C++ standard is similar to the C one, in that casting is *always* explicit - the correct term for *implicit* "type morphing" is conversion. – paxdiablo May 31 '16 at 01:11
  • @paxdiablo Thanks, edited. I have been calling them by implicit & explicit cast for a long time...everyday I learn something :) – shole May 31 '16 at 01:15