25 october conditionals and loops. presentations brendan: cyberwarfare casey: online shopping (no...

25
25 October Conditionals and Loops

Post on 20-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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

25 October

Conditionals and Loops

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

Presentations

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

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

Announcements

Papers: Good batch of papers Feel free to talk to me about any

comments – content or style “Hygiene” – staple, title, page

numbers, proof

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

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

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

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

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

Comment Examples

var start = 0; // beginning of the string

var start = 0; /* identifies the place start processing */

var start = 0; /* beginning of the stringvar end = 0;

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

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

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

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

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

Expressions: String

Primary one is concatenation Uses “+”

If with either entry is a string, means concatenate

Else means add

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

Expressions: Boolean

Comparisons Numeric or string >, <, == Not and combinations

Logical operations And, or, not

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

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?

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

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

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

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; }

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

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;};

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

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;

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

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 subtractresult = value1 - value2;sub_ops = sub_ops + 1;};

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

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

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

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

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

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”);

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

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?

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

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 usefor ( itervar = 0; itervar < endval ; itervar++) {

Statement-list}

var++ is a shorthand that is equivalent to var = var + 1;

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

What it does

for ( i = 0 ; i < n ; i++) {Statement-list}

1. i = 0;2. execute statement-list3. i = i + 1;4. if i < n go to #2

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

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; }

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

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

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

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; }