# order of operations question

Page 1 of 1

## 5 Replies - 179 Views - Last Post: 02 February 2016 - 05:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=388224&amp;s=e8cb63dee97301a3d2d5936708bd7bea&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 King8785

• New D.I.C Head

Reputation: 0
• Posts: 12
• Joined: 02-February 16

# order of operations question

Posted 02 February 2016 - 04:28 PM

I don't understand why this program outputs 5 instead of 8
```boolean valA = true;
int valB = 32;
int valC = 3;
valA = !valA || valB % valC == 0;
if(valA)
valC--;
valB /= 2 * valC;
print(valB);

```

More specifically I don't know what affect valA = !valA || valB % valC == 0; has on the program. What is occurring to change the value from 8 to 5?
Is This A Good Question/Topic? 0

## Replies To: order of operations question

### #2 Brokenprogrammer

Reputation: 12
• Posts: 80
• Joined: 05-January 16

## Re: order of operations question

Posted 02 February 2016 - 04:33 PM

valA is the boolean used in the if statement, in this case it looks like it just decrements valC.

If the value is decremented valB is divided by 4 instead of 6 causing the result to become either 8 or 5.33.

So in this program the statement:
```valA = !valA || valB % valC == 0;
```

Is what is affecting the result of the calculation.

This post has been edited by Brokenprogrammer: 02 February 2016 - 04:35 PM

### #3 King8785

• New D.I.C Head

Reputation: 0
• Posts: 12
• Joined: 02-February 16

## Re: order of operations question

Posted 02 February 2016 - 04:43 PM

Brokenprogrammer, on 02 February 2016 - 04:33 PM, said:

valA is the boolean used in the if statement, in this case it looks like it just decrements valC.

If the value is decremented valB is divided by 4 instead of 6 causing the result to become either 8 or 5.33.

So in this program the statement:
```valA = !valA || valB % valC == 0;
```

Is what is affecting the result of the calculation.

What do you mean valA decrements valC? also if valB is divided by 4 instead of 6 should I be getting an output of 8 instead of an output of 5?
When I plug it in I get a value of 5 for valB. Is it giving me this output because valA is false? So it's using the 3 instead of making valB 2? If this were the case then wouldn't it also ignore the line with the equation in it?

### #4 Brokenprogrammer

Reputation: 12
• Posts: 80
• Joined: 05-January 16

## Re: order of operations question

Posted 02 February 2016 - 04:46 PM

In this case if the if statement is executed the valC should be decremented. What i meant is that valA is the value deciding if ValC should be decremented or not because of the if statement in the code.

You can count the expected result for yourself (32 / 4 = 8) or (32 / 6 = 5.33)

In this case if you're getting the result of 5 it was because valA is false. Only if valA is true the valC will be decremented.

The line with the calculation is not ignored since its not wrapped in opening and closing braces { and }

I would suggest you to use better names for variables. Its much easier to follow along in the code. You should also try to use proper indentation.

This post has been edited by Brokenprogrammer: 02 February 2016 - 04:50 PM

### #5 NormR

• D.I.C Lover

Reputation: 313
• Posts: 2,269
• Joined: 25-December 13

## Re: order of operations question

Posted 02 February 2016 - 05:01 PM

Quote

I don't understand why this program outputs 5 instead of 8

Try debugging it while it executes to see what each statement does.
If you don't know how to use an IDE's debugger, add a print statment after each statement that prints out the results of that statement. The print out will show you what the code is doing.
For example:
``` int x = 1;
System.out.println("x="+x); // prints x=1
x += 5;
System.out.println("x="+x); // prints x=6

```

Quote

I would suggest you

Also be sure to use {} with if statements
and be sure to use () with arithmetic expressions so there is no confusion on the order of the operations.

This post has been edited by NormR: 02 February 2016 - 05:12 PM

### #6 King8785

• New D.I.C Head

Reputation: 0
• Posts: 12
• Joined: 02-February 16

## Re: order of operations question

Posted 02 February 2016 - 05:42 PM

Brokenprogrammer, on 02 February 2016 - 04:46 PM, said:

In this case if the if statement is executed the valC should be decremented. What i meant is that valA is the value deciding if ValC should be decremented or not because of the if statement in the code.

You can count the expected result for yourself (32 / 4 = 8) or (32 / 6 = 5.33)

In this case if you're getting the result of 5 it was because valA is false. Only if valA is true the valC will be decremented.

The line with the calculation is not ignored since its not wrapped in opening and closing braces { and }

I would suggest you to use better names for variables. Its much easier to follow along in the code. You should also try to use proper indentation.

Thanks for the help. I get what you mean now. I really appreciate it.