Expressions & Conditionals CSE 120 Winter 2019

Slides:



Advertisements
Similar presentations
Selection (decision) control structure Learning objective
Advertisements

Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Changing Control.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Program Design and Development
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Statement-Level Control Structures Sections 1-4
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Decision Structures and Boolean Logic
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Flow of Control Part 1: Selection
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
CPS120: Introduction to Computer Science Decision Making in Programs.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
CPS120: Introduction to Computer Science Decision Making in Programs.
Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Expressions & Control Flow CSE 120 Spring 2017
Debugging and Random Numbers
Lightbot and Functions CSE 120 Winter 2017
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Topics The if Statement The if-else Statement Comparing Strings
Boolean Expressions & the ‘if’ Statement
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Chapter 6: Conditional Statements and Loops
Lesson 9: "if-else-if" and Conditional Logic
Control Structures.
Microsoft Visual Basic 2005 BASICS
Control Structures – Selection
Variables & Datatypes CSE 120 Winter 2018
Chap. 6 :: Control Flow Michael L. Scott.
Topics The if Statement The if-else Statement Comparing Strings
Expressions & Control Flow CSE 120 Winter 2018
Variables & Datatypes CSE 120 Spring 2017
Structured Program
Chap. 6 :: Control Flow Michael L. Scott.
Java Programming Control Structures Part 1
Introduction to Algorithms and Programming
Selection Statements.
Summary Two basic concepts: variables and assignments Basic types:
Testing and Repetition
Statement-Level Control Structures
Conditional Execution
Relational & Logical Operators, if and switch Statements
Programming Languages
Functions in Processing CSE 120 Winter 2019
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Basic Input and Output CSE 120 Winter 2019
Variables & Datatypes CSE 120 Winter 2019
Chapter8: Statement-Level Control Structures April 9, 2019
Flow of Control.
Variables, Assignments Testing + Iteration
Arrays CSE 120 Winter 2019 Instructor: Teaching Assistants:
Using Decision Structures
Loops & Nested Loops CSE 120 Winter 2019
Javascript Chapter 19 and 20 5/3/2019.
Buttons & Boards CSE 120 Winter 2019
Using C++ Arithmetic Operators and Control Structures
Controlling Program Flow
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Expressions & Conditionals CSE 120 Winter 2019 Instructor: Teaching Assistants: Justin Hsia Ann Shan, Eunia Lee, Pei Lee Yap, Sam Wolfson, Travis McGaha Delivery Robot Engulfed in Flames, Honored on Campus With Candlelight Vigil “The University of California, Berkeley, lost a beloved member of their campus last week, when a delivery robot was engulfed in flames outside the student union. “The courier rovers started delivering food to students about two years ago. The service seems to have been operating relatively smoothly until… a Kiwibot spontaneously combusted while on the job. “After an investigation, the company revealed … ‘a defective battery was put in place of a functioning one. This caused an exceedingly rare occurrence of the battery experiencing thermal runaway.’ ” https://gizmodo.com/delivery-robot-engulfed-in-flames- honored-on-campus-wi-1831145871

Administrivia Assignments: “Big Ideas” this week: Digital Distribution Animal Functions due tonight (1/28) Reading Check 4 due Thursday @ 3:30 (1/31) Jumping Monster due Friday (2/1) “Big Ideas” this week: Digital Distribution

Outline Expressions & Operators Conditionals if else else if

Expressions “An expression is a combination of one or more values, constants, variables, operators, and functions that 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: drawMouse(rowX+4*50,rowY,rowC);

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

Operators Built-in “functions” in Processing that use special symbols: Multiplicative: * / % Additive: + - Relational: < > <= >= Equality: == != Logical: && || ! Logical operators use Boolean values (true, false) AND (&&) OR (||) NOT (!) x y x && y false true x y x || y false true x !x false true

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

Modulus Operator: % x % y is read as “x mod 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

Conditionals Worksheet Work just on Page 1 (Questions 1-6) Operators: Arithmetic: + - * / % Relational: < > <= >= Equality: == != Logical: && || ! Data Types: Arithmetic: give numbers, get number Relational: give numbers, get Boolean Logical: give Boolean, get Boolean Equality: give same type, get Boolean

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;

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!

Outline Expressions & Operators Conditionals if else else if

If-Statements Sometimes you don’t want to execute every instruction 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

If-Statements Basic form: if(condition) { // "then" // statements } Start End “Then” Statements True False 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 )

If-Statements With else clause: if(condition) { // "then" Start End “Then” Statements True False “Otherwise” With else clause: if(condition) { // "then" // statements } else { // "otherwise" }

If-Statements With else if clause: if(cond1) { // "then" // statements Start End “Then” Statements True False “Otherwise if” Cond2? With else if clause: if(cond1) { // "then" // statements } else if(cond2) { // "otherwise if" }

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: if(cond1) { if(cond2) { // statement1 } else { // statement2 } if(cond1 && cond2) { // statement1 } else if(cond1) { // statement2 }

Practice Question Which value of x will get the following code to print out "Maybe"? 1 3 5 7 We’re lost… Think for a minute, then discuss with your neighbor(s) Vote at http://PollEv.com/justinh if (x == 5) { print("Yes"); } else if ((x >= 6) || (x < 2)) { print("No"); } else { print("Maybe"); }

Conditionals Worksheet Work on Page 2 (Questions 7-9) if(cond1) { // "then" } else if(cond2) { // "otherwise if" } else { // "else" } Cond1? Start End “Then” Statements True False “Otherwise if” Cond2? “Else”

Processing Demo: Drawing Dots

Jumping Monster Using expressions and conditionals in conjunction with variables and user input (Wed) to control what is drawn as well as motion: