programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Junilu Lacar
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Knute Snortum
• Tim Cooke
• Devaka Cooray
Saloon Keepers:
• Ron McLeod
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Carey Brown
Bartenders:
• Piet Souris
• Frits Walraven
• Ganesh Patekar

# Why this while doesn't work as expected?

Greenhorn
Posts: 24
i am new to all this and need some help doing a simple exercise

how come the while loop doesnt exit?

i thought it meant while userNumber not equal to 1 or 2 or 3 or 4 keep looping
why doesnt it exit when i press 1 2 3 or 4?

Saloon Keeper
Posts: 5809
146
Welcome to the Ranch.

At least 3, if not all 4, of the sub-expressions will always be true, and so the entire expression will always be true.

Rather than || you ought to use a different combinator.

Eric Flynn
Greenhorn
Posts: 24
if 4 is entered, am i right by saying

F                             T                            T                           T

so the loop keeps running because aspects of it are true

so instead of || && will work as

T                             F                            F                           F

so it exists cause the whole condition is False

Eric Flynn
Greenhorn
Posts: 24
Hi Tim thanks!

i suppose you could use &&

but you could also do this and it works

but i dont really understand why

which would you prefer?

Marshal
Posts: 65806
250

Eric Flynn wrote:. . . it works . . . but i dont really understand why

Look up de Morgan's law.Your last formula contains double negatives, but they eventually resolve to give you the test you wanted.

which would you prefer?

None of them. The formula with the && is better, but what about this?This isn't so much a C++ question as a general question about Boolean algebra. Don't people teach Boolean algebra any more?

Marshal
Posts: 14046
234

That conditional does not do what the OP intends though: it will only accept inputs in the range of (-4 .. -1). I think you meant

@OP: if the notation Campbell gave looks strange, it's because that's the formal notation used in Boolean Algebra. Translated to Java/C++ notation, it becomes more familiar:

Given boolean values p and q,

!p || !q == !(p && q)
!p && !q == !(p || q)

If you still remember your high school algebra, it looks very similar to the distributive property of multiplication over addition:

-a + -b == -(a + b)

which is shorthand for

-1a + -1b == -1(a + b)

It might help you understand the workings of the boolean operators ||, &&, and ! if you study the truth tables for OR, AND, and NOT

Junilu Lacar
Marshal
Posts: 14046
234

Eric Flynn wrote:
... but you could also do this...

but i dont really understand why

Applying DeMorgan's Law:

!(userNumber != 1 || userNumber != 2 || userNumber != 3 || userNumber != 4)

is equivalent to

!(userNumber != 1) && !(userNumber != 2) && !(userNumber != 3) && !(userNumber != 4)

Simplifying, you get

(userNumber == 1) && (userNumber == 2) && (userNumber == 3) && (userNumber == 4)

you wrote:... and it works

Hmmm... are you sure it works? Because the boolean math says otherwise (and so do my tests). That expression will evaluate to false for all values of userNumber.

Eric Flynn
Greenhorn
Posts: 24
hi Junilu Lacar it seemed to work ok with me and the loop terminated when 1 to 4 were entered, am i missing something?

so is && preferred to|| ,   || does it just depend on the situation && what you are trying to do?

Junilu Lacar
Marshal
Posts: 14046
234

Eric Flynn wrote:hi Junilu Lacar it seemed to work ok with me and the loop terminated when 1 to 4 were entered, am i missing something?

You probably missed checking whether it also terminated when you entered an invalid value.

Junilu Lacar
Marshal
Posts: 14046
234

Eric Flynn wrote:so is && preferred to|| ,   || does it just depend on the situation && what you are trying to do?

It depends on what you're you|| ( ) trying to do, really.

In computer programming, the value of DeMorgan's Law is in reducing complex expressions to simpler ones so they are more understandable. When faced with a complicated boolean expression like what you have, simpler is better since it's easier to see where a bug might be in the logic. However, to write bug-free code, I rely heavily on unit tests and refactoring to clarify my intent. Here's what I might have done with your code:

DeMorgan's Law has practical applications in the real-world. In digital electronics, NAND gates are cheaper and more plentiful than other types of gates so if you can use a NAND gate instead of an AND gate plus a separate inverter or an OR gate plus multiple inverters, then you can save on cost.

Logic circuits are usually designed using boolean algebra. You can apply DeMorgan's Law so that terms in the boolean expressions correspond directly to physical logic gates. For example, if you designed a circuit that had two inputs A and B and the output needed represented by the expression (!A || !B) then instead of using three components, two inverters (the two ! operations) and one OR gate, you'd use DeMorgan's Law to get the equivalent !(A && B), which translates to just one component: a NAND gate with A and B as its inputs. That's a much simpler and cheaper physical implementation.

Campbell Ritchie
Marshal
Posts: 65806
250

Junilu Lacar wrote: . . . . I think you meant
. . ..

Yes, I think you are right.

. . . if the notation Campbell gave looks strange . . ..

. . . and the fact that every logic book I have read seems to have a slightly different notation doesn't help.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?