Lesson 2

Variables

What Makes a program

Programs made up of different things

Variables

Conditions

Collections

Loops

Functions

Classes

Variables

Used to track values as they change over the course of a program.

A symbol or set of symbols that can change value over time.

  • Name
  • Value
  • Type

Types

"String"

"Hello World!"

Number

42

Boolean

true

text = "Hello World"
count = 12
ugly = false

Types Cont.

'Character'

'H'

Double

42.215

firstInitial = 'M'
taxRate = 0.75

Statements

A single line of instructions to follow

a = b * 2

a and are variables

2 is a literal value (it never changes)

* is an operator (it does a thing)

Numbers

What can you do with numbers? Maths!

100 / 10 - 6

2 * (10 + 6)

a = 12

b = a * 3

Special Numbers

NaN

Infinity

Strings

Text inside of quotes

"Welcome to monkey island"
'The Gorillas raped that girl'

Your supposed to use single quotes for strings though...

'Thats what "she" said'
"Your 'elephant' is huge"

Quotes inside of quotes

Strings - cont.

'Winter is \'comming\''
'Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit.'

Trailing backslash before quotes prevents excapes

Special characters inside of strings

\n creates a new line

Addings strings togeather (concatination)

'Hello' + ' ' + 'World'

a = 'Hello'
b = ' '
c = 'world!'

a + b + c

Comments

Single Line

//this won't be seen by program

Comment Block

/* Comment Blocks Need Closing Tags */

firstInitial = 'M' //for mathew

/*
 This Next Part calculates Taxes
*/

taxRate = 0.75 

New Line

Go to the next line

;

firstInitial = 'M'
secondlineVariable = 10;

{
  indentAThing = 11
  intentSecondThing = 12;
}

Code Block

{        

  // some code here ...      

}

Read as

firstInitial = 'M'secondlineVariable = 10;
{indentAThing = 11intentSecondThing = 12;}

New Line cont.

firstInitial = 'M';
secondlineVariable = 10;

{
  indentAThing = 11;
  intentSecondThing = 12;
}

Read as

firstInitial = 'M';
secondlineVariable = 10;
{indentAThing = 11;
intentSecondThing = 12;}

Operators

Minus

-

Plus

+

a = 10 - b
b = c + 7

Double Minus

--

Double Plus

++

a = 10
a++
a == 11 //true
a = 10
a--
a == 9 //true

More MATHS

Multiplication

*

a = 10 * b

Division

/

a = 10/5
a == 2

Operators

Equals

=

a = 10
b = c + 1

Double Equals

==

Checking Equality

a = 10
a++
a == 11 //true
a = 10
a--
a != 10 //true

'Assignment'

Not Equals

!=

Checking Inequality

a = 10
a++
a != 11 //false
a = 10
a--
a == 9 //true

Operators

Triple Equals

===

a = 10
a === "10" //false

a = 5
a === 5 //true
"Hello" !== "hello" //true
"true" !== false //true
a = "10"
a !== 10 //true

Matches Type

tax = "false"
tax === true //false

tax = 0
tax == false //true

Triple Not Equals

!==

Lesson 2 - Variables

By Mathew Kleppin

Lesson 2 - Variables

A quick introduction into variables

  • 393