CISC 110 Day 2
description
Transcript of CISC 110 Day 2
![Page 1: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/1.jpg)
CISC 110Day 2
Programming Basics
![Page 2: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/2.jpg)
Outline
• Comments• Data Types• Conditional Statements (Branching)• Loops• Functions• Variable Scope
2
![Page 3: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/3.jpg)
Comments
Two Ways to Comment Code:
// This is a comment
/*
This is a comment spanning multiple
lines
*/
3
![Page 4: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/4.jpg)
Data Types
Integer (e.g. 5)
Number (e.g. 5.5)
String (e.g. “Hello!”)
Boolean (true/false)
4
![Page 5: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/5.jpg)
Comparison Operations
Operators: == (is equal to)!= (not equal to)> (greater than)>= (greater than or equal to)< (less than)<= (less than or equal to)
5
![Page 6: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/6.jpg)
Boolean Data Type
6
Example:
var x:Boolean;
x = (1 == 1);
trace(x);
Output: true
![Page 7: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/7.jpg)
Conditional Statements
Statements: if (…something is true…)
{…execute these lines of code…
}else
{…else, execute these lines of code…
}
7
![Page 8: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/8.jpg)
Conditional Statements
8
Example:
var x = 10;
if(x <= 100)
{
trace(“Howdy!”);
}
Output: Howdy!
![Page 9: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/9.jpg)
Conditional Statements
9
Example:
var x = 100;
if(x <= 10)
{ trace(“Howdy!”);}
else
{ trace(“Heya”);}
Output: Heya
![Page 10: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/10.jpg)
Logical Operators
Operators:
&& (and) e.g. (true && true) == true
(true && false) == false
|| (or) e.g. (true || false) == true
10
![Page 11: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/11.jpg)
Logical Operators
11
Example:
var x = 1;
var y = 2;
if((x == 1) && (y == 2))
{
trace(“Howdy!”);
}
Output: Howdy!
![Page 12: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/12.jpg)
While Loop
Statement:
while (…something is true…)
{
…execute these lines of code…
}
12
![Page 13: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/13.jpg)
While Loop
13
Example:
var x = 1;
while(x < 10)
{
x += 1;
}
trace(x);
Output: 10
![Page 14: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/14.jpg)
For Loop
Statement:
for(…until condition is not true…)
{
…execute these lines of code…
}
14
![Page 15: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/15.jpg)
For Loop
15
Example:
for(var k = 1; k < 10; k = k + 1)
{
trace(“!”);
}
Output:!
!
!
!
!
!
!
!
!
![Page 16: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/16.jpg)
For Loop
16
Example:
for(var k = 1; k < 10; k++)
{
trace(“!”);
}
Output:!
!
!
!
!
!
!
!
!
![Page 17: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/17.jpg)
Functions
17
Example:
function displayMessage()
{
trace(“I\’m in a function!”);
}
displayMessage();
Output: I’m in a function!
![Page 18: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/18.jpg)
Function with Argument
18
Example:
function displayMessage(message:String)
{
trace(message);
}
displayMessage(“I\’m in a function!”);
Output: I’m in a function!
![Page 19: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/19.jpg)
Function with Arguments
19
Example:
function displayMessage(m1:String, m2:String)
{
trace(m1+m2);
}
displayMessage(“Well…”,” Hello…”);
Output: Well…Hello…
![Page 20: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/20.jpg)
Function with Return Value
20
Example:
var s:String = “”;
function createMessage(x:String):String
{
return “Hello “ + x + ”!”;
}
s = createMessage(“CISC 110”);
trace(s);
Output: Hello CISC 110!
![Page 21: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/21.jpg)
Variable Scope
21
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 they’re defined, except inside a function with a duplicate variable name.
![Page 22: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/22.jpg)
Variable Scope
22
Example:
var s:String = “”;
function createMessage(x:String)
{
return “Hello “ + x + ”!”;
}
s = createMessage(“CISC 110”);
trace(x);
Output: undefined
![Page 23: CISC 110 Day 2](https://reader035.fdocuments.us/reader035/viewer/2022062423/56814d05550346895dba3565/html5/thumbnails/23.jpg)
Null
23
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.