Jesse Wiatrak
Jesse Wiatrak

Reputation: 73

How would I completely restart a for loop or an if statement?

I am looking for an answer to EITHER of these two questions because either one will work for my code. Is there a function or something that will completely restart a for loop or rerun an if statement? For the for loop, let's say it is

for(int i = 0; i<=30; i++){
   //code here
}

and by this point it had gotten to i = 5, how would I tell it to go back to i = 0?

For the if statement, let's say the if statement was:

if(i == 1 && j == 2){
  //more code here
}

How would I tell it, at the end of the if statement, if the if statement is true, to completely rerun the if statement?

Upvotes: 3

Views: 34821

Answers (5)

Alireza
Alireza

Reputation: 1

By setting i to -1 you can restart the for loop but be careful do not enter an endless loop by the way you set i to -1.

Upvotes: -1

epedos
epedos

Reputation: 1

All the other answers are incorrect, because on the first iteration i=0; sets i to 0 and then i++ in the for loop increases it to 1, so basically you never start from i=0 again, which will result in always skipping the first array element if you want to check an Array a[i].

Upvotes: -2

elixenide
elixenide

Reputation: 44851

TL;DR It sounds like you want a while loop or possibly a recursive function.

The for Loop Version

You can't restart an if statement, but you can modify your loop control variable in the for loop. Just do i = 0; and watch out for infinite loops, like this:

for(int i = 0; i < 30; i++) {
    // do stuff
    if(someCondition) {
        i = 0;
    }
}

This isn't very good style, though, and can lead to some devilish bugs that are hard to find. People aren't used to looking for modifications to the loop control variable inside the for loop because it's one of those things that You. Just. Don't. Do.

The while Loop Version

So, instead of a for loop, you should use a while loop if you plan to modify i inside the loop.

Example:

int i = 0;
while(i < 30) {
    // do stuff
    if(someCondition) {
        i = 0;
    } else {
        i++;
    }
}

The Recursive Function Version

Depending on what you really want to do, you might also consider creating a function and calling it recursively:

function myFunction() {
    // do stuff
    if(someCondition) {
        myFunction(); // this is the recursion
    }
}
for(int i = 0; i < 30; i++) {
    myFunction();
}

With all of these approaches, watch out for infinite loops! They are easy to create if you aren't careful.

Upvotes: 11

Pranav Totla
Pranav Totla

Reputation: 2432

Typically you can use continue and break statements for your problem, but they won't initialize back to i = 0.

However, for your problem the following is suitable:

for(i=0; i<30; i++){
    if(condition check) {i=0;}
    // other code
}

Upvotes: 1

Lance
Lance

Reputation: 3932

Be careful doing this. You can get stuck in a loop you can't get out of.

for(int i = 0; i<=30; i++){
   //code here
    if (some condition) {i=0;}
}

Upvotes: 1

Related Questions