Title: CISC 110 Day 2
1CISC 110Day 2
2Outline
- Comments
- Data Types
- Conditional Statements (Branching)
- Loops
- Functions
- Variable Scope
3Comments
- Two Ways to Comment Code
- // This is a comment
- /
- This is a comment spanning multiple
- lines
- /
3
4Data Types
- Integer (e.g. 5)
- Number (e.g. 5.5)
- String (e.g. Hello!)
- Boolean (true/false)
4
5Comparison Operations
- Operators
- (is equal to)
- ! (not equal to)
- gt (greater than)
- gt (greater than or equal to)
- lt (less than)
- lt (less than or equal to)
-
6Boolean Data Type
Example var xBoolean x (1
1) trace(x) Output true
6
7Conditional Statements
- Statements
- if (something is true)
-
- execute these lines of code
-
- else
-
- else, execute these lines of code
-
-
7
8Conditional Statements
Example var x 10 if(x lt 100)
trace(Howdy!) Output Howdy!
8
9Conditional Statements
Example var x 100 if(x lt 10)
trace(Howdy!) else trace(Heya) Output
Heya
9
10Logical Operators
- Operators
- (and) e.g. (true true) true
- (true false) false
- (or) e.g. (true false) true
10
11Logical Operators
Example var x 1 var y 2 if((x 1)
(y 2)) trace(Howdy!) Output
Howdy!
11
12While Loop
- Statement
- while (something is true)
-
- execute these lines of code
-
13While Loop
Example var x 1 while(x lt 10) x
1 trace(x) Output 10
13
14For Loop
- Statement
- for(until condition is not true)
-
- execute these lines of code
-
14
15For Loop
Example for(var k 1 k lt 10 k k 1)
trace(!)
Output ! ! ! ! ! ! ! ! !
15
16For Loop
Example for(var k 1 k lt 10 k)
trace(!)
Output ! ! ! ! ! ! ! ! !
16
17Functions
Example function displayMessage() trace(I
\m in a function!) displayMessage() Outpu
t Im in a function!
17
18Function with Argument
Example function displayMessage(messageString)
trace(message) displayMessage(I\m in
a function!) Output Im in a function!
18
19Function with Arguments
Example function displayMessage(m1String,
m2String) trace(m1m2) displayMessage(
Well, Hello) Output WellHello
19
20Function with Return Value
Example var sString function
createMessage(xString)String return
Hello x ! s createMessage(CISC
110) trace(s) Output Hello CISC 110!
20
21Variable Scope
Scope means where variables exist, and can
therefore be accessed, in a program. Local
Variables Variables defined inside a function
only exist within that function. They are
created when the function starts executing and
are destroyed when it finishes. Global Variables
Variables defined outside of a function exist
everywhere after theyre defined, except inside a
function with a duplicate variable name.
21
22Variable Scope
Example var sString function
createMessage(xString) return Hello x
! s createMessage(CISC
110) trace(x) Output undefined
22
23Null
null A variable can be assigned to the value
null (e.g. var x null). undefined
Variables should not be assigned to the value
undefined this is the value automatically
assigned to variables that have not been assigned
to any other value.
23