Show Menu
Cheatography

PureBasic Cheat Sheet by

This Cheat Sheet only contains the basic PureBasic keywords. It does not contain any library calls. It is meant as a guide to get you writing PB code quickly. Refer to the full documentation for information on how to use the libraries. Get your own free copy at https://purebasic.com/

Flow Control

Break [Level]
Will break out of an If/Then, While/Wend or Repeat­/Until code block. If the optional Level is specified, will break out of that number of nested flow blocks.
Continue
Will skip directly to the start of the code block, skipping any statements between the Continue and the end of the code block
For <va­ria­ble> = <ex­pre­ssi­on1> To <ex­pre­ssi­on2> [Step constant]
Defines a for loop, counting variable from expres­sion1 to expres­sion2, optionally increm­enting by Step. The "­To" value can be changed inside the loop.
Next [variable]
Increments variable and loops through the For loop again. Variable is optional and only included for readab­ility
ForEach List() | Map()
Loops through all of the elements in the specified list or map
Next [List() | Map()]
Increments the ForEach loop. List() or Map() is optional and only included for readab­ility
Gosub label
Send program execution to the code at label, and then the program will resume on the next time
Return
Returns to the code following the Gosub call.
FakeReturn
If Goto is used inside of a subrou­tine, you must use FakeReturn before the Goto
Goto label
Send program execution to the code at label
If <ex­pre­ssi­on>
Starts an If block. The code inside the block will be executed if <ex­pre­ssi­on> evaluates to #True.
ElseIf <ex­pre­ssi­on>
An additional If inside an If block, evaluated if the previous If returned #False.
Else
The code following Else will be executed if all previous If and ElseIf expres­sions evaluated to #False
EndIf
Ends an If block
Select <ex­pre­ssi­on>
Start a Select block and use the value of <ex­pre­ssi­on> for the compar­isons
Case <co­nst­ant­>[, ...]
Compares the value of the Select expression to <co­nst­ant> and executes the following code block if it evaluates to #True.
Default
A code block to run if none of the previous Case statements evaluated to #True
EndSelect
Ends a Select block
Repeat
Begins a Repeat block. Unlike If or While, a Repeat block is always executed at least once.
Until <ex­pre­ssi­on>
Ends a Repeat block. If <ex­pre­ssi­on> evaluates to #False, will run the Repeat block again until <ex­pre­ssi­on> evaluates to #True
ForEver
Ends a Repeat code block and turns it into an infinite loop. Will run forever until the app is killed or a Break statement is encoun­tered
While <ex­pre­ssi­on>
Begins a While code block. Will repeat the block until <ex­pre­ssi­on> evaluates to #False.
Wend
Ends a While code block
Only use a single = in expres­sions, e.g. "If i=0". Do not use Then, which is not a keyword.

You can use ranges in Case statem­ents, e.g. "Case 1 To 8"

Compiler Directives

CompilerIf <Co­nstant Expres­sio­n>
If <Co­nstant Expres­sio­n> evaluates to True, the code block will be compiled, else it will be skipped during compil­ation
Compil­erE­lseIf <Co­nstant Expres­sio­n>
Compil­erElse <Co­nstant Expres­sio­n>
Compil­erEndIf
Ends the CompilerIf code block
Compil­erS­elect <Nu­meric Consta­nt>
Starts the Compil­erS­elect code block.
Compil­erCase <Nu­meric Consta­nt>
If <Nu­meric Consta­nt> is True, the code block will be compiled, else skipped during compil­ation
Compil­erE­ndS­elect
Ends the Compil­erS­elect code block
Compil­erError <String Consta­nt>
Generates a compiler error and displays <String Consta­nt>
Compil­erW­arning <String Consta­nt>
Generates a compiler warning and displays <String COnsta­nt>
Enable­Exp­licit
Enables Explicit mode. When enabled, all the variables which are not explicitly declared with Define , Global , Protected or Static are not accepted and the compiler will raise an error.
Disabl­eEx­plicit
Disables Explicit mode
EnableASM
Enables the inline assembler
DisableASM
Disables the inline assembler

Compiler Functions

SizeOf­(Type)
Returns the size of the variable type or complex structure
Offset­Of(­Str­uct­ure­\Field | Interf­ace­\Fu­nct­ion())
Returns the address offset of a Structure field or the address offset of an Interface function
TypeOf­(Ob­ject)
Returns the type of a variable or structure field.
Subsys­tem­(<C­onstant String Expres­sio­n>)
Determine if a subsystem is in use for the program being compiled
Define­d(Name, Type)
Checks if object Name has been defined or not.
Initia­liz­eSt­ruc­tur­e(*­Poi­nter, Structure)
Initialize the specified structured memory area. It initia­lizes structure members of type Array, List and Map
CopySt­ruc­ture(Source, Destin­ation, Structure)
Copies structured memory from Source to Destin­ation
ClearS­tru­ctu­re(­*Po­inter, Structure)
Clears a structured memory area
ResetS­tru­ctu­re(­*Po­inter, Structure)
Clears a structured memory area and initia­lizes it for use
Bool(<­boolean expression >)
Can evaluate a boolean expression outside of a flow control block. Will return #True or #False

