5 Replies - 187 Views - Last Post: 02 February 2016 - 05:42 PM Rate Topic: -----

#1 King8785  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 83
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 King8785  Icon User is offline

  • New D.I.C Head

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

Re: order of operations question

Posted 02 February 2016 - 04:43 PM

View PostBrokenprogrammer, 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?
Was This Post Helpful? 0
  • +
  • -

#4 Brokenprogrammer  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 83
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 319
  • View blog
  • Posts: 2,349
  • 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

Was This Post Helpful? 1
  • +
  • -

#6 King8785  Icon User is offline

  • New D.I.C Head

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

Re: order of operations question

Posted 02 February 2016 - 05:42 PM

View PostBrokenprogrammer, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1