How to do and 'and' and an 'or' in the one if statement?
Please show me if there is an easier way but i'm just stuck at the moment with this code
if((currentYear - aVehicle.getYearOfManufacture() >= 7) || (currentYear - aHGV.getYearOfManufacture() >= 4))
{
System.out.println("This vehicle needs a roadworthy test:");
System.out.println(aVehicle.toString());
System.out.println(aOwner.toString());
}else{
System.out.println("No vehicles need a road test yet");
}
I havn't bothered pasting all the code as i don't think there's any need. What i want this method to be able to do is:
if the currentYear minus the vehicles year of manufacture >=7 AND/OR the currentYear minus the HGV's year of manufacture >=4
its prints out the correct procedure.
I don't know how to implement the AND/OR part if you understand? As you can see all i have in is the ||. I tried ||&& but that gives me comile error as you probably know
Thanks.
Message was edited by:
Capsud
[1243 byte] By [
Capsuda] at [2007-11-27 4:28:13]

> could he use a boolean of booleans, ie
>
> if(condition 1 = false && condition 2 = false {
> return false;
> } else {
> return true;
> }
>
> New to java but thats how i would try doing it.
>
> Doo
then you would be doing much more work than needed.
Using || is the simplest way, and the simplest code is the easiest code to maintain.
> > then you would be doing much more work than
> needed.
> >
> >
> > Using || is the simplest way, and the simplest
> code
> > is the easiest code to maintain.
>
> But in theroy would it work? Get it working, see it
> working, make it better?
No, what he wants is to check if either both are true, (which && and || both would work for) or if one is true and one is false, (which || only would work).
condition1 | condition2 | && |||
--
T|T| T|T
T|F| F|T
F|T| F|T
F|F| F|F
no using && is not the same as and/or
Best way to remember it is the following:
when someone asks you if you want cream or sugar in your coffee that is an inclusive or -you can get both of them
when someone asks you if you want a car or a truck that is an exclusive one - you only get one of the two but not both
Aside: if you want exclusive-or (soup or salad) you can write:
if ( A ^ B ) {
}
One could make an aside here about conditional execution, but I don't nest asides!
Extra credit: when is (A ^ B) ^ C true? How about A1 ^ A2 ^ ... An?