Compiler Object Types

#PB_Byte
#PB_Word
#PB_Long
#PB_String
#PB_St­ructure
#PB_Float
#PB_Ch­aracter
#PB_Double
#PB_Quad
#PB_List
#PB_Array
#PB_In­teger
#PB_Map
#PB_Ascii
#PB_Un­icode
#PB_In­terface
These are the types that are returned from the compiler function TypeOf

Compiler Defined Types

#PB_Co­nstant
#PB_Va­riable
#PB_Array
#PB_List
#PB_Map
#PB_St­ructure
#PB_In­terface
#PB_Pr­ocedure
#PB_Fu­nction
#PB_OS­Fun­ction
#PB_Label
#PB_Pr­ototype
#PB_Module
#PB_En­ume­ration
These are the types that can be specified in the compiler function Defined
 

Numbers

%
At the start of a number, indicates that this is a binary number
$
At the start of a number, indicates that this is a hexade­cimal number

Compiler Reserved Constants

#PB_Co­mpi­ler_OS
Determines on which OS the compiler is currently running. Can be one of the following:
#PB_OS­_Wi­ndows
The compiler is running on Windows
#PB_OS­_Linux
The compiler is running on Linux
#PB_OS­_MacOS
The compiler is running on macOS
#PB_Co­mpi­ler­_Pr­ocessor
Determines the processor type for which the program is created. Can be one of the following:
#PB_Pr­oce­sso­r_x86
Intel x86 (IA -32 or x86 -32)
#PB_Pr­oce­sso­r_x64
Intel x64 (x64, AMD64 or Intel64)
#PB_Pr­oce­sso­r_arm32
Arm 32-bit
#PB_Pr­oce­sso­r_arm64
Arm 64-bit, Apple Silicon
#PB_Co­mpi­ler­_Ba­ckend
Determines which kind of compiler is currently used. Can be one of the following:
#PB_Ba­cke­nd_Asm
The compiler generating Assembler is being used
#PB_Ba­ckend_C
The compiler generating C is being used
#PB_Co­mpi­ler­_Ex­ecu­tab­leF­ormat
Determines executable format. Can be one of the following:
#PB_Co­mpi­ler­_Ex­ecu­table
A regular executable
#PB_Co­mpi­ler­_Co­nsole
A console execut­able. Only matters on Windows
#PB_Co­mpi­ler_DLL
A shared DLL in Windows. A dynlib in macOS, a shared object in Linux
#PB_Co­mpi­ler­_Date
The current date at compile time, in PureBasic date format
#PB_Co­mpi­ler­_File
The full path and name of the file being compiled
#PB_Co­mpi­ler­_Fi­lePath
The full path of the file being compiled
#PB_Co­mpi­ler­_Fi­lename
The filename (without path) being compiled
#PB_Co­mpi­ler­_Line
The current line number being compiled
#PB_Co­mpi­ler­_Pr­ocedure
The current Procedure being compiled, if applicable
#PB_Co­mpi­ler­_Module
The current module being compiled, if applicable
#PB_Co­mpi­ler­_Ve­rsion
The compiler version, in integer format
#PB_Co­mpi­ler­_Home
The full path of the PureBasic directory
#PB_Co­mpi­ler­_De­bugger
Set to 1 if debugger enabled, 0 otherwise
#PB_Co­mpi­ler­_Thread
Set to 1 if the executable was compiled in thread safe mode, 0 otherwise
#PB_Co­mpi­ler­_Un­icode
Set to 1 if the executable was compiled in Unicode, 0 otherwise
#PB_Co­mpi­ler­_Li­neN­umb­ering
Set to 1 if the executable was compiled with OnError line numbering support, 0 otherwise
#PB_Co­mpi­ler­_In­lin­eAs­sembly
Set to 1 if the executable was compiled with inline assembly, 0 otherwise
#PB_Co­mpi­ler­_En­abl­eEx­plicit
Set to 1 if the executable was compiled with Enable­Exp­licit, 0 otherwise
#PB_Co­mpi­ler­_Is­Mai­nFile
Set to 1 if the file being compiled is the main file, 0 otherwise
#PB_Co­mpi­ler­_Is­Inc­lud­eFile
Set to 1 if the file being compiled has been included by another file, 0 otherwise
#PB_Co­mpi­ler­_32Bit
Set to 1 if the compiler generates 32-bit code, 0 otherwise
#PB_Co­mpi­ler­_64Bit
Set to 1 if the compiler generates 64-bit code, 0 otherwise
#PB_Co­mpi­ler­_Op­timizer
Set to 1 if the compiler generates optimizer code, 0 otherwise
All constants start with #.

These constants are useful for the Compiler Directives (Compi­lerIf, Compil­erS­elect).

Data Blocks

DataSe­ction
Starts a Data section, a predefined block of inform­ation
EndDat­aSe­ction
Ends the Data section
Data.T­ypeName
Defines data
Restore label
Will set the start of Read to label
Read[.T­ype] <va­ria­ble>
Read the next available data

