Assignment Operators Topics Increment and Decrement Operators

Slides:



Advertisements
Similar presentations
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Advertisements

C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
CS1061 C Programming Lecture 6: Operators and Expressions A. O’Riordan, 2004.
CS150 Introduction to Computer Science 1
BBS Yapısal Programlama (Structured Programming)1 Lexical Elements Kinds of tokens in C : Keywords Identifiers Constants Operators Punctuators.
Introduction to Computers and Programming Class 9 Introduction to C Professor Avi Rosenfeld.
1 Shortcuts for Lazy Programmers! Topics Increment and Decrement Operators Assignment Operators.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Basic Elements of C++ Chapter 2.
Expressions, Data Conversion, and Input
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
LESSON 6 – Arithmetic Operators
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
IT 152 Data Structures and Algorithms Tonga Institute of Higher Education.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
ITM © Port, KazmanVariables - 1 ITM 352 Expressions, Precedence, Working with Strings.
Data Types and Operators  Two category of data types:  Primitive type: value type. Store values.  Object type: reference type. Store addresses.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
© 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.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
Recap……Last Time [Variables, Data Types and Constants]
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Repetition statements
UMBC CMSC 104 – Section 01, Fall 2016
UMBC CMSC 104 – Section 01, Fall 2016
Lecture 3 Java Operators.
Lecture 3: Operators, Expressions and Type Conversion
Lecture 7: Repeating a Known Number of Times
Chapter 2 Assignment and Interactive Input
Assignment and Arithmetic expressions
ITM 352 Expressions, Precedence, Working with Strings Class #5
Computing with C# and the .NET Framework
Arithmetic Operator Operation Example + addition x + y
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Lecture 3 Expressions Richard Gesick.
Numerical Data Types.
With Assignment Operator
3-4: Arithmetic Sequences
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
elementary programming
Data Types and Expressions
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Presentation transcript:

Assignment Operators Topics Increment and Decrement Operators Debugging Tips Reading Sections 3.11 - 3.12

Increment and Decrement Operators The increment operator ++ The decrement operator -- Precedence: lower than (), but higher than * / and % Associativity: right to left Increment and decrement operators can only be applied to variables, not to constants or expressions

Increment Operator If we want to add one to a variable, we can say: count = count + 1 ; Programs often contain statements that increment variables, so to save on typing, C provides these shortcuts: count++ ; OR ++count ; Both do the same thing. They change the value of count by adding one to it.

Postincrement Operator The position of the ++ determines when the value is incremented. If the ++ is after the variable, then the incrementing is done last (a postincrement). int amount, count ; count = 3 ; amount = 2 * count++ ; amount gets the value of 2 * 3, which is 6, and then 1 gets added to count. So, after executing the last line, amount is 6 and count is 4.

Preincrement Operator If the ++ is before the variable, then the incrementing is done first (a preincrement). int amount, count ; count = 3 ; amount = 2 * ++count ; 1 gets added to count first, then amount gets the value of 2 * 4, which is 8. So, after executing the last line, amount is 8 and count is 4.

Code Example Using ++ /* count from 1 to 10 */ #include <stdio.h> int main ( ) { int i = 1 ; /* count from 1 to 10 */ while ( i < 11 ) printf (“%d ”, i) ; i++ ; /* same as ++i */ } return 0 ;

Decrement Operator If we want to subtract one from a variable, we can say: count = count - 1 ; Programs often contain statements that decrement variables, so to save on typing, C provides these shortcuts: count-- ; OR --count ; Both do the same thing. They change the value of count by subtracting one from it.

Postdecrement Operator The position of the -- determines when the value is decremented. If the -- is after the variable, then the decrementing is done last (a postdecrement). int amount, count ; count = 3 ; amount = 2 * count-- ; amount gets the value of 2 * 3, which is 6, and then 1 gets subtracted from count. So, after executing the last line, amount is 6 and count is 2.

Predecrement Operator If the -- is before the variable, then the decrementing is done first (a predecrement). int amount, count ; count = 3 ; amount = 2 * --count ; 1 gets subtracted from count first, then amount gets the value of 2 * 2, which is 4. So, after executing the last line, amount is 4 and count is 2.

A Hand Trace Example int answer, value = 4 ; Code Value Answer 4 garbage value = value + 1 ; value++ ; ++value ; answer = 2 * value++ ; answer = ++value / 2 ; value-- ; --value ; answer = --value * 2 ; answer = value-- / 3 ;

Practice Given int a = 1, b = 2, c = 3 ; What is the value of this expression? ++a * b - c-- What are the new values of a, b, and c?

More Practice Given int a = 1, b = 2, c = 3, d = 4 ; What is the value of this expression? ++b / c + a * d++ What are the new values of a, b, c, and d?

Assignment Operators = += -= *= /= %= Statement Equivalent Statement = += -= *= /= %= Statement Equivalent Statement a = a + 2 ; a += 2 ; a = a - 3 ; a -= 3 ; a = a * 2 ; a *= 2 ; a = a / 4 ; a /= 4 ; a = a % 2 ; a %= 2 ; b = b + ( c + 2 ) ; b += c + 2 ; d = d * ( e - 5 ) ; d *= e - 5 ;

Practice with Assignment Operators int i = 1, j = 2, k = 3, m = 4 ; Expression Value i += j + k j *= k = m + 5 k -= m /= j * 2

Code Example Using /= and ++ Counting the Digits in an Integer #include <stdio.h> int main ( ) { int num, temp, digits = 0 ; temp = num = 4327 ; while ( temp > 0 ) { printf (“%d\n”, temp) ; temp /= 10 ; digits++ ; } printf (“There are %d digits in %d.\n”, digits, num) ; return 0 ;

Debugging Tips Trace your code by hand (a hand trace), keeping track of the value of each variable. Insert temporary printf() statements so you can see what your program is doing. Confirm that the correct value(s) has been read in. Check the results of arithmetic computations immediately after they are performed.