Presentation is loading. Please wait.

Presentation is loading. Please wait.

25 October Conditionals and Loops. Presentations Brendan: Cyberwarfare Casey: Online shopping (No current event today)

Similar presentations


Presentation on theme: "25 October Conditionals and Loops. Presentations Brendan: Cyberwarfare Casey: Online shopping (No current event today)"— Presentation transcript:

1 25 October Conditionals and Loops

2 Presentations Brendan: Cyberwarfare Casey: Online shopping (No current event today)

3 Announcements Papers: Good batch of papers Feel free to talk to me about any comments – content or style “Hygiene” – staple, title, page numbers, proof

4 Documenting Your Code Documenting your code is not necessary for the compiler or interpreter, but is typically needed by people – including yourself! ALWAYS document what you write Information for people only are comments

5 Comments Two Forms /* … */ // /* … */ Everything between the delimiters is a comment and not processed Can cross multiple lines Can start anywhere // Everything from there until the end of the line is not processed

6 Comment Examples var start = 0; // beginning of the string var start = 0; /* identifies the place start processing */ var start = 0; /* beginning of the string var end = 0;

7 What to do with Variables Simplest action is to assign a value: assignment statement Name = “Tom”; Price = 13.25; Can use any type of expression on the right – both constants and variables

8 Expressions: Arithmetic There are rules of precedence 1+b*c Do I multiply or add first? If you use parentheses, you don’t have to remember! Binary operations +, -, /, *, % (modulus or remainder) Unary operations Negation

9 Expressions: String Primary one is concatenation Uses “+” If with either entry is a string, means concatenate Else means add

10 Expressions: Boolean Comparisons Numeric or string >, <, == Not and combinations Logical operations And, or, not

11 Logic Tables 0 = false, 1 = true And 0 && 0 = 0 0 && 1 = 0 1 && 0 = 0 1 && 1 = 1 Or 0 || 0 = 0 0 || 1 = 1 1 || 0 = 1 1 || 1 = 1 Not !0 = 1 !1 = 0 There are actually 8 different logical operations on two variables? How would you convince yourself of that?

12 Practice Write a statement that computes total price including tax and shipping computes the area of a circle calculates the average speed of a car trip

13 How to Change the Operations Conditional statements Iterations (doing it more than once) Functions (an algorithm that can be executed more than once) All need compound statements – multiple statements that can be processed as one { stmt; stmt; stmt; }

14 Conditional Statements Executes the statement only if a condition is true Format: if (boolean) statement; /* avoid divide by zero */ if (n != 0) m = m/n; /* if purchase made, increase total purchase price and the number of items bought */ if (purchased = true) { total = total + price; items = items + quantity; };

15 Conditional with else Executes the first statement if a condition is true or the second if it’s false Format: if (boolean) statement; else statement; /* take the absolute value */ if (n < 0) abs_val = !n; else abs_val = n;

16 Conditional with else (cont.) /* execute either add or subtract depending on button and keep track of how many of each operation is performed */ if (operation = add) { result = value1 + value2; add_ops = add_ops + 1; }; else { // must be subtract result = value1 - value2; sub_ops = sub_ops + 1; };

17 Practice Write a statement that computes total price including tax if the purchaser is in state and shipping in all cases computes the area of a circle or a square depending on whether the radius or side was given calculates the average speed of a car trip if the length of the trip was more than one hour

18 Arrays Arrays are variables with dimensions Why are they interesting? Dealing with a table of values Translations Series of different values Processing a stretch of elements Strings Series of numbers

19 Arrays in JavaScript Declaration var name = new Array (no-elems); Examples var ZodiacSigns = new Array (12); Or can initialize Var HalfZodiacSigns = new Array (“Aries”, “Taurus”, “Gemini”, “Cancer”, “Leo”, “Virgo”);

20 Referencing Arrays Array element can be referenced any place that a constant or variable can be used Form: array [ index ] Array is the name Index is which element of the array you want Can be a constant, variable, or expression Must evaluate to an integer between 0 and one less than the array size Example MySign = HalfZodiacSigns[0]; What value does MySign have?

21 Iterations: Doing it more than once If I want to process all of the elements in an array, I need to tell the computer to do something for each element: Form that we will use for ( itervar = 0; itervar < endval ; itervar++) { Statement-list } var++ is a shorthand that is equivalent to var = var + 1;

22 What it does for ( i = 0 ; i < n ; i++) { Statement-list } 1. i = 0; 2. execute statement-list 3. i = i + 1; 4. if i < n go to #2

23 Iteration Example var numbers = new Array (10); var j; /* initialize an array to 1 to 10 */ for ( j=0; j<10; j++) { numbers[j] = j+1; }

24 Practice Write a loop that computes total price including tax and shipping for 3 orders computes the area of a “n” circles calculate the average speed of 2 legs of a car trip

25 Loops with conditionals var values = new Array (10); var results = new Array (10); var j; for ( j=0; j<10; j++) { if (values[j] < 0) results[j] = -j; else results[j] = j; }


Download ppt "25 October Conditionals and Loops. Presentations Brendan: Cyberwarfare Casey: Online shopping (No current event today)"

Similar presentations


Ads by Google