Jump to content

increment and decrement operators

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
I have a small doubt about my knwoledge on this small topics

++a

a++

for example these two
let say we say a represents a counter
the original code should be gradeCounter = gradeCounter + 1;
everyone understand this...
instead, we use the increment operator here
which one should i use?

i am confuse at the actual explanation in the book

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
++a is increment before `a' is evaluated, a++ is increment after `a' is evaluated. Meaning if `a' = 10 and you print out ++a you will see 11 since `a' was incremented before it was printed (evaluated). However if `a' = 10 and you print a++ you will still see 10 since `a' was first printed and then it was incremented. As for the original code you provided, it depends where it was placed (before gradeCounter is evaulated or after?). This is an issue that arises so often in computer science that we refer to it is "the off by one error."

#3
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
for the original code, it's within a loop
for example
while ( grade != -1 ) {
 total = total + grade;
 gradeCounter = gradeCounter + 1
.... skip the rest
}

in the book, just like what you said above

Quote

++a = increment a by 1, then use the new value of a in the expression in which a resides
a++ = use the current value of a in the expression in which a resides, then increment a by 1

since the loop is going to check the value, if the value is -1, then the loop stops, otherwise, the counter will increase by 1 each time a new value enters.
and if this is the case, in another word, gradeCounter is postincrement (a++)? am i correct?

but when i looked over the book, different examples shows different increment operator, can be a++, ++a, so i bring up here and ask for discussion :) thanks


for the original code, the whole program is quoted below
// This is a C++ program
// Welcome to another program written by me John Wong
// Okay, let's go to the program then

#include <iostream>
#include <iomanip>
using namespace std;

int main ()
    {
         // declare and define variables
         int grade, total, gradeCounter;
         float average;
         
         // initialize sets values
         total = 0;
         gradeCounter = 0;
         
         // print some messages first
         cout << "Welcome to simple average program!\n" << endl;
         cout << "Please enter each grade values and to terminate the program to print the result, please type -1 into the grade value.\n" << endl;
         
         // now, let us begin our program
         cin.get();
         cout << "Enter a grade, -1 to end: ";
         cin >> grade;
         cin.get();
         
         while ( grade != -1 ) {
               total = total + grade;
               gradeCounter = gradeCounter + 1;
               cout << "Enter a grade, -1 to end: ";
               cin >> grade;
         }
         cin.get();
         
         // terminate the program
         if ( gradeCounter != 0 ) {
              average = static_cast< float > (total) / gradeCounter;
              cout << "Average is " << setprecision ( 2 ) << setiosflags( ios::fixed | ios::showpoint ) << average << endl;
         }
         else
             cout << "No grade were entered" << endl;
        cin.get();
         // end the entire program
         return 0;
               
    }


#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
   while ( grade != -1 ) {
      total = total + grade;
      gradeCounter++;
      cout << "Enter a grade, -1 to end: ";
      cin >> grade;
   }
will produce the same results as
   while ( grade != -1 ) {
      total = total + grade;
      cout << "Enter a grade, -1 to end: ";
      cin >> grade;
      ++gradeCounter;
   }

A lot of people argue against the use of the increment/decrement operator is it tends to diminish the readability of the code. For example:
   int i = 0;    
   i =+ i++ +i++ -i-- *i;


#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
As a standalone statement, there is no practical difference between a++ and ++a (other than possible slight performance differences in the implementation). The difference appears when used as part of a larger statement.
int a,b,c;
a=10;
b=a++; // a++ increments a, but returns the original value.  b==10, a==11;
c=++b; // ++b increments b and returns the new value. c==11, b==11;

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
MerakSpielman

MerakSpielman

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
I always just over-use parentheses, just so I know for sure what's happening where. I don't like to get tripped up by the obscure bits in the order of operations.

#7
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
I can understand the examples above, but i still don't see the point you mentioned, panther.

Quote

The difference appears when used as part of a larger statement.
maybe i don't have enough real programming experience, or most of the time i tend to be a reader only? hhahaha
Hmm, how would you explain that quote you said?

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Basically, with the example I was trying to show how ++ as pre- or post-fix increments the variable the same, but behaves differently when used as part of a larger assignment statement. One of the odd things about C++ is that EVERY statement has a return value. Normally, you think of something like "a=10" just storing 10 in a, but it also has an overall result of 10, which can be assigned to something else. As a result, this is valid: "a=b=c=10" assigns 10 to all three variables and is equivalent to "a=(b=(c=10))".
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
XJamesX

XJamesX

    Newbie

  • Members
  • PipPip
  • 17 posts
If you just want to increase the value of a then ++a;
If you want to increase the value of a but use a's original value before its incremented then a++;

But in areas "where it doesn't matter" say like a for loop

for(a = 0; a < 10; ++a)
;
or
for(a = 0; a < 10; a++)
;
Most people say either is ok, but the first is more efficient.
The program will see the ++ first and increment the next variable;
With a++ it has to do more work cause it hits the variable first and when it hits the ++ it has to go back and get the variable and increment it after whatever process was supposed to of used the value before being incremented.(or this is how I understood it)

In places were it doesnt matter ++a is prefered but not required.

and the only time you need a++ is when you need to do something with a before incremented.

a = 0;
c = 10 + ++a;
c would equal 11
a would equal 1

a = 0;
c = 10 + a++;
c would equal 10
a would equal 1

(all the rules apply for the -- as they do with the ++)

#10
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
Thanks :) I get it the point now