java - How would I completely restart a for loop or an if statement? -
i looking answer either of these 2 questions because either 1 work code. there function or restart for
loop or rerun if
statement? for
loop, let's is
for(int = 0; i<=30; i++){ //code here }
and point had gotten i = 5
, how tell go i = 0
?
for if
statement, let's if
statement was:
if(i == 1 && j == 2){ //more code here }
how tell it, @ end of if
statement, if if
statement true
, rerun if
statement?
tl;dr sounds want while
loop or possibly recursive function.
the for
loop version
you can't restart if
statement, can modify loop control variable in for
loop. i = 0;
, watch out infinite loops, this:
for(i = 0; < 30; i++) { // stuff if(somecondition) { = 0; } }
this isn't style, though, , can lead devilish bugs hard find. people aren't used looking modifications loop control variable inside for
loop because it's 1 of things you. just. don't. do.
the while
loop version
so, instead of for
loop, should use while
loop if plan modify i
inside loop.
example:
var = 0; while(i < 30) { // stuff if(somecondition) { = 0; } else { i++; } }
the recursive function version
depending on want do, might consider creating function , calling recursively:
function myfunction() { // stuff if(somecondition) { myfunction(); // recursion } } for(i = 0; < 30; i++) { myfunction(); }
with of these approaches, watch out infinite loops! easy create if aren't careful.
Comments
Post a Comment