25 October Conditionals and Loops. Presentations Brendan: Cyberwarfare Casey: Online shopping (No...
-
date post
20-Dec-2015 -
Category
Documents
-
view
213 -
download
0
Transcript of 25 October Conditionals and Loops. Presentations Brendan: Cyberwarfare Casey: Online shopping (No...
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
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
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
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;
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
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
Expressions: String
Primary one is concatenation Uses “+”
If with either entry is a string, means concatenate
Else means add
Expressions: Boolean
Comparisons Numeric or string >, <, == Not and combinations
Logical operations And, or, not
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?
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
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; }
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;};
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;
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;};
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
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
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”);
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?
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;
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
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; }
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
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; }