PlayBasic TUTORIALS


     New to programming and need some help getting started ? - Well, then our tutorial section will help kick start your programming adventure. If you're looking for more Source Code / Tutorials & Media ? - Then remember to visit the PlayBasic Resource board on our forums.

Found #7 items in Variables category


 Common Variables Questions In PlayBasic

By: Kevin Picone Added: January 7th, 2023

Category: All,Variables,Integers,Floats,Strings,Learn To Code







Learn To Code: Introduction To Variables (Integers / Floats & Strings) in PlayBasic




Q) Whats the numeric range of a 32bit integer



 A)  The numeric range of a 32-bit integer is -2147483648 to 2147483647.

     In a 32-bit signed integer, one bit is used to represent the sign of the number (positive or negative), and the remaining 31 bits are used to represent the magnitude of the number. This means that a 32-bit integer can represent any integer value in the range -2147483648 to 2147483647, inclusive.

    Here's a breakdown of the range of values that can be represented by a 32-bit integer:

    -   The largest positive number that can be represented is 2147483647. This is the value that is obtained when all 31 magnitude bits are set to 1 and the sign bit is set to 0.

    -   The smallest negative number that can be represented is -2147483648. This is the value that is obtained when all 31 magnitude bits are set to 1 and the sign bit is set to 1.

    -   The range of values between -2147483648 and 2147483647 includes all possible integer values that can be represented using a 32-bit signed integer.



Q) How do add or subtract 32bit integers together ?

     To add or subtract 32-bit integers, you can use the standard arithmetic operators + and - in your code.

     For example, to add two 32-bit integers x and y, you can use the following code:

PlayBasic Code:
 z = x + y

COMMANDS USED:


    To subtract y from x, you can use the following code:

PlayBasic Code:
 z = x - y

COMMANDS USED:


    Both of these operations will work regardless of the size of the integers, as long as the result fits within the range of values that can be represented by a 32-bit integer.

   In most programming languages, 32-bit integers have a range of -2147483648 to 2147483647. This means that the result of an addition or subtraction operation must be within this range in order to be stored in a 32-bit integer.

    If the result of the operation is outside of this range, the result may be truncated or wrapped around to fit within the range. This can cause unexpected results, so it is important to check the range of the result before storing it in a 32-bit integer.

    In summary, you can use the + and - operators to add and subtract 32-bit integers, and you should check the range of the result to ensure that it fits within the range of values that can be represented by a 32-bit integer.



Q) How can I multiply or divide 32 bit integer variables


    To multiply or divide 32-bit integer variables, you can use the standard arithmetic operators * and / in your code.

    For example, to multiply two 32-bit integers x and y, you can use the following code:

PlayBasic Code:
 z = x * y

COMMANDS USED:


    To divide x by y, you can use the following code:

PlayBasic Code:
 z = x / y

COMMANDS USED:


     Both of these operations will work regardless of the size of the integers, as long as the result fits within the range of values that can be represented by a 32-bit integer.

    In most programming languages, 32-bit integers have a range of -2147483648 to 2147483647. This means that the result of a multiplication or division operation must be within this range in order to be stored in a 32-bit integer.

    If the result of the operation is outside of this range, the result may be truncated or wrapped around to fit within the range. This can cause unexpected results, so it is important to check the range of the result before storing it in a 32-bit integer.

    In summary, you can use the * and / operators to multiply and divide 32-bit integers, and you should check the range of the result to ensure that it fits within the range of values that can be represented by a 32-bit integer.




 Q)  What's the numeric range of a 32bit float ?

  The numeric range of a 32-bit float, also known as a single-precision floating-point number, is approximately 3.4 x 10^-38 to 3.4 x 10^38.

   In a single-precision floating-point number, the range of values that can be represented is determined by the number of bits used for the significant (mantissa) and the exponent. In a 32-bit float, 23 bits are used for the significant and 8 bits are used for the exponent.

  The smallest positive number that can be represented in a 32-bit float is approximately 1.4 x 10^-45, and the largest positive number is approximately 3.4 x 10^38. The range of values between these two numbers includes all possible positive single-precision floating-point numbers.

   Negative numbers are also possible, and the range of negative values is the same as the range of positive values, but with the opposite sign. This means that the smallest negative number that can be represented in a 32-bit float is approximately -3.4 x 10^38, and the largest negative number is approximately -1.4 x 10^-45.

   The range of values that can be represented using a 32-bit float is not an exact range, but rather an approximate range, because the values are represented using a finite number of bits. The precision of the values within this range may also vary depending on the exponent.




 Q)  When would I choose to use 32bit integer instead of a 32bit float ?


