Fenia Kechagia
Fenia Kechagia

Reputation: 83

Jump values in loop "for"

I have two "for" loops.

for(n=0;n<6;n++){
for(w=n;w<6;w++){
// if w==4 then go to first loop an continue from n=4!!
}
}

How can i jump to n=4 when w takes value 4; Like old Basic command "goto".. Thanks

Upvotes: 0

Views: 82

Answers (1)

Nir Alfasi
Nir Alfasi

Reputation: 53535

How can i jump to n=4 when w takes value 4; Like old Basic command "goto"

You can use break to "jump" out of the current loop back to the outer loop:

for(n=0;n<6;n++){
    // other code
    for(w=n;w<6;w++){
    // if w==4 then go to first loop an continue from n=4!!
        if (w == 4) {
            n = 4;
            break;
        }
    }
    // other code
}

Upvotes: 1

Related Questions