Correct way to Toggle or use Not

Just starting out? Need help? Post your questions and find answers here.
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 617
Joined: Mon May 09, 2011 9:36 am

Correct way to Toggle or use Not

Post by VB6_to_PBx »

i'm trying to create a Toggle or Off/On Switch

whats the best correct way to create a Toggle Switch with or without the Keyword "Not" ??

it seems either Code Line gives the same result :

If Not b = 1 : b = 1 : Else : b = 0 : EndIf
-or-
If b = 1 : b = 0 : Else : b = 1 : EndIf

code i tried + tested :

Code: Select all

Declare Toggle()
Procedure Toggle()
  Static.d b
  If Not b = 1 : b = 1 : Else : b = 0 : EndIf
  ;If b = 1 : b = 0 : Else : b = 1 : EndIf

  Debug "In Procedure: "+Str(b)
EndProcedure 

Toggle()
Toggle()
Toggle()
Debug "---------------"
Toggle()
Toggle()
Toggle()

 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 198
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: Correct way to Toggle or use Not

Post by Otrebor »

i do not know if this is correct, but i use in this way :wink:

Code: Select all

Procedure Toggle()
  Static.d b
  b=1-b
  ;If b = 1 : b = 0 : Else : b = 1 : EndIf

  Debug "In Procedure: "+Str(b)
EndProcedure 

Toggle()
Toggle()
Toggle()
Debug "---------------"
Toggle()
Toggle()
Toggle()
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Correct way to Toggle or use Not

Post by jacdelad »

..it is the correct way. I use it in plenty of my procedures to toggle something.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Jeff8888
User
User
Posts: 38
Joined: Fri Jan 31, 2020 6:48 pm

Re: Correct way to Toggle or use Not

Post by Jeff8888 »

Yes just use ! (XOR) which will toggle a between 1 and 0 as shown.

Code: Select all

a=1
b=1
Debug a
a = a!b
Debug a
a= a!b
Debug a
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Correct way to Toggle or use Not

Post by BarryG »

Here's how I do it:

Code: Select all

Repeat
  toggle=1-toggle
  Debug toggle
  Delay(250)
ForEver
TassyJim
Enthusiast
Enthusiast
Posts: 151
Joined: Sun Jun 16, 2013 6:27 am
Location: Tasmania (Australia)

Re: Correct way to Toggle or use Not

Post by TassyJim »

Code: Select all

state.i = 1

state ! 1
Debug state

state ! 1
Debug state
Saves some typing.

Jim
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Correct way to Toggle or use Not

Post by kenmo »

I prefer :)

Code: Select all

b = Bool(Not b)
To answer your original question, there is basically no difference between the two versions you wrote. They achieve the same thing.
Perhaps one version saves you some microseconds, but I think that's unimportant here. Understandable code is important.

I do think it's strange to use a .d double to store a boolean 0/1 though!
It can only lead to bugs, maybe "b" becomes 1.000000001 due to a calculation, and then it technically fails the "b = 1" check.
I think the PB team would recommend using a .i integer.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Correct way to Toggle or use Not

Post by RASHAD »

You are looking for a flag between 0 or 1
1- Using a Procedure is a waste of time
2- Using a Function is the same as above
2- XOR is your target
Egypt my love
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 617
Joined: Mon May 09, 2011 9:36 am

Re: Correct way to Toggle or use Not

Post by VB6_to_PBx »

thanks everyone for all the great answers + the help !

i first tried this with "Not" , but it caused an Error
so that's why asked what was correct way to use "Not" , also in a Toggle

Code: Select all

Declare Toggle()
Procedure Toggle()
  Static.d b
  b = Not b ;<- causes error
  Debug "In Procedure: "+Str(b)
EndProcedure 

Toggle()
Toggle()
Toggle()
Debug "---------------"
Toggle()
Toggle()
Toggle()

---------------------------------------------------

by kenmo » Tue Dec 07, 2021 12:13 am
this works with "Not"

Code: Select all

Declare Toggle()
Procedure Toggle()
  Static.d b
  b = Bool(Not b)
  Debug "In Procedure: "+Str(b)
EndProcedure 

Toggle()
Toggle()
Toggle()
Debug "---------------"
Toggle()
Toggle()
Toggle()
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Correct way to Toggle or use Not

Post by NicTheQuick »

VB6_to_PBx wrote: Tue Dec 07, 2021 7:48 am thanks everyone for all the great answers + the help !

i first tried this with "Not" , but it caused an Error
so that's why asked what was correct way to use "Not" , also in a Toggle

Code: Select all

Declare Toggle()
Procedure Toggle()
  Static.d b
  b = Not b ;<- causes error
  Debug "In Procedure: "+Str(b)
EndProcedure 

Toggle()
Toggle()
Toggle()
Debug "---------------"
Toggle()
Toggle()
Toggle()
It causes an error because you are using a double. Just use an integer and you are good. Doubles are not for boolean values!
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Correct way to Toggle or use Not

Post by mk-soft »

Never use float or double for toggle because value 1.0 is not 1.0

Code: Select all

Procedure Toggle()
  Static b.i
  b = Bool(Not b)
  ProcedureReturn b
EndProcedure

For i = 1 To 10
  Debug Toggle()
  Delay(100)
Next
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 617
Joined: Mon May 09, 2011 9:36 am

Re: Correct way to Toggle or use Not

Post by VB6_to_PBx »

mk-soft wrote: Tue Dec 07, 2021 9:15 am Never use float or double for toggle because value 1.0 is not 1.0

Code: Select all

Procedure Toggle()
  Static b.i
  b = Bool(Not b)
  ProcedureReturn b
EndProcedure

For i = 1 To 10
  Debug Toggle()
  Delay(100)
Next
thanks mk-soft for your example !

i really needed 2 different Toggles
one with an Integer like yours above ... i'm going to name it just Procedure Toggle() ,,, and use your example
and another one with a Double without Keyword "Not" ... i'm going to name it Procedure ToggleD()
because it compares + toggles between 2 Double values
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
Post Reply