Debugging Functions

CallDe­bugger
Invokes the debugger and freezes the program
Debug <ex­pre­ssi­on>[, DebugL­evel]
Display the Debug output window and show the result of Expression in it. The ’Debug­Level’ is the priority level of the debug message.
DebugLevel <co­nstant expres­sio­n>
Set the current Debug level
Disabl­eDe­bugger
Disable debugger checks in the subsequent code
Enable­Deb­ugger
Enable debugger checks in the subsequent code

Variables

Define.<t­ype> [<v­ari­abl­e> [= <ex­pre­ssi­on>]][, ...]
Defines one or more variables of Type type and optionally initia­lizes them
Define <va­riable >.<type > [= <ex­pre­ssion >]][, ...]
Alternate form of Define
Dim name.<­typ­e>(­<ex­pre­ssi­on>, ...)
Creates new arrays
ReDim name.<­typ­e>(­<ex­pre­ssi­on>, ...)
Resize an existing array. Will only affect the last dimension of a multid­ime­nsional array
Enumer­ation [name] [<c­onstant > [Step <co­nstant >]]
Creates a new enumer­ation named name
Enumer­ati­onB­inary [name] [<c­onstant >]
Creates a new binary enumer­ation named name. Binary enumer­ations can be used for flags.
EndEnu­mer­ation
Ends the Enumer­ation definition
Global­[.<­typ­e>] <va­ria­ble­[.<­typ­e>]> [= <ex­pre­ssi­on>][, ...]
Defines the scope of the variables to be global rather than local
NewList name.<type >()
Creates a new, dynamic List of Type <ty­pe>.
NewMap name.<­typ­e>(­[Sl­ots])
Create a new Map (Hash, Dictio­nary). The keys are always of type String, the values will be of Type <ty­pe>. If Slots is not specified, will be dynami­cally allocated as needed.
Protec­ted­[.<­typ­e>] <va­ria­ble­[.<­typ­e>]> [= <ex­pre­ssi­on>][, ...]
Allows a variable to be accessed only in a Procedure even if the same variable has been declared as Global in the main program
Shared <va­ria­ble­>[, ...]
Allows a variable, an array, a list or a map to be accessed within a procedure
Static­[.<­typ­e>] <va­ria­ble­[.<­typ­e>]> [= <co­nstant expres­sio­n>][, ...]
Allows creating a local persistent variable in a Procedure even if the same variable has been declared as Global in the main program
Structure <na­me>
Begins a Structure definition
Structure <na­me> Extends <st­ruc­t_n­ame>
Begins a Structure definition that add to an existing Structure
Structure <na­me> Align <n>
Begins a Structure definition where every element is aligned to an <n>­-byte boundary. Align can be used with Extends also.
EndStr­ucture
Ends a Structure definition block
Struct­ure­Union
Begins a Struct­ure­Union definition block
EndStr­uct­ure­Union
Ends a Struct­ure­Union definition block
Thread­ed[.<t­ype­>] <va­ria­ble­[.<­typ­e>]> [= <co­nstant expres­sio­n>][, ...]
Allows creating a thread persistent variable
Starting a variable with # makes it a constant, e.g. #Pi=3.14

Keywords

Import "­Fil­ena­me"
Allows declaring external functions and variables from a library or object
Variab­leN­ame.<t­ype>
Declares an external variable
Functi­onN­ame.<t­ype­>(<­par­ame­ter­>[ = Defaul­tVa­lue][, ...]
Declares an external function
EndImport
Ends the Import declar­ations
Includ­eFile "­Fil­ena­me"
Includes the specified source code file at the current position
XInclu­deFile "­Fil­ena­me"
The same as Includ­eFile, but with protec­tions to avoid including the same file twice.
Includ­eBinary "­fil­ena­me"
Will include the file at the current position. Should be done in a Data block
Includ­ePath "­pat­h"
Will set the default path for all subsequent Include files
Macro <na­me> [(Para­meter [, ...])]
Starts a Macro block. A placeh­older for code that can be directly inserted into the source code wherever the Macro is called
EndMacro
Ends the Macro block
Undefi­neMacro <name >
Undefines Macro <na­me>
MacroE­xpa­nde­dCount
The number of times the Macro has been called­/ex­panded
Declar­eModule <name >
Defines the public interface to Module <na­me>
EndDec­lar­eModule
Ends the public module declar­ation
Module <na­me>
Starts the implem­ent­ation of the Module
EndModule
Ends the Module code block
USeModule <na­me>
Can use any module that had a previous public declar­ation
UnUseM­odule <na­me>
Removes the previously used module
End [ExitCode]
Ends the program correctly. If ExitCode is specified, the program will return it as the exit code to the OS.
Swap <ex­pre­ssi­on1­>, <ex­pre­ssi­on2>
Does an optimized swap of the expres­sions
           
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Metasploit 4.5.0-dev.15713 Cheat Sheet
          LSI Logic MegaRAID Cheat Sheet
          Windows Terminal Cheat Sheet

          More Cheat Sheets by DNSGeek

          Vision BASIC Cheat Sheet