You would choose to use a 32-bit integer instead of a 32-bit float (single-precision floating-point number) when you need to represent integer values and do not need a fractional component.

Integer values are whole numbers that do not have a fractional component, such as 1, 2, 3, 4, etc. Integer values can be positive, negative, or zero.

On the other hand, floating-point numbers are numbers that have a fractional component, such as 1.5, 2.6, 3.14, etc. They can also be positive, negative, or zero.

There are a few reasons why you might choose to use a 32-bit integer instead of a 32-bit float:

   Integer values take up less space in memory than floating-point numbers. A 32-bit integer uses 4 bytes of memory, while a 32-bit float uses 4 bytes of memory.

   Integer values are faster to process than floating-point numbers. This is because most computers have special hardware that is optimized for processing integer values, and integer operations are generally faster than floating-point operations.

   If you need to represent a value that does not have a fractional component, using a floating-point number would be unnecessary and could introduce additional complexity or error.

In general, you should use a 32-bit integer when you need to represent integer values and do not need a fractional component, and use a 32-bit float when you need to represent decimal values and need a fractional component.



  Q). Can i perform addition subtracts multiplication or division between 32bit integers and 32bit floats ?


     Yes, you can perform arithmetic operations between 32-bit integers and 32-bit floats (single-precision floating-point numbers) in most programming languages.

   When you perform arithmetic operations between integers and floats, the result will be a float. For example:

PlayBasic Code:
    x = 5  // 32-bit integer
    y# = 2.5  // 32-bit float

    z# = x + y#  // z is a float (7.5)
    w# = x * y#  // w is a float (12.5)

COMMANDS USED:


   You can also mix and match arithmetic operations between integers and floats. For example:

PlayBasic Code:
    x = 5  // 32-bit integer
    y# = 2.5  // 32-bit float

     z# = x + y# * 2  // z is a float (10)
     w# = (x + y#) / 2  // w is a float (3.75)

COMMANDS USED:


   When you perform arithmetic operations between integers and floats, it is important to be aware of the potential for loss of precision, particularly when you are working with large or small numbers. Floating-point numbers have a fixed number of bits to represent the significant (mantissa) and the exponent, which means that they can only represent a finite set of values. This can cause some values to be rounded or approximated when they are represented as floats.

   In summary, you can perform arithmetic operations between 32-bit integers



Q)How DO I compare numeric variables together ?


     To compare numeric variables, you can use the comparison operators > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to), = (equal to), and <>  != (not equal to).

     For example, to compare two variables x and y to see if x is greater than y, you can use the following code:

PlayBasic Code:
if (x > y) 
  // Do something
endif

COMMANDS USED:


To see if x is less than or equal to y, you can use the following code:

PlayBasic Code:
if (x <= y) 
  // Do something
endif

COMMANDS USED:


    To see if x is equal to y, you can use the following code:

PlayBasic Code:
if (x = y)
  // Do something
endif

COMMANDS USED:


     To see if x is not equal to y, you can use the following code:

PlayBasic Code:
if (x <> y) 
  // Do something when there not equals
endif


if (x != y) 
  // Do something when there not equals
endif


COMMANDS USED:


     These operators work with any numeric data type, including integers, floats, and doubles.

     It's important to note that the comparison between different data types can produce rounding or truncating errors.   So it's best to convert your variable to the same datatype when making comparisons between them.




  Q) What's can a String variable hold ?


    A string variable is a variable that holds a sequence of characters.

    In most programming languages, a string is a data type that represents a sequence of characters, such as a word, a sentence, or a series of words and sentences. Strings are often used to store and manipulate text data, such as in a database or a document.

   Strings can be stored in variables just like other data types, such as numbers or booleans. For example, in the following code snippet, the variable message$ is a string that holds the value "Hello, World!"

PlayBasic Code:
    message$ = "Hello, World!"

COMMANDS USED:


   You can manipulate strings by concatenating them, extracting substrings, and performing other operations on them. For example, you can concatenate two strings using the + operator, like this:

PlayBasic Code:
  greeting$ = "Hello, "
  name$ = "John"
  fullGreeting$ = greeting$ + name$  // "Hello, John"

