Coding Basics - XRRCA/CreativeCoding GitHub Wiki

Variables

Variables are names which you assign values to, and can reassign value to later (hence the name). They're essential to a program's ability to "remember" values for later, or to pass values around within the program, and to your own ability to remembering what your code does and what each piece of data is for.

There are many different ways and types of variables you can define. Some examples in different common languages below

// Javascript
let name = 'rca';
// C++
float i = 35 / 2;
# Python
i, j = 0, 5

Control Flow

You can run different parts of your program at different times, in case you want to only do something if a condition is met (conditionals), or you want to repeat a task until some condition is met (loops).

Conditionals

Statements like if...else and switch let you run certain code and skip certain code when certain conditions are met.

i = 2
if (i < 5) {
  console.log('i is less than 5');
} else {
  console.log('i not less than 5');
}

results in the following output

i is less than 5

You can chain else if statements to an if as many times as you like, or not at all. Sometimes, for simple and long chained conditionals, a switch statement might be more appropriate:

switch (pay_grade) {
  case 31:
    return 41489;
  case 32:
    return 42631;
  case 33:
    return 43805;
  case 34:
    return 45016;
}

and the equivalent if...else blocks:

if (payGrade == 31) {
  return 41489;
} else if (payGrade == 32) {
  return 42631;
} else if (payGrade == 33) {
  return 43805;
} else if (payGrade == 34) {
  return 45016;
}

Loops

Most languages support while and for loops. A while loop repeats its body while a condition is true, while a for loop instantiates a variable, usually a counter, and runs its body until the variable no longer satisfies a condition.

let i = 0;
while (i < 10) {
  console.log(i);
  i += 2;
}

is equivalent to

for (let i = 0; i < 10; i+=2) {
  console.log(i);
}

and results in the following output

0
2
4
6
8

Loops are often used to iterate over lists. for loops sometimes also support this use case by letting you easily instantiate a variable which represents the current list item.

list = [0, 1, 2, 3]
while i < len(list):
    print(list[i])
    i += 1

is equivalent to

list = [0, 1, 2, 3]
for i in range(len(list)):
    print(list[i])

and results in the following output

0
1
2
3

Functions

If variables are the nouns of your program, the functions are the verbs: they are what your program does. In many languages, functions are also objects, meaning they can be stored inside variables. Functions are so important to programming that an entire class of languages, functional, exist in which the whole paradigm of programming is oriented around functions.

def test(a):
  return a > 5

is equivalent to

function test(a) {
  return a > 5;
}

is equivalent to

test = lambda a: a > 5

is equivalent to

const test = (a) => a > 5;

Functions take parameters, which are values passed into the function, and can return values. When you call a function, control of the program moves the function body, and is returned to the callee when the function returns or finishes execution.

def functionName(parameter1, parameter2):
  # function body
  return parameter1 + parameter2

Objects

An object is a thing in your program which has essential has variables of its own, called properties or members.

point1 = {x: 5, y: 25}
point2 = {x: 0, y: -1, normalized: false}

console.log(point1.x - point2.x);
console.log(point2.normalized);

results in the following output

5
false

Classes

In some languages, objects are typed with classes. For example, an object may be a specific job position, have properties like salary, sick leave, holiday allocation, employer, and be of a class called Job.

class Job:
  def __init__(self, pay_grade, sick_leave, holiday_leave, employer):
    self.salary = getSalary(pay_grade)
    self.sick_leave = sick_leave
    self.holiday_leave = holiday_leave
    self.employer = employer

myJob = new Job(31, 5, 25, 'RCA')

which is equivalent to

class Job {
  constructor(payGrade, sickLeave, holidayLeave, employer) {
    this.payGrade = payGrade;
    this.sickLeave = sickLeave;
    this.holidayLeave = holidayLeave;
    this.employer = employer;
  }
}

myJob = new Job(31, 5, 25, 'RCA')

Footnotes

† An excellent criticism of the noun focused programming found in object oriented languages can be found on Steve Yegge's blog

Further reading

⚠️ **GitHub.com Fallback** ⚠️