How to break out of loop if a function returned a value

时间:2016-02-12 20:50:58

标签: c++ function for-loop

This is the function:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+/pagename\.php\?id=([^&]+)&title=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteRule ^([^/]+)/(-[^/]+)/?$ /pagename.php?id=$1&title=$2 [L,QSA]

And here i used it:

void LoseRollDice(int Result1, int Result2, int i)
{
    if (i == 1 && (Result1 == 2 || Result1 == 3 || Result1 == 12))
    {
        cout << "You Lose." << endl;
        return;
    }
    else if (i == 1 && (Result2 == 2 || Result2 == 3 || Result2 == 12))
    {
        cout << "Computer Loses." << endl;
        return;
    }
    if (Result1 == 7 && i > 1)
    {
        cout << "You Lose." << endl;
        return;
    }
    if (Result2 == 7 && i > 1)
    {
        cout << "Computer Loses." << endl;
        return;
    }
}

How to break out of for loop if the condition is true in the function? If i put break after the function in the loop it will break out of the loop even if the condition is not met

3 个答案:

答案 0 :(得分:3)

Change LoseRollDice to:

https://wordpress.org/plugins/restrict-categories/

Then your other code to:

bool LoseRollDice(int Result1, int Result2, int i)
    {
    if (i == 1 && (Result1 == 2 || Result1 == 3 || Result1 == 12))
    {
        cout << "You Lose." << endl;
        return true;
    }
    else if (i == 1 && (Result2 == 2 || Result2 == 3 || Result2 == 12))
    {
        cout << "Computer Loses." << endl;
        return true;
    }
    if (Result1 == 7 && i > 1)
    {
        cout << "You Lose." << endl;
        return true;
    }
    if (Result2 == 7 && i > 1)
    {
        cout << "Computer Loses." << endl;
        return true;
    }
    return false;
}

答案 1 :(得分:0)

Douglas Dawson's answer is correct. If you REALLY REALLY REALLY wanted that method to be void, you could also have the method throw an Exception, then catch the exception outside of the loop. Thats an absolutely terrible way of implementing it though.

Or you could catch it in the loop and call backToClasses

答案 2 :(得分:0)

Or use pointer... Send the same as parameter in function.. Change the pointer value inside if.. After that if you call function check point.. If that's changed means if had been called.. You can break your loop.