COMMANDS USED:



   You can also use string functions and methods to manipulate strings. For example, you can use the length property to get the length of a string, or the mid$() function to extract a substring from a larger string.

PlayBasic Code:
  message$ = "Hello, World!"
  messageLength = len(message$)  // 13
  firstWord$ = mid$(message,1, 5)  // "Hello"

COMMANDS USED: LEN | MID$ |


  In summary, a string variable is a variable that holds a sequence of characters, and you can manipulate strings by concatenating them, extracting substrings, and using various string functions and methods.    



 Q) When would I choose to use a String variable instead of a 32bit Integer or 32bit float ?

 A) You would choose to use a string variable instead of a 32-bit integer or 32-bit float when you need to represent and manipulate text data, rather than numeric data.

  Integer variables are used to represent whole numbers that do not have a fractional component, such as 1, 2, 3, 4, etc. Integer variables are useful for representing and manipulating numerical data, such as counts, quantities, and other values that do not have a decimal component.

  Floating-point variables, also known as single-precision floating-point variables, are used to represent decimal values that have a fractional component, such as 1.5, 2.6, 3.14, etc. Floating-point variables are useful for representing and manipulating decimal values, such as measurements, coordinates, and other values that require a fractional component.

  On the other hand, string variables are used to represent and manipulate text data, such as words, sentences, and other sequences of characters. String variables are useful for representing and manipulating text data, such as names, addresses, and other values that are expressed as text.

  In general, you should use a string variable when you need to represent and manipulate text data, and use an integer or floating-point variable when you need to represent and manipulate numeric data.

   For example, you might use a string variable to store a person's name, and use an integer variable to store their age. Or, you might use a string variable to store a product's description, and use a floating-point variable to store its price.



Q) How do I append strings together ?


       To append two strings together, you can use the concatenation operator + or the concatenation assignment operator +=.

      For example, to append two strings s1 and s2 together and store the result in a new string s3, you can use the following code:

            s3$ = s1$ + s2$


      To append s2 to the end of s1, you can use the following code:

            s1$ += s2$

      Both of these operations will work regardless of the size of the strings or the data they contain.

      In most programming languages, strings are immutable, which means that they cannot be modified once they have been created. When you perform a concatenation operation, the original strings are not modified; instead, a new string is created that contains the concatenated result.

      You can also use string interpolation to append strings together. String interpolation allows you to embed expressions inside a string literal, and the expression is evaluated and the result is included in the string.

       For example, you can use string interpolation to append a string and a variable together like this:

             name$ = "John";
             greeting$ = "Hello, " +name$ +"!"  // "Hello, John!"

        In summary, you can use the + and += operators to append two strings together, and you can use string interpolation to embed expressions in a string and append them together.





Q) What characters can a string variable hold ?


   A string variable can hold any sequence of characters that is allowed by the encoding used to represent the string.

   In most programming languages (such as PlayBasic), strings are stored as sequences of bytes, and each character in the string is represented by one or more bytes, depending on the character encoding that is used.    

   The most common character encoding for strings is ASCII (American Standard Code for Information Interchange), which uses a  single byte to represent each character. ASCII includes 128 characters, including the upper and lowercase letters of the English alphabet, digits 0 through 9, punctuation marks, and a few special characters such as the dollar sign ($) and the at symbol (@).

    Other character encodings, such as Unicode, can represent a much larger set of characters, including characters from many different scripts and languages. Unicode uses a variable-length encoding scheme, which means that each character can be represented by one or more bytes, depending on the character. Unicode includes over 100,000 characters, covering virtually every written language in the world.

   In summary, a string variable can hold any sequence of characters that is allowed by the character encoding that is used to represent the string. ASCII and Unicode are the most common character encodings for strings, and both of these encodings support a wide range of characters.



Q.) What's the difference between a bit and a byte  ?

    A bit is the smallest unit of data in a computer, and a byte is a group of bits that is used to represent a character or a small integer.

    A bit is a binary digit, which can have one of two values: 0 or 1. Bits are used to represent the values of variables and to store data in a computer's memory.

   A byte is a group of 8 bits that is used to represent a character or a small integer. A byte can represent any of 256 different values, because 2 to the power of 8 (2^8) is equal to 256.

   Here are some examples of the relationship between bits and bytes:

       A single bit can represent a boolean value (true or false).

       A group of 4 bits is known as a nibble, and can represent a small integer value between 0 and 15.

       A byte can represent a single character in the ASCII character set, which includes upper and lowercase letters, digits, punctuation marks, and a few special characters.

       A byte can also represent a small integer value, such as a number between 0 and 255.

   In summary, a bit is the smallest unit of data in a computer, and a byte is a group of 8 bits that is used to represent a character or a small integer. Bytes are used to store data in a computer's memory, and are the basic unit of storage for most computer systems.



