Difference between revisions of "S2 Guide: Language Tutorial"
(→Variables: text-machete!) |
(→Variables) |
||
Line 10: | Line 10: | ||
[http://en.wikiversity.org/wiki/Introduction_to_Programming/Variables#Variables Variables on wikiversity] | [http://en.wikiversity.org/wiki/Introduction_to_Programming/Variables#Variables Variables on wikiversity] | ||
+ | |||
+ | [http://www.greenteapress.com/thinkpython/thinkCSpy/html/chap02.html How to Think Like a Computer Scientist (Python version): Variables, expressions and statements] (via <dwcomm>intro_to_cs</dwcomm>) | ||
== Declaring a variable == | == Declaring a variable == |
Revision as of 09:11, 14 June 2010
S2 is a programming language designed to be a style system. It has properties in common with Perl, Python, and Java.
Contents
Variables
Variables are like containers that hold values. You can put values into them, use their values in your programming, or change their value to something else.
Variables are one of the basic concepts of programming. If you're not familiar with them, you may be able to pick up what they are and do from context here, or you can also look for some more in-depth explanations.
More about variables (programming) on wikipedia
How to Think Like a Computer Scientist (Python version): Variables, expressions and statements (via intro_to_cs)
Declaring a variable
In S2, variables must be declared ahead of time in order to be used. The basic format goes:
var TYPE variablename;
- var indicates you're creating a new variable
- TYPE describes what type of variable you're making. The type can be one of the basic types, described below, or a previously defined class (to be explained later). This type doesn't change.
- variablename is the name you give the variable in your code. This name must start with a letter and can contain letters, digits, and the underscore character ("_").
You can assign a variable a value when declaring it, or wait until later--but make sure to assign a variable a value before you use it! An example of declaring a variable while assigning it is:
var TYPE variablename = VALUE;
Or, in real code:
var string greetings = "hello";
Note: you can assign the value of a variable to another variable, as well, if they are the same type. Here's an example that gives variable1 the value of variable2:
var TYPE variable1 = $variable2;
Or, in real code:
var string goodbye = greetings;
Basic Types
Strings
String variables, which are lists of characters, are declared as string in S2. You can define strings in one of two ways.
The first way comes in between double quotes:
var string test = "Testing";
The second way comes in between sets of three double quotes--this is very useful when writing HTML, which can have a lot of single double quotes in it. Here's an example:
"""I am also a string, but I can have "quotes" inside me.""";
The above example demonstrates another property of string values, in that if you don't assign them to a variable name, they'll print to output.
Some characters need to be escaped to show up properly in strings, with a backslash (\):
- If you need a newline, use: \n
- If you need a double quote inside single double quotes (or need to make three or more double quotes inside a triple double quotes), use: \"
- If you need to make a backslash, use: \\
- If you want to make a dollar sign (you'll learn why later in variables), use: \$
Integers
Integers (whole numbers) are another value like strings that the S2 language understands, and are declared with int. This is an example of a variable being assigned a literal integer value:
var int width = 500;
Booleans
A boolean is a variable that is either true or false, and is declared with bool. Here is an example that expresses having candy but no cake as boolean variables:
var bool has_candy = true; var bool has_cake = false;
Arrays
An array is a list of items. They must all be the same type! Declaring an array variable is a little different from the usual variable declaration:
var TYPE[] variablename;
The [] after the type lets the program know this variable is an array.
Here is an example with strings:
var string[] counting = ["one", "two", "three"]
Associative arrays
Instead of an ordered list of items, an associative array gives each item in it a "key" that serves as an identifying label. All keys must be strings, and all the times must be the same type. Like regular arrays, declaring associative arrays is a little different:
var TYPE{} variablename;
The {} after the type lets the program know this variable is an associative array.
Here's an example with strings:
var string{} fruits = ["apple" => "red", "lemon" => "yellow", "grape" => "purple"]
Accessing variables
Putting a $ in front of the variable name lets you access it: $variablename. Remember how in strings, the dollar sign needed to be escaped like \$? That's because when you reference a variable in a string, it's replaced with the value of the variable! For instance, this code:
var string greeting = "hi"; print "I greet you with: $greeting\n"; $greeting = "hello"; print "I greet you with: $greeting\n"; print "I greet you with: \$greeting\n";
Will print out:
I greet you with hi I greet you with hello I greet you with $greeting
For regular arrays, you'll need to refer to the position of the value you want in the array; the count starts at 0. Example:
var string[] counting = ["one", "two", "three"] print "I have $counting[2] bananas and $counting[1] apples and $counting[0] orange.\n";
Will print out:
I have three bananas and two apples and one orange.
For associative arrays, you'll want to use the key of the value you want. Example:
var string{} fruits = ["apple" => "red", "lemon" => "yellow", "grape" => "purple"] print "I have $fruits{"grape"}-colored pants.\n";
Will print out:
I have purple-colored pants.
Properties
Properties are special global variables that are available between layers. EXPAND
Statements
If/else
Foreach
Operators
Assignment
Arithmetic operators
Comparison operators
Logical operators
Type operators
String concatenation
Range operator
Named unary operators
A unary operator takes one argument after the operator, and returns one value.
- isnull returns a true value if the argument is null (has no value) and false otherwise
- defined is the opposite of isnull--it returns true when the argument is defined and false if it hasn't been
- new creates a new instance of the given classname; read more about this in the #classes section.
- null creates an undefined variable of the given classname; chances are, you won't use this
- reverse is used on strings and will return a copy of the given string with all the characters reversed
- size takes an array and returns the number of elements in it
- reverse $array
- returns shallow copy of $array with elements reversed.