Author Topic: Is there a shortcut way to add something to itself?  (Read 3170 times)

0 Members and 1 Guest are viewing this topic.

Offline Raptor88

  • Newbie
  • Posts: 22
    • View Profile
Is there a shortcut way to add something to itself?
« on: April 18, 2019, 07:15:15 pm »
In QB64, is there a shortcut way to add something to itself?

For example:
MyRunningTotal = MyRunningTotal +1
-or-
MyRunningTotal = MyRunningTotal + 22

A shortcut to negate having to retype MyRunningTotal again on the right side of the equation?

In C, I believe to increment by 1, the following can be used:
MyRunningTotal++

Something similar in QB64?

FellippeHeitor

  • Guest
Re: Is there a shortcut way to add something to itself?
« Reply #1 on: April 18, 2019, 07:39:29 pm »
Nope.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Is there a shortcut way to add something to itself?
« Reply #2 on: April 18, 2019, 08:51:21 pm »
You can get pretty close though:
Code: QB64: [Select]
  1. variable = 0
  2. 'variable += 1
  3. pe variable, 1 'close!
  4. PRINT variable
  5. pe variable, 10
  6. PRINT variable
  7. pe variable, 100
  8. PRINT variable
  9.  
  10. 'pe stands for plus equal +=
  11. SUB pe (number, plus)
  12.     number = number + plus
  13.  
  14.  


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Is there a shortcut way to add something to itself?
« Reply #3 on: April 18, 2019, 09:41:04 pm »
Wait... If you thought the last reply was fun, look at this!
Code: QB64: [Select]
  1. variable = 0
  2. e variable, "+10" 'variable = variable + 10
  3. PRINT "0 + 10 ="; variable
  4. e variable, "-5" 'variable = variable - 5
  5. PRINT "10 - 5 ="; variable
  6. e variable, "*3" 'variable = variable * 3
  7. PRINT "5 * 3 ="; variable
  8. e variable, "/3" 'variable = variable / 3
  9. PRINT "15 / 3 ="; variable
  10. e variable, "^3" 'variable = variable ^ 3
  11. PRINT "5 ^ 3 ="; variable
  12. e variable, "%10" 'variable = variable mod 10
  13. PRINT "125 mod 10 ="; variable
  14. e variable, "|0" 'variable = (variable OR 0)
  15. PRINT "5 OR 0 = "; variable
  16. e variable, "&1" 'variable = variable AND (logical) 1
  17. PRINT "-1 AND (logical) 1 ="; variable
  18. e variable, "!-1" 'variable = (variable <> -1)
  19. PRINT "-1 NOT -1"; variable
  20. e variable, "=0" 'variable = (variable == 0)
  21. PRINT " 0 = (equivalence) 0 ="; variable
  22.  
  23.  
  24. 'e is for evaluate, change$ syntax: first character is operator symbol, the rest is a number string.
  25. SUB e (number, change$)
  26.     delta = VAL(RIGHT$(change$, LEN(change$) - 1))
  27.     'PRINT delta
  28.     SELECT CASE LEFT$(change$, 1)
  29.         CASE "+": number = number + delta
  30.         CASE "-": number = number - delta
  31.         CASE "*": number = number * delta
  32.         CASE "/": IF delta <> 0 THEN number = number / delta ELSE BEEP: PRINT "Division by 0 attempted"
  33.         CASE "^": number = number ^ delta '<<<<<<<< do your own error checks
  34.         CASE "%": number = number MOD delta
  35.         CASE "|": IF (delta <> 0) OR (number <> 0) THEN number = -1 ELSE number = 0
  36.         CASE "&": IF (delta <> 0) AND (number <> 0) THEN number = -1 ELSE number = 0
  37.         CASE "!": IF number <> delta THEN number = -1 ELSE number = 0
  38.         CASE "=": IF number = delta THEN number = -1 ELSE number = 0
  39.     END SELECT
  40.  
  41.  

EDIT: more comments in code

Code: QB64: [Select]
  1. e myRunningTotal, "+101"  


compare to

Code: QB64: [Select]
  1. myRunningTotal = myRunningTotal + 101


Does save typing, specially that long variable name twice!
« Last Edit: April 18, 2019, 10:09:45 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Is there a shortcut way to add something to itself?
« Reply #4 on: April 18, 2019, 11:24:25 pm »
Eh... the first way was better for variables and number literals.

Code: QB64: [Select]
  1. a myRunningTotal, 100
  2. PRINT myRunningTotal
  3. a myRunningTotal, myRunningTotal
  4. PRINT myRunningTotal
  5. a myRunningTotal, 10
  6. PRINT myRunningTotal
  7. a myRunningTotal, myRunningTotal * 8
  8. PRINT myRunningTotal
  9. PRINT myRunningTotal / 9
  10.  
  11. SUB a (n, c)
  12.     n = n + c
  13.  
  14.  

Offline Raptor88

  • Newbie
  • Posts: 22
    • View Profile
Re: Is there a shortcut way to add something to itself?
« Reply #5 on: April 19, 2019, 03:42:36 am »
In QB64, is there a shortcut way to add something to itself?

For example:
MyRunningTotal = MyRunningTotal +1
-or-
MyRunningTotal = MyRunningTotal + 22

A shortcut to negate having to retype MyRunningTotal again on the right side of the equation?

Fellippe, Thanks for confiming that there is no built-in way to do want I asked.
bplus, Thanks for your efforts in creating work-around methods.

Since there is no built-in way in QB64 to do what I want, I created a script for the "AutoHotKey" program to Add, Subtract, Multiply and Divide a value to itself.
Here's my script:

;===== QB64 "Add, Subtract, Multiply, Divide" value to original variable =====
;.... Type variable name, then press (Ctrl +) to fill in the rest of the equation.
#IfWinActive ahk_class FREEGLUT
^=::         ; (Ctrl +) writes "x = x + "                     
  send ^+{Left}
  send ^c
  send {End}
  send {Space}={Space}
  send ^v
  send {space}{+}{space}
return
^-::         ; (Ctrl -) writes "x = x - "
  send ^+{Left}
  send ^c
  send {End}
  send {Space}={Space}
  send ^v
  send {space}{-}{space}
return
^8::         ;(Ctrl *) writes "x = x * "
  send ^+{Left}
  send ^c
  send {End}
  send {Space}={Space}
  send ^v
  send {space}{*}{space}
return
^/::         ;(Ctrl /) writes "x = x / "
  send ^+{Left}
  send ^c
  send {End}
  send {Space}={Space}
  send ^v
  send {space}{/}{space}
return
#IfWinActive

The "#IfWinActive ahk_class FREEGLUT" line allows this script to only run in QB64.
Type "MyRunningTotal" and then press the (Ctrl +) keys to fill in the rest of the equation as:
"MyRunningTotal = MyRunningTotal + "

Hope this helps others,
Raptor88