Q) How would I extract bits from an 32bit integer into separate variables


    To extract bits from a 32-bit integer and store them in separate variables, you can use bit shifting and masking operations.

    Bit shifting is the process of moving the bits in a binary number to the left or right by a certain number of positions. Bit masking is the process of isolating specific bits from a binary number by applying a mask to the number.

   Here's an example of how you might extract the bits from a 32-bit integer and store them in separate variables using bit shifting and masking:

PlayBasic Code:
    num = 123456;  // 32-bit integer

	// Extract the upper 16 bits and store them in a 16-bit integer
    upperBits = num >> 16;

	// Extract the lower 16 bits and store them in a 16-bit integer
	lowerBits = num & 0xffff;

	// Extract the 8 bits at positions 8 through 15 and store them in an 8-bit integer
	 middleBits = (num >> 8) & 0xff;


	print bin$(num)
	print bin$(lowerBits)
	print bin$(middleBits)
	
	sync
	waitkey


COMMANDS USED: PRINT | BIN$ | SYNC | WAITKEY |




   In this example, the >> operator is used to shift the bits of the integer to the right by the specified number of positions. The & operator is used to apply a mask to the integer, which allows you to isolate specific bits.

   You can use these techniques to extract any subset of bits from a 32-bit integer and store them in separate variables. You can also use bit shifting and masking to extract bits from integers of other sizes, such as 8-bit, 16-bit, and 64-bit integers.

   In summary, you can use bit shifting and masking operations to extract specific bits from a 32-bit integer and store them in separate variables. These operations allow you to manipulate the bits of an integer and extract the specific data that you need.



 




 PlayBasic Tutorial: Intro To Functions

By: Kevin Picone Added: July 7th, 2018

Category: All,Video,Beginner,Functions,Variables,Scope,Getting Started, Intermediate

     Hi welcome to our Intro to PlayBasic Functions tutorial. In this tutorial we start out with some revision of Gosub / Return statements, which are used to create a simple sub routine. Sub routines are changes in program flow, allowing the programmer to execute a chunk of code that's external to the section they may be writing, then upon completion the control returns to the caller. This is the same basic model that functions introduction to our programming, except functions are more formalized. Meaning they have some strict rules about syntax and intro a new concepts such as scope changes, which sub routines don't have.

The types of functions shown in video are very simple, initially we start by taking a sub routine that prints rows of text and convert that to function. Through this process we encounter our first problem which is variable scope and look at ways to solve it, such as making our variable global, or better yet passing a variable into the function as a parameter . Later in the video, we create our own custom distance function, as well building a function that does some simple string manipulation

NOTE: This video was recorded alive with only a few changes for length..






Get Source Code Example For This Tutorial




 PlayBasic Tutorial: From Arrays To Types (Intro To Types)

By: Kevin Picone Added: June 14th, 2018

Category: All,Video,Beginner,Types,List,Arrays,Variables,Getting Started,Learn To Code

    This tutorial picks up where the previous variables to arrays tutorial left off, in that it takes the array code, demos that code then we set about converting the parallel array approach shown in the previous tutorial and we build a structure (TYPE) to hold each characters properties. Once the type has been defined that includes all the required properties, we then define a typed array that will house the collection of characters. Later in the video take a look at using typed lists also. So if your struggling with types this could be a good place to start.



Links:

    * PlayBasic Tutorial: From Arrays To Types (Intro To Types) Source Code





 PlayBasic Tutorial: From Variables To Arrays

By: Kevin Picone Added: September 20th, 2017

Category: All,Video,Beginner,Arrays,Variables,Getting Started,Learn To Code

    Welcome PlayBasic programmers, in this tutorial we're going to introduce the concept of arrays starting out from variables. So first we build a simple game loop that controls two characters using only variables. The characters are represented on screen as filled circles.

     After we get up to speed with the variable version we then move onto how we can use parallel integer arrays to store the various properties of the characters. The array version can control as many or as few characters as you like, which is the benefit of Arrays over Variables



