16-21 Minute Read
A Complete Introduction to Blue
Save Time and Money when you automate repetitive tasks and integrate Blue into other projects.
A “Hello, world!” program in Blue
Prerequisites:
– The Blue compiler
– A C++ Compiler (Such as GCC)
– An empty folder on your OS’s Filesystem
Traditionally, when you run a program for the first time, you would print “Hello, world!” to the console, and that’s exactly what we are going to do here!
First thing’s first, we need to define an entry point for our program, where it will start executing the code we’ve typed. In Blue, the way we do this is by defining it like so:
main()
This is quite similar to a method call, which you will learn about later, but it’s a special type of a method definition, which differentiates it from generic methods.
Now we actually have to make the program write: “Hello, world!” to the standard output, we can do this by using the built in “print” macro*, which will be explained in a minute.
* A macro is a type safe, built-in, callable method evaluated at compile time
print!(“Hello, world!”)
This instructs the program to print a string (A collection of alphabetic and/or numeric characters), to the standard output, which will be displayed by the console. In our case, it’s: “Hello, world!”, but it doesn’t have to be, you can print any string you can imagine to the console, even something like: “Hello”, all you have to do is change the argument we’ve provided, all you’d have to is remove the “, world!” from, “Hello, world!”. And you could only print “Hello”.
Now, we need to end the program’s entry point, which can be done by creating a new line, and simply typing:
end
If you followed all the way through correctly, you should have written:
main()
print!(“Hello, world!”)
end
Now, open up a command line terminal, and type: “blue ‘Source Folder’ cpp”, and execute the command, this will compile the blue code you have written to lower level, faster C++ code, and will then compile the program into an executable file, ready to run. To see your program in action, go to the “export/bin” folder that the Blue compiler should have generated, and open up a new terminal, and type “main” and execute the command, if it prints out “Hello, world!”, congratulations, you have just created your first ever Blue program!
Variables
Prerequisites:
– An active Blue project on your computer
– An open .BL file in a text editor of your choice
– The Blue compiler
For many decades now, variables have been a standard part of any programming language. Variables are markers that can store data under a user specified name, and in pretty much all systems languages, must have a type that corresponds to the value given to the variable.
There are seven basic types in Blue:
– String: A collection of alphabetic and/or numeric characters surrounded in quotes
– Char: A single alphabetic or numeric character surrounded in quotes
– Float: A non whole number that can hold up to 4 bytes (7 digit precision)
– Double: A non whole number that can hold up to 8 bytes (15 digit precision)
– Int: A whole number/integer
– Bool: A value that can be either true or false, think of it as an on or off switch
– Array: A collection of values that match any type available in the language
Some languages will require you to to define the type of the variable along with the value, however Blue will automatically infer the type based on the value, so there is no need to define the type of variable. However, you still need to define the name and value of the variable.
To define a string variable, type the following:
yourNameHere = “this is a string”
To define a char variable, type the following:
yourNameHere = “c”
To define a float variable, type the following:
yourNameHere = 1.1
To define a double variable, type the following:
yourNameHere = 1.10000000
To define an int variable, type the following:
yourNameHere = 1
To define a bool variable, type the following:
yourNameHere = true
To define an array variable, type the following:
yourNameHere = [“Hello, “, “World!”]
As you can see, variables are defined by typing: {name} = {value}
To define a global variable, simply define it in the global scope (Outside of a method).
To define a local variable, simply define it inside a method.
Keep in mind that variables cannot be used as macro parameters.
Methods
Prerequisites:
– An active Blue project on your computer
– An open .BL file in a text editor of your choice
– The Blue compiler
Sometimes referred to as “Functions”, methods are named blocks of code that can be reused simply by typing the name of the method next to two brackets. These two brackets can be empty, or contain arguments.
Arguments are values that are assigned to the parameters defined along with the method. Parameters are just placeholders for values; If we gave a method a single parameter “isTrue” and call the method like so:
theMethod(true)
“isTrue” would be equal to true.
We can define a method like so:
method theMethod(isTrue)
if isTrue then
print!(“True!”)
end
return
end
This will print “True!” to the standard output if the variable “isTrue”‘s value is equal to “true”.
It would not print “True!” to the standard output if we did:
theMethod(false)
Because then “isTrue” would be equal to “false”.
We can also add more parameters to the method:
method theMethod(isTrue, isTrueStr)
if isTrue and isTrueStr not = “” then
print!(“True!”)
end
return
end
We would then need to modify the method call to adhere to the new amount of parameters:
theMethod(true, “true”)
Simply typing:
theMethod(true)
Would produce an error.
All methods must have a return statement, which will either stop the execution of the method, or stop the execution of the method and give the method a value.
A method must have a return value regardless of any conditions, so make sure to add a return statement at the bottom of the method (Which will not be reached if a return statement happens before it) and make sure it is not inside a statement, or you will get errors.
‘Return’ Statements
Prerequisites:
– An active Blue project on your computer
– An open .BL file in a text editor of your choice with a defined method
– The Blue compiler
Return statements are important for two reasons;
A) – They can stop the execution of a method, which is extremely useful to avoid excessive if statements
B) – They allow methods to have a value, and in turn be assigned to variables and other return statements
To declare a return statement, type the following:
return {optional value}
If {optional value} is omitted, the method will merely stop execution, and will have no value.
If {optional value} is not omitted, however, the method will stop execution and will now hold the value of {optional value}
‘If’ Statements
Prerequisites:
– An active Blue project on your computer
– An open .BL file in a text editor of your choice with a defined method
– The Blue compiler
An ‘If’ statement is a very standard way of checking if a certain condition is true and is implemented is nearly every language in existence.
In Blue, you can write an ‘If’ statement like so:
if {condition} then
print!(“Condition is true”)
end
If we had a variable called “cond” and it was equal to true, an example of {condition} would simply be “cond”.
That would look something like:
if cond then
print!(“Condition is true”)
end
If we used the ‘not’ operator in front of the condition, this would reverse the value of the condition and in turn would not execute the code inside the ‘If’ statement.
That would look something like:
if not cond then
print!(“Condition is false”)
end
Blue also supports multiple lined ‘If’ and ‘Else If’ statements:
if not cond then
print!(“Condition is false”)
end
‘Else If’ Statements
Prerequisites:
– An active Blue project on your computer
– An open .BL file in a text editor of your choice with a defined method
– The Blue compiler
An ‘Else If’ statement continues the control flow of one or many ‘If’ statements if the condition was false and the ‘Else If’ condition’s condition is true.
An example of an ‘Else If’ statement would be:
cond = true
if not cond then
print!(“Condition is false”)
else if cond then
print!(“Condition is true”)
end
You can replace ‘cond’ with any valid condition, and to learn about conditions, please see: ‘If’ Statements.
Please note that the example above is considered a bad practice and was only used as an example. A better approach for handling the above scenario is to use a single ‘Else’ statement (See: ‘Else’ Statements).
An example of a better approach for handling the above scenario would be:
cond = true
if not cond then
print!(“Condition is false”)
else
print!(“Condition is true”)
end
‘Else’ Statements
Prerequisites:
– An active Blue project on your computer
– An open .BL file in a text editor of your choice with a defined method
– The Blue compiler
An ‘Else’ statement continues the control flow of one or many ‘If’ statements if the condition was false.
An example of an ‘Else’ statement would be:
cond = true
if not cond then
print!(“Condition is false”)
else
print!(“Condition is true”)
end
Written by: Stephen Byrne