# Thread: [SOLVED] How can I dynanically change math signs

1. ## [SOLVED] How can I dynanically change math signs

Hi Gang:
What I'm looking for is a way to dynamically change the plus "+" or minus "-" signs in my program (I searched the forum and didn't see anything similiar).

Explanation:
• If the condition is x, then use "+" in all math operations
• If the condition is y, then use "-" in all math operations

I know that I can have two sections in the program, one that adds and the other that subtracts, but it would be easier (and more efficient) if could establish this from the begining since the two sections are the same except for the math operations.
Thanks
Chris

2. Originally Posted by CJSLMAN
Hi Gang:
What I'm looking for is a way to dynamically change the plus "+" or minus "-" signs in my program (I searched the forum and didn't see anything similiar).

Explanation:
• If the condition is x, then use "+" in all math operations
• If the condition is y, then use "-" in all math operations

I know that I can have two sections in the program, one that adds and the other that subtracts, but it would be easier (and more efficient) if could establish this from the begining since the two sections are the same except for the math operations.
Thanks
Chris
Use if-else conditions. That's all. Why you can't do it?

3. Senior Member
Join Date
Aug 2008
Posts
384
Rep Power
5
Indeed, I don't really see the problem here either...
Just use if statements or multiply some variables with -1...

4. Agreed, but I'm trying to avoid repeating the same code twice where the only difference is the math sign. Something like (simple pseudo code):
Java Code:
```if (conditon = x)
{
mathSign = +;
}
else if (conditon = y)
{
mathSign = -;
}
result = 100 mathSign 10; //depending on the value of mathSign the answer
//could be 90 or 110```
Thanks,
Chris

5. Using the ternary operator
Java Code:
`result = 100 + 10 * (conditon ? 1 : -1);`
Using if-else
Java Code:
```int sign = 0;
if (conditon) {
sign = 1;
} else {
sign = -1;
}
result = 100 + 10 * sign;```
db
Last edited by DarrylBurke; 10-04-2008 at 08:01 PM. Reason: missed a code tag

6. ## [SOLVED] How can I dynanically change math signs

Supamagier & Darryl.Burke... thanks for the responses. Multiplying by the Sign variable will do the job.
Thanks,
Chris

7. Originally Posted by CJSLMAN
Multiplying by the Sign variable will do the job.
What you mean by sign variable? Depends on your requirements either use if-else of ternory.

8. ## OK... here we go again

I will try to be as explicit as possible:
Problem: I needed to be able to change the math sign (+ or -) depending on some established condition:
• If the condition is x, then use "+" in all math operations
• If the condition is y, then use "-" in all math operations

What I didn't want to do was program two separate sections, one using the plus sign and the other using the minus sign, since the logic is the same in both sections.
Solution: Establishing a variable (sign variable) that contains a 1 or -1 (see sign variable used in Daryl Burkes example below) provides the solution I was looking for:
Java Code:
```int sign = 0;
if (conditon) {
sign = 1;
} else {
sign = -1;
}
//logic follows here...
result = 100 + 10 * sign;```
The ternary operator solution also provides a solution, but I would have to implemented in each line that needed the + or - sign. That is why I chose the solution stated above.
Thanks

9. Senior Member
Join Date
Aug 2008
Posts
384
Rep Power
5
ternary operator, what's that?

EDIT: nvm
Last edited by Supamagier; 10-06-2008 at 05:08 PM.

10. Originally Posted by Supamagier
ternary operator, what's that?

EDIT: nvm
You don't know about ternary operator? Amazing, how long you are programming in Java, or any other language?

Ternary operator is the way to write complex if-else syntax, with minimal amount of coding.

like this,
Java Code:
`sign = (conditon) ? 1 : -1;`

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•