SlideShare a Scribd company logo
1 of 1
Download to read offline
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

What's hot

Implementation of absolute loader
Implementation of absolute loaderImplementation of absolute loader
Implementation of absolute loaderJaya Krishnan P
 
Handout # 3 functions c++
Handout # 3   functions c++Handout # 3   functions c++
Handout # 3 functions c++NUST Stuff
 
Optimization in the world of 64-bit errors
Optimization  in the world of 64-bit errorsOptimization  in the world of 64-bit errors
Optimization in the world of 64-bit errorsPVS-Studio
 
Introduction of calculus in programming
Introduction of calculus in programmingIntroduction of calculus in programming
Introduction of calculus in programmingAfaq Siddiqui
 
Debugging Python with Pdb!
Debugging Python with Pdb!Debugging Python with Pdb!
Debugging Python with Pdb!Noelle Daley
 
Python recursion
Python recursionPython recursion
Python recursionToniyaP1
 
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeksBeginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeksJinTaek Seo
 
Assignment 1 for 2nd sem
Assignment 1 for 2nd semAssignment 1 for 2nd sem
Assignment 1 for 2nd semhome
 

What's hot (14)

Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Implementation of absolute loader
Implementation of absolute loaderImplementation of absolute loader
Implementation of absolute loader
 
Loader
LoaderLoader
Loader
 
Handout # 3 functions c++
Handout # 3   functions c++Handout # 3   functions c++
Handout # 3 functions c++
 
Optimization in the world of 64-bit errors
Optimization  in the world of 64-bit errorsOptimization  in the world of 64-bit errors
Optimization in the world of 64-bit errors
 
Introduction of calculus in programming
Introduction of calculus in programmingIntroduction of calculus in programming
Introduction of calculus in programming
 
types
typestypes
types
 
C,C++ In Matlab
C,C++ In MatlabC,C++ In Matlab
C,C++ In Matlab
 
lists
listslists
lists
 
Debugging Python with Pdb!
Debugging Python with Pdb!Debugging Python with Pdb!
Debugging Python with Pdb!
 
Pointers c5
Pointers c5Pointers c5
Pointers c5
 
Python recursion
Python recursionPython recursion
Python recursion
 
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeksBeginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
Beginning direct3d gameprogrammingmath02_logarithm_20160324_jintaeks
 
Assignment 1 for 2nd sem
Assignment 1 for 2nd semAssignment 1 for 2nd sem
Assignment 1 for 2nd sem
 

Viewers also liked

Power point mundial estadisticas
Power point mundial estadisticasPower point mundial estadisticas
Power point mundial estadisticasLgiozza
 
Camila y valentina partes de la compu
Camila y valentina partes de la compuCamila y valentina partes de la compu
Camila y valentina partes de la compuvaleeen
 
Guía de investigación nº 1, power point
Guía de investigación nº  1, power pointGuía de investigación nº  1, power point
Guía de investigación nº 1, power pointmicaydina
 
Presentación 1
Presentación 1Presentación 1
Presentación 1danaaleon
 
Guía de investigación nº 1
Guía de investigación nº  1Guía de investigación nº  1
Guía de investigación nº 1micaydina
 
Animaciones brisa 250 diapositivas
Animaciones brisa 250 diapositivasAnimaciones brisa 250 diapositivas
Animaciones brisa 250 diapositivasBrisa Neto
 
Aula d'acollida
Aula d'acollida Aula d'acollida
Aula d'acollida 191219977
 
Presentación1
Presentación1Presentación1
Presentación1danaaleon
 
Guía de ivestigacion n· 1 luisina
Guía de ivestigacion n· 1 luisinaGuía de ivestigacion n· 1 luisina
Guía de ivestigacion n· 1 luisinaluisiruggieri
 

Viewers also liked (20)

Les forneres
Les forneresLes forneres
Les forneres
 
Victoria martin 2
Victoria martin 2Victoria martin 2
Victoria martin 2
 
Web 2.0
Web 2.0Web 2.0
Web 2.0
 
Stugatesfeliqs
StugatesfeliqsStugatesfeliqs
Stugatesfeliqs
 
Programamos ebe13
Programamos ebe13Programamos ebe13
Programamos ebe13
 
Power point mundial estadisticas
Power point mundial estadisticasPower point mundial estadisticas
Power point mundial estadisticas
 
