开发者

C++ "while" explanation

开发者 https://www.devze.com 2023-03-14 05:52 出处:网络
i am not very sure the while(choice == 1 || choice ==2); can anyone explain. i understand this if(choice ==1)

i am not very sure the while(choice == 1 || choice ==2); can anyone explain. i understand this

if(choice ==1)
    displayMonthly(rainfall);
else if(choice == 2)
    displayTotal(rainfall);

i just don't understand the code after this. can anyone explain to me please.

int main()
{
    //declare variable and array
    int choice = 0;
    double rainfall[12] = {0.0};

    //get rainfall amounts
    for(int x =0;x<12;x++)
    {
        cout << "Enter rainfall for month "<< x+1<< ": ";
        cin >> rainfall[x];
    }

    do
    {
        //display menu and get menu choice
        cout <<endl;
        cout << "1  Display monthly amounts" << endl;
        cout << "2  Display total amount" << endl;
        cout << "3  End program" << endl;
        cout << "Enter your choice : ";
        cin >> choice;

        //call appropriate function or end program
        if(choice ==1)
            displayMonthly(rainfall);
        else if(开发者_StackOverflow社区choice == 2)
            displayTotal(rainfall);
    }while (choice == 1 || choice ==2);

    return 0;
}


It's telling you to keep looping as long as the choice is 1 or 2, but it's entirely separate from the if statement.

Could also be coded as

while(true) {
    cout stuff...

    if(choice==1)
        ...
    else if(choice==2)
        ...
    else
        break;
}

Which may be a little more readable but some old schoolers will freak right out if they see while(true)--that used to be drummed into people as a big red flag indicating a potential bug (Apparently by anyone completely unable to analyze code since the functionality is no different).


This is a do ... while loop. The idea is that the code within the block will continue executing as long as the condition (choice == 1 || choice ==2) is true.

Here is more information from a cplusplus.com article on Control Structures:

do statement while (condition);

Its functionality is exactly the same as the while loop, except that condition in the do-while loop is evaluated after the execution of statement instead of before, granting at least one execution of statement even if condition is never fulfilled. For example, the following example program echoes any number you enter until you enter 0.

The do-while loop is usually used when the condition that has to determine the end of the loop is determined within the loop statement itself, like in the previous case, where the user input within the block is what is used to determine if the loop has to end. In fact if you never enter the value 0 in the previous example you can be prompted for more numbers forever.

You can also think of is this way. You might use a do-while structure if you always want the code within the block to be executed at least once. This is exactly the case in your program since you always want to display the prompt, and can only decide whether the loop will continue after you receive and process the user's input.


In this app, if the user hits 1 or 2, it means that they wanted to see either monthly or total rainfall. What this implies is that they haven't requested to 'End program'. The code assumes that if the user has chosen 1 or 2 that they should continue using the application. If they have chosen ANYTHING else, (eg. 3, just like the menu says) then the condition choice == 1 || choice == 2 will evaluate to false and the loop with terminate, resulting in the application closing.


Basically what the do ... while loop here does is keeps repeating its self as long as the choice is 1 or 2.

After the 'do', the user is prompted to enter their choice, 1 2 or 3. If they choose 1 or 2, their respective function is called. Then the loop repeats. If they choose 3, the loop does not repeat so the program ends.

Hope this helped!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号