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.
A FOR NEXT control loop is a type of loop that allows you to repeat a block of code a specific number of times. Here is an example of how to use a FOR NEXT loop in BASIC:
PlayBasic Code:
FOR i =1TO10PRINT i
NEXT i
This FOR NEXT loop will print the numbers from 1 to 10. Let's break down the code:
The FOR statement initializes a loop variable i to the value of 1.
The TO keyword specifies the end value of the loop variable, which is 10 in this case.
The block of code between the FOR and NEXT statements is executed as long as the loop variable i is less than or equal to 10.
The PRINT statement prints the value of the loop variable i.
The NEXT statement increments the loop variable i by 1 and checks if it is still less than or equal to 10. If it is, the loop continues. If not, the loop ends.
Here is the output of this FOR NEXT loop:
1
2
3
4
5
6
7
8
9
10
You can also specify a step value for the loop variable, like this:
PlayBasic Code:
FOR i =1TO10STEP2PRINT i
NEXT i
This FOR NEXT loop will print the numbers from 1 to 10, incrementing the loop variable i by 2 each time. The output of this loop will be:
1
3
5
7
9
You can also specify a step value that is negative, like this:
PlayBasic Code:
FOR i =10TO1STEP-2PRINT i
NEXT i
This FOR NEXT loop will print the numbers from 10 to 1, decrementing the loop variable i by 2 each time. The output of this loop will be:
10
8
6
4
2
Here are some additional details about the FOR NEXT loop in BASIC:
The FOR NEXT loop is a pretest loop, which means that the loop condition (the value of the loop variable) is checked before the loop is executed. If the loop condition is FALSE, the loop is skipped and the program continues with the next statement after the NEXT statement.
The loop variable i is a local variable that is only accessible within the loop. It is created when the loop starts and is destroyed when the loop ends.
The STEP keyword specifies the amount by which the loop variable is incremented or decremented each time the loop iterates. If you omit the STEP keyword, the default step value is 1.
You can nest FOR NEXT loops, which means you can have a FOR NEXT loop inside another FOR NEXT loop. Here is an example of a nested FOR NEXT loop:
PlayBasic Code:
FOR i =1TO3FOR j =1TO3PRINTstr$(i)+" "+str$( j)NEXT j
NEXT i
This nested FOR NEXT loop will print all possible combinations of the numbers from 1 to 3. The output of this loop will be:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
You can use the EXIT FOR statement to exit a FOR NEXT loop prematurely. For example:
PlayBasic Code:
FOR i =1TO10IF i =5THENEXITFORPRINT i
NEXT i
This FOR NEXT loop will print the numbers from 1 to 4, then exit the loop when the loop variable i reaches 5. The output of this loop will be:
1
2
3
4
Can you explain what the DO / LOOP control structure is in PlayBasic ?
Can you explain what the DO / LOOP control structure is in PlayBasic ?
In PlayBasic, the DO/LOOP control structure allows you to repeatedly execute a block of code a specific number of times or until a certain condition is met. There are two forms of the DO/LOOP structure: the DO/LOOP form and the DO/LOOP WHILE form.
The DO/LOOP form has the following syntax:
DO
' code to be executed repeatedly
LOOP
The code block between DO and LOOP will be executed repeatedly until the EXIT DO statement is encountered.
Here is an example of a DO/LOOP loop in PlayBasic:
PlayBasic Code:
x =0DO
x = x +1Print x
If x =10ThenExitDOLOOPPrint"Loop complete"syncwaitkey
In this example, the loop will repeat 10 times, printing the value of x each time. When x reaches 10, the Exit DO statement will be encountered and the loop will exit. The message "Loop complete" will then be printed.
The DO/LOOP WHILE form has the following syntax:
DO
' code to be executed repeatedly LOOP Optional While condition
The code block between DO and LOOP will be executed repeatedly as long as the condition is met. When the condition is no longer met, the loop will exit and control will be passed to the next line of code after the LOOP statement.
Here is an example of a DO/LOOP WHILE loop in PlayBasic:
PlayBasic Code:
x =0DO
x = x +1Print x
LOOP x <10Print"Loop complete"
In this example, the loop will repeat until x is no longer less than 10. The loop will print the value of x each time it is executed, and when x reaches 10 the LOOP WHILE condition will no longer be met and the loop will exit. The message "Loop complete" will then be printed.
So, in general, you might use a DO/LOOP loop when you want to repeat a block of code an unknown number of times, or when you want to repeat a block of code until a certain condition is met. On the other hand, you might use a DO/LOOP WHILE loop when you want to repeat a block of code a specific number of times, or when you want to repeat a block of code while a certain condition is true.
Repeat / Until and While / EndWhile Control Loops In PlayBasic
In PlayBasic, the Repeat/Until control structure allows you to repeatedly execute a block of code until a certain condition is met. The syntax for the Repeat/Until structure is as follows:
Repeat
' code to be executed repeatedly
Until condition
The code block between Repeat and Until will be executed repeatedly until the condition is met. When the condition is met, the loop will exit and control will be passed to the next line of code after the Until statement.
Here is an example of a Repeat/Until loop in PlayBasic:
PlayBasic Code:
x =0Repeat
x = x +1Print x
Until x =10Print"Loop complete"syncwaitkey
In this example, the loop will repeat 10 times, printing the value of x each time. When x reaches 10, the Until condition will be met and the loop will exit. The message "Loop complete" will then be printed.
In PlayBasic, the While/EndWhile control structure allows you to repeatedly execute a block of code as long as a certain condition is met. The syntax for the While/EndWhile structure is as follows:
While condition
' code to be executed repeatedly
EndWhile
The code block between While and EndWhile will be executed repeatedly as long as the condition is met. When the condition is no longer met, the loop will exit and control will be passed to the next line of code after the EndWhile statement.
Here is an example of a While/EndWhile loop in PlayBasic:
PlayBasic Code:
x =0While x <10
x = x +1Print x
EndWhilePrint"Loop complete"syncwaitkey
In this example, the loop will repeat until x is no longer less than 10. The loop will print the value of x each time it is executed, and when x reaches 10 the While condition will no longer be met and the loop will exit. The message "Loop complete" will then be printed.
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
To subtract y from x, you can use the following code:
PlayBasic Code:
z = x - y
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
To divide x by y, you can use the following code:
PlayBasic Code:
z = x / y
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)
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)
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 somethingendif
To see if x is less than or equal to y, you can use the following code:
PlayBasic Code:
if(x <= y)// Do somethingendif
To see if x is equal to y, you can use the following code:
PlayBasic Code:
if(x = y)// Do somethingendif
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 equalsendifif(x != y)// Do something when there not equalsendif
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!"
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:
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.
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:
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;printbin$(num)printbin$(lowerBits)printbin$(middleBits)syncwaitkey
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: From Arrays To Types (Intro To Types)
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.
This video takes the new PlayBasic programmer through a couple of examples that demo how to save a new project and create a new project folder using the PlayBasic IDE (Source Code editor) -
1) In example one we write th classic Hello World example and then show how to save it as a project to the desktop
2) In the second example we take this further by adding some sprite media to the project manually then demo how you might load the sprites and display them.
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
PlayBasic Tutorial: Into to IF / Then and IF / ENDIF statements ( 2017/03/21)
This #tutorial walks the new coder through the IF / THEN and IF / ENDIF statement pairs which are used to selectively execute sections of the code, based upon the result of the comparison operators. IF statements actually allow changes in execution control, so they act upon the comparison, but it's just easier to think of them as part of the comparisons landscape when learning programming.
Setfps20DoClsrgb(20,30,40);
Mx =MouseX()
My =MouseY()Circle Mx,My,40,trueprint Mx
print My
box500,300,600,400,falseif(Mx >500)and(Mx <600)and My >300and My<400print"Mouse is within zone"endifSyncloop