expressions & control flow - university of washington · 2018-01-23 · l08: expressions &...
TRANSCRIPT
CSE120, Winter 2018L08: Expressions & Control Flow
Expressions & Control FlowCSE 120 Winter 2018
Instructor: Teaching Assistants:Justin Hsia Anupam Gupta, Cheng Ni, Eugene Oh,
Sam Wolfson, Sophie Tian, Teagan Horkan
Twitter: More than 677,000 U.S. users engaged with Russian troll accounts“Twitter said… it's notifying 677,775 people in the U.S. who either followed, retweeted or liked a tweet from accounts of the Kremlin-linked troll farm known as the Internet Research Agency during the 2016 election period… The company also said that… it's identified an additional 13,512 Russian-linked bot accounts that tweeted around the election, bringing the total to 50,258.“Facebook unveiled a portal last month to allow users to learn of any Facebook or Instagram contact they may have had with Russian internet trolls. Facebook has said that Russian-linked posts were viewed by up to 126 million people during that period.”• https://www.politico.com/story/2018/01/19/twitter‐users‐
russian‐trolls‐437247
CSE120, Winter 2018L08: Expressions & Control Flow
Administrivia
Assignments: Animal Functions due tonight (1/22) Reading Check 3 due before lab on Thursday (1/25) Jumping Monster due Friday (1/26)
“Big Ideas” this week: The Internet
2
CSE120, Winter 2018L08: Expressions & Control Flow
Outline
Expressions & Operators Conditionals if‐statement
Loops while‐loop for‐loop
3
CSE120, Winter 2018L08: Expressions & Control Flow
Expressions
“An expression is a combination of one or more values, constants, variables, operators, and functionsthat the programming language interprets and computes to produce another value.” https://en.wikipedia.org/wiki/Expression_(computer_science)
Expressions are evaluated and resulting value is used Assignment: x = x + 1; Assignment: x_pos = min(x_pos + 3, 460); Argument: ellipse(50+x, 50+y, 50, 50); Argument: mouse(rowX+4*50,rowY,rowC);
4
CSE120, Winter 2018L08: Expressions & Control Flow
Operators
Built‐in “functions” in Processing that use special symbols: Multiplicative: * / % Additive: + - Relational: < > <= >= Equality: == != Logical: && || !
Operators can only be used with certain data types and return certain data types Multiplicative/Additive: give numbers, get number Relational: give numbers, get Boolean Logical: give Boolean, get Boolean Equality: give same type, get Boolean
5
CSE120, Winter 2018L08: Expressions & Control Flow
Operators
Built‐in “functions” in Processing that use special symbols: Multiplicative: * / % Additive: + - Relational: < > <= >= Equality: == != Logical: && || !
Logical operators use Boolean values (true, false)
6
x y x && yfalse false falsefalse true falsetrue false falsetrue true true
x !xfalse truetrue false
AND (&&) OR (||) NOT (!)x y x || y
false false falsefalse true truetrue false truetrue true true
CSE120, Winter 2018L08: Expressions & Control Flow
Operators
Built‐in “functions” in Processing that use special symbols: Multiplicative: * / % Additive: + - Relational: < > <= >= Equality: == != Logical: && || !
In expressions, use parentheses for evaluation ordering and readability e.g. x + (y * z) is the same as x + y * z, but easier to read
7
CSE120, Winter 2018L08: Expressions & Control Flow
Modulus Operator: %
x % y is read as “xmod y” and returns the remainder after y divides x For short, we say “mod” instead of modulus
Practice:
0 % 3 is ____
1 % 3 is ____
2 % 3 is ____
3 % 3 is ____
8
4 % 3 is ____
5 % 3 is ____
6 % 3 is ____
CSE120, Winter 2018L08: Expressions & Control Flow
Modulus Operator: %
x % y is read as “xmod y” and returns the remainder after y divides x For short, we say “mod” instead of modulus
Example Uses:
Parity: Number n is even if n%2 == 0
Leap Year: Year year is a leap year if year%4 == 0
Chinese Zodiac: year1 and year2 are the same animal if year1%12 == year2%12
9
CSE120, Winter 2018L08: Expressions & Control Flow
Modulus Example in Processing
Use mod to “wrap around” Replace min/max function to “connect” edges of drawing canvas
x_pos = min(x_pos + 3, 460); x_pos = (x_pos + 3) % 460;
10
CSE120, Winter 2018L08: Expressions & Control Flow
Control Flow
The order in which instructions are executed
We typically say that a program is executed in sequence from top to bottom, but that’s not always the case: Function calls and return calls Conditional/branching statements Loops
Curly braces {} are used to group statements Help parse control flow Remember to use indentation!
11
CSE120, Winter 2018L08: Expressions & Control Flow
Outline
Expressions & Operators Conditionals if‐statement
Loops while‐loop for‐loop
12
CSE120, Winter 2018L08: Expressions & Control Flow
If‐Statements
Sometimes you don’t want to execute everyinstruction Situationally‐dependent
Conditionals give the programmer the ability to make decisions The next instruction executed depends on a specified condition• The condition must evaluate to a boolean (i.e. true or false)• Sometimes referred to as “branching”
This generally lines up well with natural language intuition
13
CSE120, Winter 2018L08: Expressions & Control Flow
If‐Statements
Basic form:if(condition) {
// "then" // statements
}
Example conditions: Variable: if( done == true ) Variable: if( done ) Expression: if( x_pos > 460 ) Expression: if( x_pos > 100 && y_pos > 100 )
14
Condition?
Start
End
“Then”Statements
True
False
CSE120, Winter 2018L08: Expressions & Control Flow
If‐Statements
With else clause:if(condition) {
// "then" // statements
} else {// "otherwise"// statements
}
15
Condition?
Start
End
“Then”Statements
True
False
“Otherwise”Statements
CSE120, Winter 2018L08: Expressions & Control Flow
If‐Statements
With else if clause:if(cond1) {
// "then"// statements
} else if(cond2) {// "otherwise if"// statements
}
16
Cond1?
Start
End
“Then”Statements
True
False
“Otherwise if”Statements
Cond2?False
True
CSE120, Winter 2018L08: Expressions & Control Flow
If‐Statements
Notice that conditionals always go from Start to End Choose one of many branches A conditional must have a single if, as many else if as desired, and at most one else
Can nest and combine in interesting ways:
17
if(cond1 && cond2) {// statement1
} else if(cond1) {// statement2
}
if(cond1) {if(cond2) {
// statement1} else {
// statement2}
}
CSE120, Winter 2018L08: Expressions & Control Flow
Peer Instruction Question
Which value of x will get the following code to print out "Maybe"?
A. 1B. 3C. 5D. 7E. We’re lost…
Think for a minute, then discuss with your neighbor(s) Vote at http://PollEv.com/justinh
18
if (x == 5) {print("Yes");
} else if ((x >= 6) || (x < 2)) {print("No");
} else {print("Maybe");
}
CSE120, Winter 2018L08: Expressions & Control Flow
Processing Demo: Drawing Dots
19
CSE120, Winter 2018L08: Expressions & Control Flow
Outline
Expressions & Operators Conditionals if‐statement
Loops while‐loop for‐loop
20
CSE120, Winter 2018L08: Expressions & Control Flow
Looping
Sometimes we want to do the same (or similar) things over and over again Looping saves us time from writing out all of the instructions
Loops control a sequence of repetitions
21
CSE120, Winter 2018L08: Expressions & Control Flow
While‐Loop
Basic form:while(condition) {
// loop // body
}
Repeat loop body until condition is false Must make sure to update conditional variable(s) in loop body, otherwise you cause an infinite loop
draw() is basically a while(true) loop22
Condition?
Start
End
Loop Body
False
True
CSE120, Winter 2018L08: Expressions & Control Flow
While‐Loop
More general form:// init cond var(s)while(condition) {
// loop body// update var(s)
}
This occurs so commonly that we create a separate syntax for it!
23
Start
Initialize Var(s)
Condition?
End
Loop Body
False
True
Update Var(s)
CSE120, Winter 2018L08: Expressions & Control Flow
For‐Loop
for(init; cond; update){// loop body
}
First runs init expression(s) Then checks cond If true, runs loop bodyfollowed by update statement(s)
24
Start
Initialize Var(s)
Condition?
End
Loop Body
False
True
Update Var(s)
CSE120, Winter 2018L08: Expressions & Control Flow
For‐Loop Example
25
Without loop:
With loop:
CSE120, Winter 2018L08: Expressions & Control Flow
Understanding the For‐Loop
Choice of variable name(s) is not critical Represent the value(s) that vary between different executions of the loop body Think of as temporary variable(s)
Variable scope: variable i only exists within this loop
26
initialization
CSE120, Winter 2018L08: Expressions & Control Flow
Understanding the For‐Loop
Condition evaluated before the loop body and must evaluate to true or false Reminder: > greater than
< less than>= greater than or equal to>= less than or equal to== equal to!= not equal to
27
condition
CSE120, Winter 2018L08: Expressions & Control Flow
Understanding the For‐Loop
Update is an assignment that is executed after the loop body
Loop body is enclosed by curly braces {} and should be indented for readability
28
update
loop body
CSE120, Winter 2018L08: Expressions & Control Flow
Processing Demo: Circles on Canvas Edge
29
CSE120, Winter 2018L08: Expressions & Control Flow
Processing Demo: Circles on Canvas Edge
30