Augmented assignment operators Taken from notes by Dr. Neil Moore

Slides:



Advertisements
Similar presentations
Lilian Blot CORE ELEMENTS PART I Python Language Autumn 2013 TPOP 1.
Advertisements

Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
Identifiers and Assignment Statements. Data structures In any programming language you need to refer to data The simplest way is with the actual data.
Operators and Expressions
LESSON 6 – Arithmetic Operators
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
© 2006 Pearson Education 1 Obj: to use assignment operators HW: Prelab Exercises 2 Quiz 3.1 – 3.4 in two class days  Do Now: 1.Read p.144 – 145 (3.4 “more.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
CS 115 Lecture 9 Augmented assignment; Boolean logic; random numbers Taken from notes by Dr. Neil Moore.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Windows Programming Lecture 03. Pointers and Arrays.
CS 115 Lecture 8 Conditionals, if statements Taken from notes by Dr. Neil Moore.
CS 115 Lecture Augmented assignment; Boolean logic; random numbers
CSE 220 – C Programming Expressions.
Expressions.
CS 115 Lecture Conditionals and if statements
Lecture 4: Expressions and Variables
INSPIRING CREATIVE AND INNOVATIVE MINDS
Introduction to Python and programming
Assignment statement:
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Arithmetic operations & assignment statement
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Introduction to Algebra
CMSC201 Computer Science I for Majors Lecture 03 – Operators
CS 115 A First Look at Python
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Introduction to Python and programming
Introduction to Programming
Introduction to Python and programming
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
With Assignment Operator
Lecture 3: Expressions and Variables
Arithmetic Expressions & Data Conversions
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
CS 115 Lecture 7 Augmented assignment; Boolean logic; random numbers
Introduction to Python and programming
Expressions and Assignment
Core Objects, Variables, Input, and Output
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
SSEA Computer Science: Track A
Lecture 4: Expressions and Variables
Chapter 8 Namespaces and Memory Realities
Taken from notes by Dr. Neil Moore and Dr. Debby Keen
OPERATORS in C Programming
is multiplied by a variable of type to yield a result of type
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Introduction to Python and programming
OPERATORS in C Programming
Arithmetic Expressions & Data Conversions
Introduction to Python
Getting Started in Python
Presentation transcript:

Augmented assignment operators Taken from notes by Dr. Neil Moore CS 115 Lecture Augmented assignment operators Taken from notes by Dr. Neil Moore

Augmented assignment Often you want to perform an operation on a variable and store the result in the same variable: num_students = num_students + 1 price = price * 0.9 # 10 percent discount change = change % 25 # change after quarters Python provides a shorthand for this, augmented assignment operators: num_students += 1 price *= 0.9 change %= 25

Augmented assignment Combines assignment with an arithmetic operator The precedence is the same as assignment (=) Evaluate the right hand side first What does this do? product *= i + 1 does NOT do: product = product * i + 1 DOES do: product = product * (i + 1) because + is higher precedence than *= Sometimes called “compound operators” in other languages

Examples: x has value 7 before the statement x += 5 executes, x has value 12 afterward (7 + 5 = 12) y has value 10 before, y /= 5 executes, y is now 2.0 (float because / gives a float always) t has value 12 before, t %= 5 executes, t becomes 2 (12 % 5 = 2) z has value 3 before, p has the value 12 before, z *= p – 5 executes, p doesn’t change, z becomes 21 (p-5 is 12-5 is 7, 3 * 7 = 21)