Lesson 7: Autoincrement and Autodecrement Operators in C++

Discussion
1. C++ uses autoincrement and autodecrement operators, such that if we have a variable count,
count++; would add 1 to count in the same way that the assignment statement count = count + 1;
does, and count-- would subtract 1 from count, in the same way that count = count -1; does.
2. Recall that count = count + 1 may also be expressed as count += 1.
3. Recall that count = count - 1 may also be expressed as count -= 1.
4. In C++, we are allowed to express count +=1 as count++.
5. In C++, we are allowed to express count -=1 as count--.
6. Looks simple enough. However, there is another expression that looks very similar, and that can
add or subtract 1 to a variable: ++count and --count.
7.Needless to say, we can only do this to integer variables, and not only to the variable count, but to
other variables as well, so we can have vote++, --candy, and the like.
8. Moreover, we can only apply the increment and decrement operators to single variables.
Expressions like (count+i)++ , or -–(x+y) are not allowed in C++.
9. When the operator is placed before the variable, say, ++count, the operator ++ is in prefix form;
putting it as count++ renders it in postfix form.
10. What's the difference? Let's consider putting the expression in an assignment statement. Recall
that the expression, say count++, is actually an assignment statement in itself, so that you we shall
be doing more than one assignment in the statements below:
m= 1;
j = 2 * m++;
11. In this postfix form, the following things happen:
    1. The old/current value of m, which is 1, is multiplied by 2
    2. The old/current value of m is incremented by 1 (which means that new value of 2 is
        assigned to m)
    3. The result of multiplication is stored or assigned to j.
        Take note that there were two assignment operations done: one on m andthe other on j.
12. If we have the following statements instead:
m= 1;
j = 2 * ++m;
the following steps would be executed:
   1. The old/current value of m which is 1 is incremented, making it 2 (The first assignment)
   2. This new value of m is multiplied by 2
   3. The product of the multiplication is stored or assigned to j.(Second assignment)
Sample Programs:
    1. incDecPost.cpp
    2. incDecPre.cpp
Exercise:
    1. Try the sample program incDec.cpp given in incDecSourceFile.pdf and study the operations
       done by the prefix and postfix increment and decrement operators.
    2. What happens if you remove line#26? Explain the output when this line is removed.
       Submit your explanation in pdf format with filename incDecfLastname.
    3. Create a program that will output two columns, with headings “Text”, and “Numeral”, for
       numbers 1 through 5. File name is tabLastname.cpp. Sample output is shown below.
                 Text                               Numeral
                 One                                1
                 Two                                2
                   :                                 :

TSPaccarangan Increment and Decrement Operators September 14, 2009

More Related Content

PDF
This was the chapter 11 assignment 1(instructions are below this assignment) ...
PDF
TD2-JS-functions
PDF
This was the chapter 11 assignment 1(instructions are below this assignment) ...
DOCX
Task python
PPTX
Functions in c++,
PDF
Consider the following C program: int fun(int *i) { *i += 5; return 4; } void...
DOCX
Cosc 2425 project 2 part 1 implement the following c++ code
PPTX
Lecture 1 mte 407
This was the chapter 11 assignment 1(instructions are below this assignment) ...
TD2-JS-functions
This was the chapter 11 assignment 1(instructions are below this assignment) ...
Task python
Functions in c++,
Consider the following C program: int fun(int *i) { *i += 5; return 4; } void...
Cosc 2425 project 2 part 1 implement the following c++ code
Lecture 1 mte 407

What's hot (14)

PPTX
Lecture 1 mte 407
DOCX
Implementation of absolute loader
DOC
PDF
Handout # 3 functions c++
PDF
Optimization in the world of 64-bit errors
PPTX
Introduction of calculus in programming
PPT
types
PPTX
C,C++ In Matlab
PPT
lists
PPTX
Debugging Python with Pdb!
PDF
Pointers c5
PPTX
Python recursion
PPTX
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
PDF
Assignment 1 for 2nd sem
Lecture 1 mte 407
Implementation of absolute loader
Handout # 3 functions c++
Optimization in the world of 64-bit errors
Introduction of calculus in programming
types
C,C++ In Matlab
lists
Debugging Python with Pdb!
Pointers c5
Python recursion
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
Assignment 1 for 2nd sem
Ad

Viewers also liked (20)

