Author Topic: Logic order of operators  (Read 2752 times)

0 Members and 1 Guest are viewing this topic.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Logic order of operators
« on: December 29, 2018, 10:54:08 am »
does multiplication still processed before addition and subtraction?
« Last Edit: December 29, 2018, 01:26:30 pm by xra7en »
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline Gets

  • Newbie
  • Posts: 28
    • View Profile
Re: Logic order of operators
« Reply #1 on: December 29, 2018, 01:48:47 pm »
Code: QB64: [Select]
  1.  
  2. a(1, 1) = 1
  3. b.q1 = 5
  4. c = 3
  5.  
  6. PRINT b.q1 + a(1, 1) * c
  7.  
  8.  

prints "8", so multiplication comes first.

The code snippet you edited out didn't provide much information, so I can only guess that  some variable isn't being passed to it correctly. Add a printout of the active variables and if statement equations to the error message  to see if you can isolate anything going wrong, I guess?

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: Logic order of operators
« Reply #2 on: December 29, 2018, 06:58:23 pm »
after a second look I do not think it is a logic issue as it is where I am placing the operators to check, I need to rearrange the if/then/else
normally I do not have to use parentheses as the logic order works fine.
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline freetrav

  • Newbie
  • Posts: 45
    • View Profile
Re: Logic order of operators
« Reply #3 on: January 02, 2019, 08:43:35 am »
Unless documented otherwise, you should always assume - in any language - that the order of evaluation is as follows:

1. Parentheses (grouping)
2. Function Calls
3. Negation (Unary -)
3. Exponentiation (if available, e.g., FORTRAN **)
4. Multiplication and Division (left to right)
5. Addition and Subtraction (left to right)

Note that, for example, APL explicitly documents that evaluation is strictly right-to-left, not hierarchical.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Logic order of operators
« Reply #4 on: January 02, 2019, 07:03:36 pm »
Hi
IMHO() let us to manage the order of processing the math operation but we must remember the original order of processing operation
http://qb64.org/wiki/Mathematical_Operations#Basic.27s_Order_of_Operations

here just an example
Code: QB64: [Select]
  1. DEFINT A-Z
  2. a = 10
  3. b = 2
  4. c = 3
  5.  
  6. PRINT "a="; a; " b="; b; " c="; c; " d="; d; " e="; e
  7. PRINT "a - b * c= "; a - b * c; " (a-b) * c="; (a - b) * c
  8. PRINT " a + b * c= "; a + b * c; "  (a + b) * c= "; (a + b) * c
  9. PRINT " a + b / c= "; a + b / c; "  (a + b) / c= "; (a + b) / c
  10. PRINT " a + b MOD c= "; a + b MOD c; "  (a + b) MOD c= "; (a + b) MOD c
  11. PRINT " a MOD b * c= "; a MOD b * c; "  (a MOD b) * c= "; (a MOD b) * c
  12. PRINT "a ^ b *c="; a ^ b * c; "(a ^ b) *c="; (a ^ b) * c
  13. PRINT "a ^ b *c="; a ^ b * c; "a ^ (b *c)="; a ^ (b * c)
  14.  
  15.  

I hope it will clear
Programming isn't difficult, only it's  consuming time and coffee