Simple toggle trick

Share your advanced PureBasic knowledge/code with the community.
Randy Walker
Addict
Addict
Posts: 1022
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Simple toggle trick

Post by Randy Walker »

Long time ago I cane across the need to toggle a variable off and on. Until recently I forgot about this trick. This little snippet demonstrates how the tilde (not) symbol can be used to toggle a variable on each pass.
Run with or without Debugger:

Code: Select all

Repeat
  Select MessageRequester("See toggle Work?", Str(t)+" <<Yes to toggle this value.", #MB_YESNO)
    Case #IDYES
      t = ~ t
    Case #IDNO
      Break
  EndSelect
  Debug t
ForEver
End
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: Simple toggle trick

Post by Bisonte »

If this is supposed to be a tip, and it is found by a beginner...
they will be confused at first.

What are these constants?

Please use PureBasic constants and not API constants (which, to my knowledge, only exist in Windows),
even if they are quicker to write... Not everyone use your environment and write in your style.

Thank you.

in this case

Code: Select all

#PB_MessageRequester_YesNo
#PB_MessageRequester_Yes
#PB_MessageRequester_No
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
AZJIO
Addict
Addict
Posts: 2158
Joined: Sun May 14, 2017 1:48 am

Re: Simple toggle trick

Post by AZJIO »

Code: Select all

t = Bool(Not t)
If you read the flag from the ini file and someone writes 15 there, your version will stop working
BarryG
Addict
Addict
Posts: 4161
Joined: Thu Apr 18, 2019 8:17 am

Re: Simple toggle trick

Post by BarryG »

I've always used "t=1-t" because it makes it clear to anyone what it does.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 599
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Simple toggle trick

Post by minimy »

l= 1
l!1
Debug l
l!1
Debug l
This work too
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
Piero
Addict
Addict
Posts: 890
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Simple toggle trick

Post by Piero »

Code: Select all

a=6
a!4
Debug a
a!4
Debug a
a!4
Debug a

Code: Select all

a=~4
Debug 4&a|2
a=~a
Debug 4&a|2
a=~a
Debug 4&a|2
viewtopic.php?p=635000#p635000

:wink:
Last edited by Piero on Thu Aug 21, 2025 10:13 pm, edited 1 time in total.
Randy Walker
Addict
Addict
Posts: 1022
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Simple toggle trick

Post by Randy Walker »

Indeed.... more good tricks. Thanks everyone for your contributions!!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1022
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Simple toggle trick

Post by Randy Walker »

Bisonte wrote: Thu Aug 21, 2025 7:35 am If this is supposed to be a tip, and it is found by a beginner...
they will be confused at first.

What are these constants?

Please use PureBasic constants and not API constants
Thank you.
[/code]
Wow!! Didn't think it would offend anyone. Have it your way...

Code: Select all

Repeat
  Select MessageRequester("See toggle Work?", Str(t)+" <<Yes to toggle this value.", #PB_MessageRequester_YesNo)
    Case #PB_MessageRequester_Yes
      t = ~ t
    Case #PB_MessageRequester_No
      Break
  EndSelect
  Debug t
ForEver
End
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: Simple toggle trick

Post by Bisonte »

Randy Walker wrote: Thu Aug 21, 2025 7:56 pm
Bisonte wrote: Thu Aug 21, 2025 7:35 am If this is supposed to be a tip, and it is found by a beginner...
they will be confused at first.

What are these constants?

Please use PureBasic constants and not API constants
Thank you.
[/code]
Wow!! Didn't think it would offend anyone. Have it your way...
...
Sorry its no offend... But your codes often have some mystical things (for beginners), their are
not in the PB Help ... ;)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Post Reply