Jump to content

logical operands

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Fredrik373

Fredrik373

    Newbie

  • Members
  • Pip
  • 7 posts
Hello!

I am quite new to Java and I have a problem with one of my programs.

The code inside the if-command should be executed only if the following statements are fulfilled.

If:

X > 100
Y > 100
(X and y) > 100

I am trying to write an if command as follows..

if (((x > 100) && (y > 100)) || ((x > 100) || (y > 100)))
{
//some code
}

What am I doing wrong?

#2
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Well the first part of your if-statement is actually redundant, as if both X and Y are bigger than 100, the "OR" side of the if statement still returns true.

What exactly is the problem though? Is it throwing you an error? Or is it just not executing the code how you would expect. :)
My Company - My Homepage - My Twitter - My Google+ - My LinkedIn

"Things don’t have to change the world to be important.” - Steve Jobs

#3
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Variables are lowerCamelCase.

anyway..

X > 100
Y > 100
(X and y) > 100

#4
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
I don't see where you fitt in the OR operator if you want to get an evalueation if x AND y are greater than 100 it's enough to just write if (x > 100 && y > 100) { .... }

#5
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
and btw if you're new to java it could be a good thing to know that singular & and | are also logical operands used in evaluation (unlike other languages that use those on binary numbers of values to get a result in binary code instead of false or true). In java they serve as full or complete evaluation && for instance goes through statements from left ti right and the first false statement it finds it doesn't evaluate the rest, the & on the other hand evaluates all statements regardless if it has already passed a false statement that makes the whole evaluation of false value.

#6
Fredrik373

Fredrik373

    Newbie

  • Members
  • Pip
  • 7 posts

so1i said:

Well the first part of your if-statement is actually redundant, as if both X and Y are bigger than 100, the "OR" side of the if statement still returns true.

What exactly is the problem though? Is it throwing you an error? Or is it just not executing the code how you would expect. :)

OK I see now what you mean. The problem was that the program was ignoring parts of my code, but now it works fine! :)

Edited by Fredrik373, 02 October 2010 - 07:55 AM.