#assignment operator meaning inside if statement

21 messages · Page 1 of 1 (latest)

ripe girder
#

int array[]={1,2,3,4,5,6};
int occurrence=-1;
int key=4;
for(int i=0;i<6;i++){
if(array[i]=key){
occurrence=i;
}
}
cout<<occurrence;

In this code above,if I use assignment operator instead of "==" in the fifth line there doesn't come any error but it's printing something else i.e 5 what it's representing here??what does that assignment operator signify here

turbid merlinBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

reef smelt
#

it just assigns right hand side to the left hand side

#

question is what happens after that value, and so after assignment you are left with the value, which gets implicitly converted to boolean

#

in this case 0 means false and anything else true

#

so the check is gonna happen 5 times, and every time you will assign key to array[i]

#

and you probably want to have occurance+=i;

#

because now it's just going to save the last i, without actually keeping number of occurances

outer garnet
#

you can think of using the assigment as two seperate statements ```cpp
array[i]=key;
if(array[i]){
occurrence=i;
}

sand zinc
#

Assignments have a return value. The reason is that you can do a = b = c = 5;. c = 5 is evaluated to 5 which is then assigned to b and so on. Here if (array[i] = key) is evaluated to if (4) and numbers other than 0 are evaluated as true.

ripe girder
#

U r telling (6=5=4=3=2=1=key) means first array[0] returns value 4(key) which is then assigned to array[1] i.e 2 nd so on till i=5 i.e last i which it is giving us as result...are u telling the same thing or I am going in wrong direction??

sand zinc
#

Can't do this. It would try to evaluate 1=key and then notice you cannot assign to 1.

#

(you could make it compile by overloading operators, but that's really not what you want)

ripe girder
sand zinc
#

There are 2 parts to it.
The first part is the return value of an assignment. a = 5 evaluates to 5 so that when you do b = a = 5 b also becomes 5.
The second part is that numbers other than 0 are evaluated as true, so if (5) is the same as if (true).

ripe girder
#

Ok can u explain me in the context of my question how it works here

sand zinc
#

if(array[i]=key) evaluates to if(array[i]=4) which evaluates to if(4) which evaluates to if(true).

ripe girder
#

Sorry I am a beginner I don't understand completely. Could u give me an example where 0 is also included and what will be the compiler's result there so that I will be more able to understand the logic here

sand zinc
#
int key = 0;
int value = 42;
if (value = key) {
  std::cout << "This will not print\n";
}

if (value = key) is evaluated to if (value = 0) which assigns 0 to value and then evaluates if (0) which evaluates to if (false).

ripe girder
#

Ok means in my question everytime it's going to be true that's why it's printing the last i traversing through the loop

ripe girder
#

!solved