Introduction
We begin by looking at the features of the Yas programming language and learning how to write simple programs to solve arithmetic problems.
Program Structure
A program in Yas consists of a sequence of statements. Each statement is an instruction to the interpreter to perform some kind action. Statements are executed in order, from top to bottom.
Each statement ends with a line break, also known as a newline. Multiple statements per line are allowed, as well - if they are separated by semicolons.
All blank spaces are ignored. Blank lines between statements are also ignored.
Print Statement
The simplest statement is the print statement.
It consists of the keyword print followed by an expression, and will output the value of this expression to the Console.
The simplest expression is just a number:
print 1You can print multiple values at once, if you separate them with commas:
print 0, 1, 1, 2, 3, 5, 8, 13, 21A print keyword without an argument expression will print a blank line to the console:
printPrint is an example of a built-in command - meaning a special type of instruction statement baked right into the language.
Numbers
Fractional (decimal) numbers are written with a period:
print 3.14159Negative numbers are preceded by a minus (-) sign:
print -42Characters and Strings
Characters and strings of characters (words, sentences, etc.) comprise another type of data in Yas. A string value is represented in program code by text wrapped in double quotes:
print "Hello"
print "How are you?"It is possible for a string to be empty, i.e. contain no characters:
# This will print a blank line.
print ""Comments
In the code examples above, any text following the hash sign #, until the end of the line on which it appears, is called a comment. Comments are ignored by the interpreter, but are useful for leaving notes and explanations in the code. You can insert comments anywhere between statements. You can also temporarily disable sections of code by commenting out the statements you don't want to execute.
# This is a comment describing the action of the program.
# It spans over multiple lines, each preceded with a # character.
print "Foo" # This is a comment just for this statement.
# The following statement will not be executed at all - it is "commented out".
# print "FizzBuzz"The Code Editor, Console and Interpreter
Let's look at the basic parts of the coding environment.
- The code editor is a tool for writing and editing code. It uses its understanding of the language syntax to highlight specific keywords, numbers and other elements of the syntax.
- To the right of the editor, you will find the console. The console is a window for displaying the output of your program. When Yas executes a print statement, it displays the value of the expression in the console.
- The interpreter is an invisible part of the coding environment that "understands" the program source code typed into the code editor and translates the statements of the Yas language into a sequence of commands for your Web browser that cause the program to be executed and its output shown in the console box.
Press the RUN button to trigger the interpreter and execute the program. Press CLEAR CONSOLE to erase the contents of the console.
Exercise
Run the following program. Then try to modify some of the lines in the editor and see how it affects the output:
console