# Why is my condition statement met? (% operator)

• 04-02-2012, 03:54 PM
EscSequenceAlpha
Why is my condition statement met? (% operator)
I can not figure this out. The task is simple, is number1 a multiple of number2 - so I use the % to find out if there is a remainder.

but look at this

Code:

package chapter_ii;

import java.util.Scanner;

public class Multiples
{
float number1;
float number2;
boolean multiple;

public static void main (String[] args)
{
int number1;
int number2;

Scanner input = new Scanner(System.in);

number1 = input.nextInt();
number2 = input.nextInt();

Multiples Multi = new Multiples(number1, number2);

Multi.calculate();

}

public Multiples(int number1, int number2)
{
this.number1 = (float)number1;
this.number2 = (float)number2;
}

public void calculate()
{
if ((number1 % number2) == 0.0)
multiple = true;
else
multiple = false;

if (multiple = true)
System.out.println(number1 + " is a multiple of " + number2);
else
System.out.println(number2 + " is not a multiple " + number2);

System.out.println(number1 + " % " + number2 + " = " + (number1 % number2) );
System.out.println(multiple);
}
}

the output I get is

5
2
5.0 is a multiple of 2.0
5.0 % 2.0 = 1.0
true

whatever I put in I get "true" for the multiple variable... the test I put at the end proves != 0 so why is the condition

Code:

if ((number1 % number2) == 0.0)
• 04-02-2012, 04:01 PM
JosAH
Re: Why is my condition statement met? (% operator)
Don't compare two boolean values with the = operator (that's the assignment operator); use the == operator instead. Even better don't compare a boolean value against a constant boolean value.

kind regards,

Jos
• 04-02-2012, 06:58 PM
EscSequenceAlpha
Re: Why is my condition statement met? (% operator)
Quote:

Originally Posted by JosAH
Don't compare two boolean values with the = operator (that's the assignment operator); use the == operator instead. Even better don't compare a boolean value against a constant boolean value.

kind regards,

Jos

thank you, I cant believe I missed that - you can see I knew what the equals operator was, because I used it in the first if statement.

so on my second if statement I wrote if (multiple = true) by mistake

I'm surprised that even compiled, because if statements arent really statements they're conditionals (doesnt have a semi colon like statements do)... does this mean that when using an if statement you can say if(a = 3) {do this} and it will treat it as a statement and assign 3 then do the task? thats odd I would have imagined a compile error.

well thats how I learn. thanks I'll try to get better at seeing stuff like this
• 04-02-2012, 08:02 PM
JosAH
Re: Why is my condition statement met? (% operator)
Quote:

Originally Posted by EscSequenceAlpha
thank you, I cant believe I missed that - you can see I knew what the equals operator was, because I used it in the first if statement.

so on my second if statement I wrote if (multiple = true) by mistake

I'm surprised that even compiled, because if statements arent really statements they're conditionals (doesnt have a semi colon like statements do)... does this mean that when using an if statement you can say if(a = 3) {do this} and it will treat it as a statement and assign 3 then do the task? thats odd I would have imagined a compile error.

well thats how I learn. thanks I'll try to get better at seeing stuff like this

An if statement takes a boolean epression; an assignment is an expression and in your case (you assigned true to a boolean variable) it is a boolean expression. You can't do if(a = 3) because a = 3 isn't a boolean expression. Check Java's syntax in the JLS for all the gory details.

kind regards,

Jos