SOW_C++_CSO_Chapter_05_9e-1.ppt
Document Details
Uploaded by SelfSatisfactionRing
Full Transcript
STARTING OUT WITH C++ 9th Edition Chapter 5 Loops and Files Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++; is the same as val = val + 1; ++ can be used before (prefix) or a...
STARTING OUT WITH C++ 9th Edition Chapter 5 Loops and Files Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++; is the same as val = val + 1; ++ can be used before (prefix) or after (postfix) a variable: ++val; val++; Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved The Increment and Decrement Operators -- is the decrement operator. It subtracts one from a variable. val--; is the same as val = val - 1; -- can be also used before (prefix) or after (postfix) a variable: --val; val--; Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved Increment and Decrement Operators in Program 5-1 Continued… Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved Increment and Decrement Operators in Program 5-1 Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved Prefix vs. Postfix ++ and -- operators can be used in complex statements and expressions In prefix mode (++val, --val) the operator increments or decrements, then returns the value of the variable In postfix mode (val++, val--) the operator returns the value of the variable, then increments or decrements Copyright © 2019, 2016, 2012 Pearson Education, Inc. All Rights Reserved Prefix vs. Postfix - Examples int num, val = 12; cout