# Returning this code to start of loop if value detected from a declared int

With the code below, how would I go about using the 'return 0;' if a value over 500 is found from the declared int?

``````  long calculatedmove=((z*6400L)/40);  //  Calculate number of steps needed in mm
stepper.runToNewPosition(calculatedmove);
currentposition = String(z);
u8g.firstPage();
do {
u8g.drawHLine(0, 15, 84);
u8g.drawVLine(50, 16, 38);
u8g.drawHLine(0, 35, 84);
u8g.setFont(u8g_font_profont11);
u8g.drawStr(0, 10, "ENTER DISTANCE");
u8g.drawStr(62, 29, "MM");
u8g.drawStr(4, 46, "cur-pos");
u8g.setPrintPos(57,47);
u8g.print(currentposition);
}
while( u8g.nextPage() );

if (currentposition <=500) {
return 0;
}
}
``````
``````currentposition = String(z);
``````

currentPosition is undefined in your snippet.

Do you mean something like this? :

``````int z;
void loop () {
... calculate current z somehow
long calculatedmove=((z*6400L)/40);  //  Calculate number of steps needed in mm
...
if (z <=500)
return;  // restart loop

... rest of loop to be executed only if z > 500
}
``````

Also, a divide expression is one of the slowest you can do, so why not simplify this:

_ long calculatedmove=((z*6400L)/40); // Calculate number of steps needed in mm_

to

_ long calculatedmove=(z * 160L); // Calculate number of steps needed in mm_

econjack:
Also, a divide expression is one of the slowest you can do, so why not simplify this:

_ long calculatedmove=((z*6400L)/40); // Calculate number of steps needed in mm_

to

_ long calculatedmove=(z * 160L); // Calculate number of steps needed in mm_

I suspect that the compiler will do that for you.

michael_x:
currentPosition is undefined in your snippet.

Do you mean something like this? :

``````int z;
``````

void loop () {
... calculate current z somehow
long calculatedmove=((z*6400L)/40);  //  Calculate number of steps needed in mm
...
if (z <=500)
return;  // restart loop

... rest of loop to be executed only if z > 500
}

Unfortunately it didn't work. the z is calculated by coming 3 entry numbers by keypad to create a full number to be used in the calculation. Found it to be too complicated to return after calculating the numbers before it was a full number

I'm sure it does, but that's not the point. The point is someone must also read the code, so why make it more complex than it needs to be? If the subexpressions help to explain what the code does, then put that into a comment at the end of the statement:

long calculatedmove = (z * 160L); // 160L = 6400L/40

Also, the code could be ported to a compiler than generates less efficient code than the GCC. Finally, I think code is always easier to read when you add whitespace around operands and operators. I see little reason to "squish" the code and remove all whitespace. (I'm not as good about this as I should be, but I try.)