Your First Program
This tutorial introduces complete beginners to programming with RCBasic. No prior programming knowledge is assumed.
Hello World
Type the following into the RCBasic Studio editor and press the Run button (the triangle, or Build → Run):
Print "Hello World"
You should see Hello World appear in the console window.
Congratulations — you just wrote your first program!
Data Types
RCBasic has two data types: Numbers and Strings.
A string is a sequence of characters enclosed in double quotes.
A number is a numeric value (integer or floating-point).
' A string and a number that look the same but behave differently:
"42" ' this is a string — two characters: '4' and '2'
42 ' this is a number — the value forty-two
String concatenation vs arithmetic addition:
Print "42" + "53" ' outputs: 4253 (string concat)
Print 42 + 53 ' outputs: 95 (numeric add)
Arithmetic Operators
Operator |
Name |
Example |
|---|---|---|
| Addition |
|
| Subtraction |
|
| Multiplication |
|
| Division |
|
| Power |
|
RCBasic respects standard order of operations (PEMDAS):
Print 4 + 3 * 2 ' outputs: 10 (multiply first)
Print (4 + 3) * 2 ' outputs: 14 (parentheses first)
The Print Statement
Print outputs values to the console. Multiple items of different types
can be printed on one line by separating them with ;:
Print "Text"; 5 + 4; " More Text";
' Ending Print with ";" keeps the cursor on the same line.
Variables
A variable stores a value so you can reuse it later. Number variables
use plain names; string variables end with $:
a = 2
b$ = "Bob"
batman = 4.5
superman$ = "some text"
Print a + batman ' outputs: 6.5
Print b$ + superman$ ' concatenates the two strings
You can change a variable's value at any time:
a = 5 : Print a ' 5
a = 6 : Print a ' 6
a = 5 + 6 : Print a ' 11
Getting Input from the User
Input$(prompt$) displays a prompt and returns whatever the user types
as a string:
name$ = Input$("What is your name? ")
Print "Hello " + name$ + ". Welcome to RC Basic."
To use the input as a number, convert it with Val():
d = Val( Input$("Enter a number: ") )
Flow Control — If / ElseIf / Else
Use If ... Then ... End If to execute code conditionally:
If name$ = "Bob" Then
Print "We have been expecting you"
End If
Multi-branch example:
a = 1 : b = 3 : c = 5
d = Val( Input$("Enter a number: ") )
If d = a Then
Print "You typed the A value"
ElseIf d = b Then
Print "That is B"
ElseIf d = c Then
Print "That would be C"
Else
Print "You did not type any of the numbers"
End If
Comparison Operators
Given variables a and b:
Operator |
Meaning |
|---|---|
| Equal to |
| Less than |
| Greater than |
| Less than or equal to |
| Greater than or equal to |
| Not equal to |
Logical Operators
Keyword |
Behaviour |
|---|---|
| Both conditions must be true. |
| At least one condition must be true. |
| Exactly one condition must be true (the other must be false). |
| Reverses the boolean result of the expression. |
If 1 = 1 AND 4 > 2 Then
Print "This is true"
End If
If NOT (1 = 3) Then
Print "This is true" ' 1≠3 is false; NOT makes it true
End If
Loops
See Loops for full documentation. Quick overview:
' FOR loop — prints 1 to 10
For i = 1 To 10
Print i
Next
' WHILE loop — prints 0 to 4
i = 0
While i < 5
Print i
i = i + 1
Wend
Built-in Functions
RCBasic provides over 200 built-in functions. A few common ones:
Print Abs(-4) ' Absolute value → 4
Print Date$() ' Current date as string
Print UCase$("hello") ' Uppercase → "HELLO"
x = Val("12") ' String to number → 12Refer to the Math, Strings, and other API sections for the complete function reference.
Challenges
Write a program that asks the user for a number, then prints every number from 1 up to that number.
Write a program that keeps asking the user for their name until they type
"Bob", then prints"Goodbye Bob".