Programs and Statements
A computer program is a list of statements for a computer to execute. When you write a program, you are essentially writing a list of things you want the computer to do. We refer to each instruction as a statement, and therefore coding (or programming) can be thought of as writing a series of statements. The editor below shows a short program with three statements.
print("Hello world!"); // Statement 1 var x = 5; // Statement 2 print(x + 2); // Statement 3
In programming, the syntax of a language is the set of rules that you must follow for the computer to understand your code. Consider the editor below: the first line uses correct syntax, whereas the second line does not use correct syntax. In the output, there is an error because we did not use proper syntax.
print("Hello world!"); // Correct syntax print "Hello world!"; // Incorrect syntax
var [name] = [value]. For example,
var x = 5; will store the value "5" in a variable named
x, and any time you reference the variable
var x = 5; print(2*x); var yourName = "John Doe"; print(yourName); var a = 3; print(a * x);
In the code above, the variables
a are defined and used. We use the
var keyword to declare or create a variable. Once a variable has been defined, you can actually change its value by using a statement of the form
[name] = [new value]. The editor below shows this idea, where a variable is defined and used, then changed and used again.
var x = 6; print(x); x = 8; print(x); x = x + 2; print(x);
var x = 5, y = 3, z = -2; print(x); print(y); print(z);
Operators are essentially tools for taking one or two values, and then producing a new value from them. For example, addition is an operator. Notice that \( 5 + 3 = 8 \). The addition "operator" takes 5 and 3 and then returns 8. In programming, there are many useful operators. We will cover a lot of operators on this page. For now, think of operators in a way similar to additon. The editor below shows a few statements that use some basic arithmetic operators.
var x = 5; print(x + 3); // Addition print(2 * x); // Multiplication print(x - 6); // Subtraction
In coding, expressions are just groups of values, variables and operators that will evaluate to a single value. For example,
5 + 3 is an expression. So is
x + 5/3. Expressions are used very often in programming. We will be using this term very frequently - just think of it as using old values and operators to create a new value.