Example #1 Variables

PlayBasic Code:
  Xpos1  = 400
   Ypos1  = 300
      
   Xpos2  = 100
   Ypos2  = 100

      
   setfps 20   
      
  do    

    cls rgb(10,20,40)

   ; Draw circle 1        
     Circle Xpos1,Ypos1, 32, true
   
     Xpos1 = Xpos1 + 2
     Ypos1 = Ypos1 - 4
  
      Xpos1 = WrapValue(Xpos1,0,800)     
       Ypos1 = WrapValue(Ypos1,0,600)     
   
   

   ; Draw circle 2      
     Circle Xpos2,Ypos2, 64, true
   
     Xpos2 = Xpos2 + 2
     Ypos2 = Ypos2 + 2
       Xpos2 = WrapValue(Xpos2,0,800)     
       Ypos2 = WrapValue(Ypos2,0,600)     
  
     sync
  loop
  
 
COMMANDS USED: SETFPS | CLS | RGB | CIRCLE | WRAPVALUE | SYNC |




Example #2 Array Version

PlayBasic Code:
 Setfps 20

   Number_Of_Characters = 50 

   dim Xpos( Number_Of_Characters  )
   dim Ypos( Number_Of_Characters  )
   dim size( Number_Of_Characters  )
   dim Xspeed( Number_Of_Characters )
   dim Yspeed( Number_Of_Characters )
   dim Colour( Number_Of_Characters )
   
   
   for lp = 1 to Number_Of_Characters
         Xpos(lp)  = rnd(800)
         Ypos(lp)  = rnd(600)
         Size(lp)  = rndrange(16,50)
         Xspeed(lp) = rndrange(-5, 5)
       Yspeed(lp) = rndrange(-5,5)
       Colour(lp) = rndrgb()
   next
   
   
  do 
     
        Cls rgb(0,400,20)
        
        for lp=1 to Number_Of_Characters
           
           Radius = Size(lp)

           circlec xpos(lp),ypos(lp),Radius,true,Colour(lp)
           
         //  xpos(lp) = Xpos(lp) + Xspeed(lp)
         //  ypos(lp) = ypos(lp) + Yspeed(lp)
           
           xpos(lp) = wrapvalue(xpos(lp) + Xspeed(lp) , -Radius, 800 + Radius)
           ypos(lp) = wrapvalue(ypos(lp) + Yspeed(lp) , -Radius, 600 + Radius)
                      
        next
          
        Sync
     loop


COMMANDS USED: SETFPS | DIM | RND | RNDRANGE | RNDRGB | CLS | RGB | CIRCLEC | WRAPVALUE | SYNC |






 PlayBasic Tutorial: Intro To FOR NEXT LOOPS

By: Kevin Picone Added: March 20th, 2017

Category: All,Video,Beginner,Getting Started,Variables,Loops

    This tutorial takes the new basic coder through the bare bones of the FOR / NEXT loop. The tutorial was recorded live and is uploaded virtually as is, warts and all. In this tutorial we use loops to draw simple graphical objects such as boxes moving through to making a grid of scrolling boxes.

     Commands used in this tutorial. For / Next / Step / Exit / Continue / Print / Line / Box / Sync / Ink / RGB() / RND() / RndRGB() and possibly a few others.








 PlayBasic Tutorial: Intro To Integer Variables & Character Movement

By: Kevin Picone Added: February 24th, 2017

Category: All,Video,Beginner,Getting Started,Variables

PlayBasic Tutorial: Intro To Integer Variables & Character Movement

This tutorials starts out with a brief look Intro To Integer Variables, but soon moves on looking at some really basic user controlled/player movement code.

The commands you'll see in this tutorial are Print, Sync, WaitKey, Do / Loop, SetFps, Circle and If/EndIF









 Game Programming For Beginners

By: TDK_Man , Kevin Picone Added: April 20th, 2008

Category: All, Beginner,Getting Started,Variables,Arrays,Functions, Learn To Code

Learn basic game programming in this 10 part tutorial series for beginners. This series covers everything from Variables, Arrays, Program Structure, Loops, Functions through to various articles on game timing and logic.

Highly Recommended





Viewing Page [1] of [1]





Looking for More Tutorials?:



 

 
     
 
       

(c) Copyright 2002 / 2024 Kevin Picone , UnderwareDesign.com  - Privacy Policy   Site: V0.99a [Alpha]