0
for (int att = 1; att < 11; att++)
{
     <body>;
     //break will completely finish running the program
}

I'm making a CodeBreaker(Mastermind) game, and I'm having trouble with ending a loop earlier than it needs to at less than 11, and then set the loop back to the initialization state of att = 1.

att stands for "attempts". The user can guess a randomly generated code up to a maximum of 10 times. Once the user guesses the correct code in less than 10 attempts, I want to prompt the user to play again and generate a new random code. But the loop shown above is still running.

How can I end the loop early, but still continue running the program? Majority of the program depends on this one loop, so break will completely stop it running.

2
  • 2
    Set att = 255; (or any other value that doesn't satisfy att < 11)? Or, maybe you want to reset everything to the start, so att = 1? But are you sure you want to do things like that? Surely, you want to run one iteration for 'for up to 10 iterations while solution not found' as the main loop, and then a second, outer loop (probably in a different function that calls the function containing the current loop) that controls whether the user gets another go at the overall problem. At any rate, there's nothing intrinsically difficult about jiggering the loop. You can adjust att as required. Jul 7, 2015 at 5:57
  • Thank you all. I took each of your posts to heart and with a few modifications, I got it working.
    – dendritic
    Jul 7, 2015 at 6:23

5 Answers 5

6

To set the loop back to the initialization state of att = 1, you can use continue:

for (int att = 1; att < 11; att++)
{
    if(you_want_to_set_loop_back) {
        att = 1;
        continue;    //It will begin the loop back with att=1, but if any other variable is modified, they will remain as it is (modified).
    }
}

OR

You can write your loop in a function with all the variable that you want at their initial value. And keep calling this function as long as you want. To break out the loop, use break and return from function or directly return from the loop instead of breaking it.

3

You could do something like:

while(true){
    for (int att = 1; att < 11; att++)
    {
        <body>;
        //game, and when it finishes
        break;
    }
    //Asks player if he wants to continue, if not then break again
}
1

How about a while-loop around the for-loop?

while(programRunning){
    for (int att = 1; att < 11; att++)
    {
        <body>;
        if(answer==correct){
            att = 12; // ends the for-loop
        }
    }
    if(gameOver){
        programRunning = false; // unless you want to end the game, starts the for-loop from att = 1
    }
}
1

I think you might try in following way:

bool bQuitGame = false;
while(!bQuitGame)
{
    for(att = 1; att < 10; ++att)
    {
        if(you want to only quit "for" but stay in "while")
        {
            <code...>
            break;
        }
        else if(you want to quit "while")
        {
            <code...>
            bQuitGame = true;
            break
        }
        else if(you want to start the next iteration in "for")
        {
            <code..>
            continue;
        }
        else //you want to stay in "for"
        {
            <code...>
        }
    }
}
0

It seems your problem can be solved by simple nested loops like these :

while(!success){
    for (int att = 1; att < 11; att++){
        <body>;
        if(answer is correct){
            success = true;
            break;
        }
        //break will completely finish running the program

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.