trouble with loop and break

Hey guys, here is my code:
while (z < 30)
{
z++;
if (x_initial == -1) // create jump in values
break;
Serial.println(z);
delay(100);
}

For some reason the loop keeps completing it’s incrementing sequence. It doesn’t stop when the variable x_inital == -1. What am I missing? Thanks.

while (z < 30)
{
z++;
if (x_initial == -1) // create jump in values
break;
Serial.println(z);
delay(100);
}

Anyone? I did try to put the code in proper format but it didn’t take. Not sure why.

SteveRogers: Anyone?

It's been 8 whole minutes. You might try having a little patience.

You're only showing this one little snippet of code and I can't see anywhere in that that this x_initial value gets set to -1. So I'm going to go with that variable is probably not ever equal to -1 and that's why the loop doesn't break.

If you would care to show the rest of the code, someone might be able to figure out what is going wrong.

You have correctly coded the loop to break when x_initial == -1. However the value of x_initial is not updated by the loop. Since it never changes in the loop you probably shouldn't test it inside the loop. Enclose the loop in an if (x_initial != -1).

It doesn't stop when the variable x_inital == -1

You need to question this since the loop will clearly terminate when x_initial == -1.

Thank you David Oconner. I will try enclosing the while loop in an if statement. That makes a lot of sense.

After much frustration I am still unsure how to include the while inside the if statement.

SteveRogers: After much frustration I am still unsure how to include the while inside the if statement.

It would help if you posted your complete code. Since we have no way of knowing what x_initial is when you start the loop, or what changes it (hint: nothing in the loop changes it), we really can't do much for you.

If you want to post your code, please do the following...

In the IDE compile your code hit CTRL-T to format the code press CTRL-A to select all the code press CTRL-C to copy it.

In the forum editor window click on the # icon above the editor window you will see that the code tags are shown, with the cursor between them press CTRL-V to paste your code.

I did post my entire code. I just don’t know how to program I guess.

SteveRogers: I did post my entire code. I just don't know how to program I guess.

If that loop is your entire code, you are wasting your time and ours. That code will not compile.

Where are the declarations of x_initial and z? Where is setup()? Where is loop()? if you don't have loop() or setup() you need a main().

If you aren't going to post your code, the least you could do is to not lie about having posted it.

SteveRogers: I did post my entire code.

Of course you didn't. The snippet you posted would not even compile in the first place.

When people say "entire code", they mean just that - the whole of the code you wrote in the IDE.

As much to the point, you need to explain what the function actually is that you want (the code) to perform.

SteveRogers:
I did post my entire code. I just don’t know how to program I guess.

No Steve. This…

  while (z < 30)
    {
    z++;
     if (x_initial == -1)      // create jump in values
      break;
       Serial.println(z); 
      delay(100); 
     }

… is not a complete sketch which would include setup() and loop() so we can see where x_initial is declared and what makes it change.

EDIT… just like Larry and Paul said faster than me 8)

JimboZA:
EDIT… just like Larry and Paul said faster than me 8)