循环使用&&

时间:2016-06-01 06:43:08

标签: c++ visual-c++

while循环应保持循环,直到两个条件都为真,但只有一个变为真时才会停止。为什么呢?

#include <iostream.h>
int main()
{
    int programsdone, result;
    cout<<"You must complete 5 programs and have a result of at least 50"<<endl;
    cout<<"Enter the number of programs completed"<<endl;
    cin>>programsdone;
    cout<<"Enter your result"<<endl;
    cin>>result;
    while (programsdone<5 && result<50)
    {
        cout<<"You need more programs or a higher result"<<endl;
        cout<<"Enter the number of programs again"<<endl;
        cin>>programsdone;
        cout<<"Enter result again"<<endl;
        cin>>result;
    }
    cout<<"congrats"<<endl;
    return 0;
}

1 个答案:

答案 0 :(得分:1)

while循环只在两个条件为真时继续运行,直到它们变为真。如果你希望程序在两个条件都为真时停止,只需使用否定。

!(programsdone<5 && result<50)