1 / 23

CISC 110 Day 2

CISC 110 Day 2. Programming Basics. Outline. Comments Data Types Conditional Statements (Branching) Loops Functions Variable Scope. Comments. Two Ways to Comment Code: // This is a comment /* This is a comment spanning multiple lines */. 3. Data Types. Integer (e.g. 5)

radley
Télécharger la présentation

CISC 110 Day 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CISC 110Day 2 Programming Basics

  2. Outline • Comments • Data Types • Conditional Statements (Branching) • Loops • Functions • Variable Scope

  3. Comments Two Ways to Comment Code: // This is a comment /* This is a comment spanning multiple lines */ 3

  4. Data Types Integer (e.g. 5) Number (e.g. 5.5) String (e.g. “Hello!”) Boolean (true/false) 4

  5. Operators: == (is equal to) != (not equal to) > (greater than) >= (greater than or equal to) < (less than) <= (less than or equal to) Comparison Operations

  6. Boolean Data Type Example: var x:Boolean; x = (1 == 1); trace(x); Output: true 6

  7. Conditional Statements Statements: if (…something is true…) { …execute these lines of code… } else { …else, execute these lines of code… } 7

  8. Conditional Statements Example: var x = 10; if(x <= 100) { trace(“Howdy!”); } Output: Howdy! 8

  9. Conditional Statements Example: var x = 100; if(x <= 10) { trace(“Howdy!”);} else { trace(“Heya”);} Output: Heya 9

  10. Logical Operators Operators: && (and) e.g. (true && true) == true (true && false) == false || (or) e.g. (true || false) == true 10

  11. Logical Operators Example: var x = 1; var y = 2; if((x == 1) && (y == 2)) { trace(“Howdy!”); } Output: Howdy! 11

  12. While Loop Statement: while (…something is true…) { …execute these lines of code… }

  13. While Loop Example: var x = 1; while(x < 10) { x += 1; } trace(x); Output: 10 13

  14. For Loop Statement: for(…until condition is not true…) { …execute these lines of code… } 14

  15. For Loop Example: for(var k = 1; k < 10; k = k + 1) { trace(“!”); } Output: ! ! ! ! ! ! ! ! ! 15

  16. For Loop Example: for(var k = 1; k < 10; k++) { trace(“!”); } Output: ! ! ! ! ! ! ! ! ! 16

  17. Functions Example: function displayMessage() { trace(“I\’m in a function!”); } displayMessage(); Output: I’m in a function! 17

  18. Function with Argument Example: function displayMessage(message:String) { trace(message); } displayMessage(“I\’m in a function!”); Output: I’m in a function! 18

  19. Function with Arguments Example: function displayMessage(m1:String, m2:String) { trace(m1+m2); } displayMessage(“Well…”,” Hello…”); Output: Well…Hello… 19

  20. Function with Return Value Example: var s:String = “”; function createMessage(x:String):String { return “Hello “ + x + ”!”; } s = createMessage(“CISC 110”); trace(s); Output: Hello CISC 110! 20

  21. Variable 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 they’re defined, except inside a function with a duplicate variable name. 21

  22. Variable Scope Example: var s:String = “”; function createMessage(x:String) { return “Hello “ + x + ”!”; } s = createMessage(“CISC 110”); trace(x); Output: undefined 22

  23. Null 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

More Related