How to unglue this variable so that for loop resets variable every iteration?

 
   for(int x=OrdersTotal()-1;x>=0;x--)
     {
      if(!OrderSelect(x,SELECT_BY_POS))
         continue;
      int variable=0; 

      if(OrderSymbol()==_Symbol && MarketInfo(_Symbol,MODE_ASK)<=OrderOpenPrice())
         variable=1;

      if(variable=1)
         Print("now variable is 1");

       if(variable=0)
         Alert("now variable is 0");
      }

 Why does the variable become stickied to 1 after it is 1 for the first time? It resets to 0 every iteration of the for loop unless there is something to be done if the conditions for variable =1 are met. I would like it to be able to not be 1 when the for loop's subsequent iterations happen. It gets glued to 1; how do I have it reset every iteration?


If we remove the

 if(variable=1)
 Print("now variable is 1");

 if(variable=0)
 Alert("now variable is 0");

from the for loop, and then proceed to check out what the variable is with Print(), we can see that it actually does update properly upon every iteration of the for loop. Problem happens when we assign a conditional statement to it upon it being 1.

How do I get variable to not be glued to 1 once I have assigned something to be done upon the conditions for variable 1 are met? I tried reversing it by assigning a conditional statement when it is 0 which did nothing to help. I've got no errors in the tabs.

Heres the EA. Thanks a ton in advanced.

Files:
variable.mq4  2 kb
 
This sets the variable to one, and since non-zero is true you get a printout.
if(variable=1)
   Print("now variable is 1");
This sets the variable to zero and since zero is false you do not get a printout
if(variable=0)
 Alert("now variable is 0");

Don't assign, compare

 
whroeder1:
This sets the variable to one, and since non-zero is true you get a printout.
This sets the variable to zero and since zero is false you do not get a printout

Don't assign, compare

 

I think you're missing the crux of what @whroeder1 is telling you.

This:

if(variable=1)

means something very different to this:

if(variable==1)

The first means "set variable to 1" (wrong in this context)

The second means "check if variable is 1" (correct in this context).

 

WHroeder already told you. Read again.

A single "=" is an assignment.

Double "==" is comparison.


if(variable = 1)   //<== This does not compare, it assigns 1 to the "variable". 
                   // The result is the assigned value. It evaluates to false if you assign zero, true otherwise.


What you want is:

if(variable == 1)  // Note the "==" operator


Also, read the documentation: Operations and Expressions

 
honest_knave:

I think you're missing the crux of what @whroeder1 is telling you.

This:

means something very different to this:

The first means "set variable to 1" (wrong in this context)

The second means "check if variable is 1" (correct in this context).

Thanks a ton all, didn't notice first time I read, super late here/
 
nadiawicket:
Thanks a ton all, didn't notice first time I read, super late here/
Issue resolved. thanks again
Reason: