Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chrono1081

macrumors G3
Original poster
Sorry for my second post in such a short time, but I just wondered if there is something wrong here.

I was doing an exercise from a book which is pretty simple but it wasn't working correctly. The culprit was a do-while loop. A shortened version of the code is this:

Code:
char operator;

do {
         //do stuff here regarding the value of operator

}while ( (operator != 'e') || (operator != 'E') )

For some reason, the loop never exits even though operator gets set to e or E. The code works perfectly fine if I change the while section to one of the following:

Code:
while(operator != 'e') //here I only put in a lowercase e and it works
while(operator != 'E') //here I only put in an uppercase E and it works
while(toupper(operator) != 'E') //again, this works

Am I unable to make comparisons in the do-while loops arguments? Since operator is equal to 'e' OR 'E' the loop should break, but it doesn't unless I remove the comparison.
 
Sorry for my second post in such a short time, but I just wondered if there is something wrong here.

I was doing an exercise from a book which is pretty simple but it wasn't working correctly. The culprit was a do-while loop. A shortened version of the code is this:

Code:
char operator;

do {
         //do stuff here regarding the value of operator

}while ( (operator != 'e') || (operator != 'E') )

For some reason, the loop never exits even though operator gets set to e or E. The code works perfectly fine if I change the while section to one of the following:

Code:
while(operator != 'e') //here I only put in a lowercase e and it works
while(operator != 'E') //here I only put in an uppercase E and it works
while(toupper(operator) != 'E') //again, this works

Am I unable to make comparisons in the do-while loops arguments? Since operator is equal to 'e' OR 'E' the loop should break, but it doesn't unless I remove the comparison.

Fill in the boolean values of the sub-expressions:

e: (operator != 'e') -> NO
(operator != 'E') -> YES
NO || YES -> YES

E: (operator != 'e') -> YES
(operator != 'E') -> NO
YES || NO -> YES

You need && instead of ||.
 
I think you want && not ||.

Code:
 op | A = (op != 'e') | B = (op != 'E') | A && B | A || B
----+-----------------+-----------------+--------+--------
'E' | True            | False           | False  | True
'e' | False           | True            | False  | True
'q' | True            | True            | True   | True
 
Ohhhh I'm an idiot. Thanks guys!

Its time for bed. I've been goofing up non-stop for the past hour and a half 😛
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.