Camila y valentina partes de la compu
Camila y valentina partes de la compuCamila y valentina partes de la compu
Camila y valentina partes de la compu
 
үнэт цаас
үнэт цаасүнэт цаас
үнэт цаас
 
Guía de investigación nº 1, power point
Guía de investigación nº  1, power pointGuía de investigación nº  1, power point
Guía de investigación nº 1, power point
 
Evaluation Question 2
Evaluation Question 2Evaluation Question 2
Evaluation Question 2
 
Poppy Paintings
Poppy Paintings Poppy Paintings
Poppy Paintings
 
Presentación 1
Presentación 1Presentación 1
Presentación 1
 
Guía de investigación nº 1
Guía de investigación nº  1Guía de investigación nº  1
Guía de investigación nº 1
 
Animaciones brisa 250 diapositivas
Animaciones brisa 250 diapositivasAnimaciones brisa 250 diapositivas
Animaciones brisa 250 diapositivas
 
Աֆրիկա
ԱֆրիկաԱֆրիկա
Աֆրիկա
 
Green Line Linear Park
Green Line Linear ParkGreen Line Linear Park
Green Line Linear Park
 
Aula d'acollida
Aula d'acollida Aula d'acollida
Aula d'acollida
 
Presentación1
Presentación1Presentación1
Presentación1
 
Guía de ivestigacion n· 1 luisina
Guía de ivestigacion n· 1 luisinaGuía de ivestigacion n· 1 luisina
Guía de ivestigacion n· 1 luisina
 
Valentin days article i
Valentin days article iValentin days article i
Valentin days article i
 

Similar to Lesson7 incdecoperators

Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introductionAnanda Kumar HN
 
A MATLAB project on LCR circuits
A MATLAB project on LCR circuitsA MATLAB project on LCR circuits
A MATLAB project on LCR circuitssvrohith 9
 
Dsp manual completed2
Dsp manual completed2Dsp manual completed2
Dsp manual completed2bilawalali74
 
Although people may be very accustomed to reading and understanding .docx
Although people may be very accustomed to reading and understanding .docxAlthough people may be very accustomed to reading and understanding .docx
Although people may be very accustomed to reading and understanding .docxmilissaccm
 
Java conceptual learning material
Java conceptual learning materialJava conceptual learning material
Java conceptual learning materialArthyR3
 
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops  General Discussion of Loops A loop is a.docxC++ Loops  General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docxhumphrieskalyn
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingBurhan Ahmed
 
Aad introduction
Aad introductionAad introduction
Aad introductionMr SMAK
 
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c languagekamalbeydoun
 
Computer experiments 1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
Computer experiments   1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branchComputer experiments   1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
Computer experiments 1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branchSAKSHIGAWADE2
 
complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdfpasinduneshan
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteachingeteaching
 
maXbox Blix the Programmer
maXbox Blix the ProgrammermaXbox Blix the Programmer
maXbox Blix the ProgrammerMax Kleiner
 

Similar to Lesson7 incdecoperators (20)

Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introduction
 
A01
A01A01
A01
 
Create and analyse programs
Create and analyse programsCreate and analyse programs
Create and analyse programs
 
pyton Notes9
pyton Notes9pyton Notes9
pyton Notes9
 
A MATLAB project on LCR circuits
A MATLAB project on LCR circuitsA MATLAB project on LCR circuits
A MATLAB project on LCR circuits
 
Dsp manual completed2
Dsp manual completed2Dsp manual completed2
Dsp manual completed2
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
 
Although people may be very accustomed to reading and understanding .docx
Although people may be very accustomed to reading and understanding .docxAlthough people may be very accustomed to reading and understanding .docx
Although people may be very accustomed to reading and understanding .docx
 
Java conceptual learning material
Java conceptual learning materialJava conceptual learning material
Java conceptual learning material
 
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops  General Discussion of Loops A loop is a.docxC++ Loops  General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docx
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Aad introduction
Aad introductionAad introduction
Aad introduction
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Notes7
Notes7Notes7
Notes7
 
Algorithm and c language
Algorithm and c languageAlgorithm and c language
Algorithm and c language
 
Computer experiments 1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
Computer experiments   1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branchComputer experiments   1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
Computer experiments 1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
 
Maxbox starter
Maxbox starterMaxbox starter
Maxbox starter
 
complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdf
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
 
maXbox Blix the Programmer
maXbox Blix the ProgrammermaXbox Blix the Programmer
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