ODP
Les forneres
PPTX
Victoria martin 2
PPTX
Web 2.0
PPTX
Stugatesfeliqs
PPT
Programamos ebe13
PPTX
Power point mundial estadisticas
DOC
Camila y valentina partes de la compu
PDF
үнэт цаас
PPTX
Guía de investigación nº 1, power point
PPTX
Evaluation Question 2
PPT
Poppy Paintings
PPTX
Presentación 1
PPTX
Guía de investigación nº 1
PPTX
Animaciones brisa 250 diapositivas
PPTX
Աֆրիկա
PPTX
ODP
Aula d'acollida
PPTX
Presentación1
PPTX
Guía de ivestigacion n· 1 luisina
DOCX
Valentin days article i
Les forneres
Victoria martin 2
Web 2.0
Stugatesfeliqs
Programamos ebe13
Power point mundial estadisticas
Camila y valentina partes de la compu
үнэт цаас
Guía de investigación nº 1, power point
Evaluation Question 2
Poppy Paintings
Presentación 1
Guía de investigación nº 1
Animaciones brisa 250 diapositivas
Աֆրիկա
Aula d'acollida
Presentación1
Guía de ivestigacion n· 1 luisina
Valentin days article i
Ad

Similar to Lesson7 incdecoperators (20)

PDF
Oop with c++ notes unit 01 introduction
PDF
A01
PPT
Create and analyse programs
PDF
pyton Notes9
PDF
A MATLAB project on LCR circuits
PDF
Dsp manual completed2
PPTX
Get Fast C++ Homework Help
DOCX
Although people may be very accustomed to reading and understanding .docx
PDF
Java conceptual learning material
DOCX
C++ Loops General Discussion of Loops A loop is a.docx
PPTX
Operator overloading
PPT
Aad introduction
DOCX
Functions in c++
PDF
PDF
Algorithm and c language
PDF
Computer experiments 1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
PDF
Maxbox starter
PDF
complexity analysis.pdf
PPS
basics of C and c++ by eteaching
PDF
maXbox Blix the Programmer
Oop with c++ notes unit 01 introduction
A01
Create and analyse programs
pyton Notes9
A MATLAB project on LCR circuits
Dsp manual completed2
Get Fast C++ Homework Help
Although people may be very accustomed to reading and understanding .docx
Java conceptual learning material
C++ Loops General Discussion of Loops A loop is a.docx
Operator overloading
Aad introduction
Functions in c++
Algorithm and c language
Computer experiments 1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
Maxbox starter
complexity analysis.pdf
basics of C and c++ by eteaching
maXbox Blix the Programmer

Lesson7 incdecoperators

  • 1. Lesson 7: Autoincrement and Autodecrement Operators in C++ Discussion 1. C++ uses autoincrement and autodecrement operators, such that if we have a variable count, count++; would add 1 to count in the same way that the assignment statement count = count + 1; does, and count-- would subtract 1 from count, in the same way that count = count -1; does. 2. Recall that count = count + 1 may also be expressed as count += 1. 3. Recall that count = count - 1 may also be expressed as count -= 1. 4. In C++, we are allowed to express count +=1 as count++. 5. In C++, we are allowed to express count -=1 as count--. 6. Looks simple enough. However, there is another expression that looks very similar, and that can add or subtract 1 to a variable: ++count and --count. 7.Needless to say, we can only do this to integer variables, and not only to the variable count, but to other variables as well, so we can have vote++, --candy, and the like. 8. Moreover, we can only apply the increment and decrement operators to single variables. Expressions like (count+i)++ , or -–(x+y) are not allowed in C++. 9. When the operator is placed before the variable, say, ++count, the operator ++ is in prefix form; putting it as count++ renders it in postfix form. 10. What's the difference? Let's consider putting the expression in an assignment statement. Recall that the expression, say count++, is actually an assignment statement in itself, so that you we shall be doing more than one assignment in the statements below: m= 1; j = 2 * m++; 11. In this postfix form, the following things happen: 1. The old/current value of m, which is 1, is multiplied by 2 2. The old/current value of m is incremented by 1 (which means that new value of 2 is assigned to m) 3. The result of multiplication is stored or assigned to j. Take note that there were two assignment operations done: one on m andthe other on j. 12. If we have the following statements instead: m= 1; j = 2 * ++m; the following steps would be executed: 1. The old/current value of m which is 1 is incremented, making it 2 (The first assignment) 2. This new value of m is multiplied by 2 3. The product of the multiplication is stored or assigned to j.(Second assignment) Sample Programs: 1. incDecPost.cpp 2. incDecPre.cpp Exercise: 1. Try the sample program incDec.cpp given in incDecSourceFile.pdf and study the operations done by the prefix and postfix increment and decrement operators. 2. What happens if you remove line#26? Explain the output when this line is removed. Submit your explanation in pdf format with filename incDecfLastname. 3. Create a program that will output two columns, with headings “Text”, and “Numeral”, for numbers 1 through 5. File name is tabLastname.cpp. Sample output is shown below. Text Numeral One 1 Two 2 : : TSPaccarangan Increment and Decrement Operators September 14, 2009