while loop error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

while loop error

hy guys, can you tell me where am i mistaking please; #include <iostream> using namespace std; int main() { int x,y; cout << "Enter a number from 3 to 20 !" << endl; cin>>x; y=x+17; while(x<3 && x>20) { cout<< "Error ,enter a number from 3 to 20 !"<<endl; cin>>x; } cout<<"The new number is : "<<y<<endl; return 0; }

16th Feb 2019, 10:47 PM
Akagami
Akagami - avatar
2 Answers
+ 6
Your condition of the while loop is a logical AND. But a number x can not be smaller than 3 AND larger than 20 at the same time, so your loop is never executed. What you need here is an OR (written as two pipe symbols like this || ).
16th Feb 2019, 10:51 PM
merkrafter
0
ah okay thank you
16th Feb 2019, 10:58 PM
Akagami
Akagami - avatar