0

NOT operator in C#

Hi. I'm making a Currency Convert for USD and EUR. I make the user type int to select currency conversion(1/2). But I wanted to make a while loop so when the user types something else, the program will ask for input again. It's working fine but now it keeps asking me for input for every number I type. Is my while loop not good? -while( !(currency == 1) || !(currency == 2)-

7th Jun 2022, 5:32 AM
Nektarios Kouromichelakis
Nektarios Kouromichelakis - avatar
1 Odpowiedź
+ 5
The loop terminates when its terminating condition becomes false. When does it become false? !A or !B ≡ !(A and B), and that is false if A and B is true, which is true if both A and B are true. That means in your case, it terminates if currency is both 1 and 2. And that is never the case. You want &&, I think. And !(x == y) is usually written (x != y).
7th Jun 2022, 6:09 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar