- Post Reply Bookmark Topic Watch Topic
- New Topic

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

posted 3 weeks ago

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?

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?

posted 3 weeks ago

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.

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

posted 3 weeks ago

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

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

posted 3 weeks ago

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?

i suppose you could use &&

but you could also do this and it works

but i dont really understand why

which would you prefer?

posted 3 weeks ago
` && `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?

Look up de Morgan's law.∴Your last formula contains double negatives, but they eventually resolve to give you the test you wanted.Eric Flynn wrote:. . . it works . . . but i dont really understand why

None of them. The formula with thewhich would you prefer?

posted 3 weeks ago

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

Campbell Ritchie wrote:but what about this?

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 && !q == !(p || q)

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

which is shorthand for

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

*The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.*—Kent Beck

[How to Ask Questions] [How to Answer Questions]

posted 3 weeks ago

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)`

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 wrote:

... but you could also do this...

but i dont really understand why

Applying DeMorgan's Law:

is equivalent to

Simplifying, you get

you wrote:... and it works

Hmmm... are you

*The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.*—Kent Beck

[How to Ask Questions] [How to Answer Questions]

Eric Flynn

Greenhorn

Posts: 24

posted 3 weeks ago

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?

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

posted 3 weeks ago

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

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.

*The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.*—Kent Beck

[How to Ask Questions] [How to Answer Questions]

posted 3 weeks ago

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.

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

It depends on what

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.

*The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.*—Kent Beck

[How to Ask Questions] [How to Answer Questions]

Campbell Ritchie

Marshal

Posts: 65806

250

posted 3 weeks ago

Yes, I think you are right.Junilu Lacar wrote: . . . . I think you meant

. . ..

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

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

- Post Reply Bookmark Topic Watch Topic
- New Topic

Boost this thread!