tomsseisums
tomsseisums

Reputation: 13357

Make current iteration repeat itself

Is there a way to make the current iteration repeat itself in a loop?

var repeated = false;

var total = 0;

for (var i = 0; i < 50; i++)
{
    total += i;

    if (i == 33 && !repeated)
    {
        repeated = true;
        repeat; // imaginary
    }
}

Upvotes: 2

Views: 2551

Answers (3)

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28528

You can try this, don't need to repeat:

var total = 0;
for (var i = 0; i < 50; i++)
{
    total += i;
    if (i == 33)
    {
       total += i;
    }
}

Upvotes: 0

Bill
Bill

Reputation: 25555

Sure, just subtract 1 from i.

var repeated = false;

var total = 0;

for (var i = 0; i < 50; i++)
{
    total += i;

    if (i == 33 && !repeated)
    {
        repeated = true;
        i--;
    }
}

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191729

Just subtract 1 from i to "repeat"

Upvotes: 5

Related Questions