There are 3 options that depend a little on what you want. Note that your code never goes into if
because the condition inside for
does not allow negative values. Your for
runs for the last time when i
is 1 and doing i--
gives you a last value of 0
and its if
looks <0
. Having said that the options are:
break;
Break stops the loop and moves on to the first line of code after of the loop. If you do not need to go through all the iterations of a loop
but need to run code after the loop, use this option.
continue;
The continue jumps to the next iteration of the loop. If you do not need to run all the code for a specific iteration, but need all iterations, use this option.
return;
The return stops the function it is in and returns the value after the word "return". It is inside a loop, switch or other the function stops immediately and does not execute the next line to return.
How much processing savings depends on what you need. The most economical is the return because it is the most powerful and ensures that no further code is run. The suggestion is to always use the most defenitive and do not limit the code you want to run.
Is it useful to use these methods to save processing? Yes, of course.