Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fluency with Information Technology INFO100 and CSE100 Katherine Deibel 2012-02-06Katherine Deibel, Fluency in Information Technology1.

Similar presentations


Presentation on theme: "Fluency with Information Technology INFO100 and CSE100 Katherine Deibel 2012-02-06Katherine Deibel, Fluency in Information Technology1."— Presentation transcript:

1 Fluency with Information Technology INFO100 and CSE100 Katherine Deibel 2012-02-06Katherine Deibel, Fluency in Information Technology1

2  Many names: conditionals, branches, tests  Programs are typically step-to-step and sequential  Branches allow programs to follow different paths of execution based on  User feedback  Variable values  Comparisons between values 2012-02-06Katherine Deibel, Fluency in Information Technology2

3  Boolean: a value that is true or false  Boolean variable: a JS variable that has been set to either true or false  [Boolean] test: a computation that returns a Boolean value 2012-02-06Katherine Deibel, Fluency in Information Technology3

4  Conditionals test an expression to see ifit is true or false  General Form: if( ) ;  In JavaScript: if(day == "Monday") evening_plan = "Watch HIMYM"; 2012-02-06Katherine Deibel, Fluency in Information Technology4

5  Do not put a semicolon after an if statement: if(day == "Monday");  BAD!!  View the if statement and its following line as one statement: if(day == "Monday") plan = "HIMYM"; 2012-02-06Katherine Deibel, Fluency in Information Technology5

6  Only some statements are executed day == "Monday" evening_plan = "Watch HIMYM" " Are they equal?" test Yes No 2012-02-06Katherine Deibel, Fluency in Information Technology6

7  All conditions result in either true or false  The condition is usually a test  Example: day == "Monday"  Conditions can also be a Boolean value  Suppose leap_year is a Boolean variable: if(leap_year) Feb_days = 29;  Which is equivalent to: if(leap_year == true) Feb_days = 29; 2012-02-06Katherine Deibel, Fluency in Information Technology7

8  Branch both ways with If-Then-Else: if( ) ; else ;  In JavaScript: if(leapYear) daysInFeb = 29; else daysInFeb = 28; 2012-02-06Katherine Deibel, Fluency in Information Technology8

9 if(leapYear) daysInFeb = 28 TrueFalse daysInFeb = 29 2012-02-06Katherine Deibel, Fluency in Information Technology9

10 if(month == "January") days = 31; else if (month == "February") days = 28; else if (month == "March") days = 31; … else if (month == "December") days = 31; 2012-02-06Katherine Deibel, Fluency in Information Technology10

11 Alert, Confirm, Random 2012-02-06Katherine Deibel, Fluency in Information Technology11

12  Functions are not like math functions  Functions are subroutines  One or more statements separate from the main program  Functions can be called by the main program  Functions simplify writing code by allowing coders to reuse complex statements without copying/pasting 2012-02-06Katherine Deibel, Fluency in Information Technology12

13  Returns a random decimal number x from the range 0≤x<1  Usage: var x = Math.random(); 2012-02-06Katherine Deibel, Fluency in Information Technology13

14  Generates a message box with an OK button  Message box displays the message string that is alert's parameter  Example: alert("Let's show off the alert() function"); 2012-02-06Katherine Deibel, Fluency in Information Technology14

15  The operation confirm() is like alert() except it has two buttons: Cancel and OK  When the user clicks a button, the system returns to the program the information of which button was clicked:  Cancel  0  OK  1  We can illustrate this behavior 2012-02-06Katherine Deibel, Fluency in Information Technology15

16 2012-02-06Katherine Deibel, Fluency in Information Technology16 First JavaScript body {background-color: black; color: gold; font-family; corbel;} Showing Simple JS var whatButton; whatButton = confirm("Do you bring your clicker to class every lecture?"); if(whatButton == 1) alert("You clicked OK"); else alert("You clicked Cancel");

17 2012-02-06Katherine Deibel, Fluency in Information Technology17

18  With if and if-else statements, only the immediate statement after the if and the else are executed: if(temp < 32) water = "ice"; state = "frozen";  always executed  To do more stuff, we could test again if(temp < 32) water = "ice"; if(temp < 32) state = "frozen"; 2012-02-06Katherine Deibel, Fluency in Information Technology18

19  A better solution: compound statement  Any number of statements enclosed in curly braces { }, is treated as one statement  Example: if(temp < 32) { water = "ice"; state = "frozen"; } No semicolon after the brace!! 2012-02-06Katherine Deibel, Fluency in Information Technology19

20 if(clean_clothes > 0) if(day=="Saturday") morning_plan = "Sleep In"; else morning_plan = "Do_Laundry"; 2012-02-06Katherine Deibel, Fluency in Information Technology20 If there are no clean clothes …?

21 if(clean_clothes > 0) if(day=="Saturday") morning_plan = "Sleep In"; else morning_plan = "Do_Laundry"; Else always associates with the nearest if. Indentation does not matter! 2012-02-06Katherine Deibel, Fluency in Information Technology21

22 if(clean_clothes > 0) if(day=="Saturday") morning_plan = "Sleep In"; else morning_plan = "Do_Laundry"; Put nested ifs in compound statements 2012-02-06Katherine Deibel, Fluency in Information Technology22

23 if(clean_clothes > 0) { if(day=="Saturday") { morning_plan = "Sleep In"; } else { morning_plan = "Do_Laundry"; } 2012-02-06Katherine Deibel, Fluency in Information Technology23

24  Always use curly braces with if and if-else statements  Most of the time, you will be doing it anyhow  Easier to add more statements later 2012-02-06Katherine Deibel, Fluency in Information Technology24

25 Paging Dr. Ruth or Dr. Spock… 2012-02-06Katherine Deibel, Fluency in Information Technology25

26  Make comparisons between numeric values  Used in if statements and for stop tests in loops (later topic)  Outcome is a Boolean value, true or false <less than <=less than or equal to ==equal to !=not equal to >=greater than or equal to >greater than 2012-02-06Katherine Deibel, Fluency in Information Technology26

27  = is the assignment operator  == is the test for equivalence  Never use = in an if-statement! It will usually always default to true. 2012-02-06Katherine Deibel, Fluency in Information Technology27

28  Allow you to combine multiple tests x && yboth x AND y must be true x || yeither x OR y must be true  Good practice to put each test in parentheses  Example: if(leapYear && (month == "February")) days = 29; 2012-02-06Katherine Deibel, Fluency in Information Technology28

29  Conditional statements allow us to execute some statements and not others  The test is enclosed in parentheses  We always use compound statements to group the operations of conditional statements properly  A compound statement is just a bunch of statements inside of { } … DO NOT follow it with a ;


Download ppt "Fluency with Information Technology INFO100 and CSE100 Katherine Deibel 2012-02-06Katherine Deibel, Fluency in Information Technology1."

Similar presentations


Ads by Google