clarification

I have small puzzle, with operator || and &&.

when I tried to break from do/while loop I tried this condition

 
 while (ans != 'n' || ans != 'N');


expecting to work in such way if while ans is not equal to n OR N continue but this is not breaking from the loop. On other hand if I use this:

 
 while (ans != 'n' && ans != 'N');

my understanding was that if while ans is not equal to n AND ans is not equal to N continue

so with || operator i was expecting if at least one condition is true break
and with && that both conditions needs to be true to break.






you've got
1
2
3
do{
   //statements
}while( condition );
the loop will end when condition is false.


> expecting to work in such way if while ans is not equal to n OR N continue
> so with || operator i was expecting if at least one condition is true break
¿break or continue? decide yourself.


make a true table
ans   a = ans!='n'   b = ans!='N'   a or b  a and b
'n'      0              1             1        0
'N'      1              0             1        0
'y'      1              1             1        1

¿can you imagine a value for ans so that both `a' and `b' are false?
Last edited on
HI @ne555

just to check I am not making basic mistake,
false = 0 True = 1 correct?

I wanted to break from the loop when one of the condition is met ie. if ans is equal to lower case or upper case N.
so if users types n loop will break, if user types N loop will break. If user types anything else loop will happily continue.

but in your example you if I understand it correctly is the other way round :/

ps I find confusing/annoying true and false arguments. above example
if ans is not equal to zero is True, otherwise is false so its zero. On the other hand if program finish successfully it return 0.
false = 0
true = 1


> I wanted to break from the loop when one of the condition is met
> ie. if ans is equal to lower case or upper case N.
ans != 'n' || ans != 'N' does not mean «if ans is equal to lower case or upper case N»
for that you need to write ans == 'n' or ans == 'N'
however, to exit the loop you need the condition to be false, so negate it
not (ans == 'n' or ans == 'N')

¿do you know De Morgan? https://en.wikipedia.org/wiki/Demorgan_Law
not (ans == 'n' or ans == 'N') may be written as ans not_eq 'n' and ans not_eq 'N'


> ps I find confusing/annoying true and false arguments. above example if ans
> is not equal to zero is True, otherwise is false so its zero. On the other
> hand if program finish successfully it return 0.
I have no idea what you are talking about.
Try to rephrase it.
Hi ne555

first part I is crystal clear :) thanks.

De Morgan Law - never came across it (however during my school years its possible it was explained but without explicit name)

In my last section it was just remark that its confusing use of True and false in c++ as not always 0 is false and 1 true. (example I have used when program terminates with return 0 it is considered success)
Example: I have used when program terminates with return 0 it is considered success.
Don't get confused by this: your program returns an error code. That is, it answers the question "what error occurred?", and in a Boolean context, consequentially "was there an error at all?".

This is generally consistent, but IMO it makes more sense if you think about return 0 not as "return success" but as "return no error" or "I have no error to describe" or "I did not fail".
Last edited on
@xxvms

The entire problem can be avoided if one makes use of either the toupper or tolower functions, then one only has to test against one of them not both.

char AnswerAsUpper = toupper(answer);

I have a pathological hatred for UGLY constructs like this, especially if one tries to scale them:

while (ans != 'A' && ans != 'B' && ans != 'C');

In such cases, one is better off using a switch IMO

Last edited on
@mbozzi - that is actually a great explanation! simple but to the point. thank you :)

@TheIdeasMan - that sounds interesting :) I will explore toupper and tolower!
on other hand I do appreciate your words but at learning stage you have no idea what is ugly or pretty constructor. :D you barely know that there is a constructor ;)
Hi,

Not constructor, that is a different concept. Perhaps I should have said expression :+)

I guess ugly is a long series of conditionals, versus something simple.
Topic archived. No new replies allowed.