1
PORTB = pattern ++;
//The author explains te upper code with the code below
pattern = pattern + 1;
PORTB = pattern;

PORTB = ++ pattern; //That's what I think is right

I think this code is wrong, as the post-increment should add 1, after using it in the statement, so for the code below, there has to be an pre-increment e. But as I am not that familiar with c/c++ (only read one book), I wanted to know whether I'm messing something up, or the author is just wrong.

Edit: The Book is not about c/c++, it's about arduino.

some_user
  • 179
  • 11
  • You are right.. – Eugene Sh. May 30 '18 at 15:23
  • 1
    You are correct. The explaining code is not equivalent to the code above it. Indeed, `pattern ++;` first uses the variable (assigns it) and then increments it. – Paul Ogilvie May 30 '18 at 15:23
  • You might benefit from throwing that book away. [We have a carefully curated list of suggestions](https://stackoverflow.com/questions/388242/). – Drew Dormann May 30 '18 at 15:25
  • Ok, thanks. It's very weird, because I already discovered some other faults in this book, while it had a really good rating. – some_user May 30 '18 at 15:25
  • Don't believe the ratings... I guess. Believe the experts recommendations. – Eugene Sh. May 30 '18 at 15:27
  • @Drew Dormann One has to say, that the book isn't about c++ itsself, it's about arduino. However, I'm a bit dissapointed. – some_user May 30 '18 at 15:27
  • Explains a lot. :) – Eugene Sh. May 30 '18 at 15:27
  • @DrewDormann Just out of curiosity, do you have such a list of books in other languages, too? (german) – some_user May 30 '18 at 15:30
  • @SomeWindowsUser My lecturer (one of the best in one of the leading institutions) in computer science back few decades ago was pretty strict about it: Never learn C or C++ by any books written not in English or even translated. Perhaps it was changed since then... – Eugene Sh. May 30 '18 at 15:32
  • @EugeneSh. But I don't think there are no good books in other languages, I just think they are harder to find/pretty rare. – some_user May 30 '18 at 15:34
  • Start [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and see if there are any translations available. – rustyx May 30 '18 at 15:47
  • This illustrates why expressions with side-effects is not a good idea. – August Karlstrom May 30 '18 at 16:42
  • @AugustKarlstrom Sorry, what do you mean with "expressions with side-effects"? Do you mean that there is a problem if a book covers 2 different topics, or did I get this wrong? – some_user May 30 '18 at 20:21
  • @SomeWindowsUser To be precise, I mean sub-expressions with side-effects. An expression with a side-effect changes the state of the computation, for instance by assigning a new value to a variable. Such expressions are hard to read and can lead to undefined behavior. That's why most programming languages don't provide such constructs. – August Karlstrom May 30 '18 at 20:40
  • @AugustKarlstrom Ok, I think I get it now, thanks. – some_user May 30 '18 at 20:47

2 Answers2

6

you are right

PORTB = pattern++;

is equivalent to

PORTB = pattern;
pattern = pattern + 1;
Tyker
  • 2,971
  • 9
  • 21
2

Prefix form [--,++]a do action (increment or decrement), and then use the result

For example:

  int a[] = {0,1,2};
  int i = -1;
  std::cout<< a[++i] << ' ';
  std::cout<< a[++i] << ' ';
  std::cout<< a[++i] << std::endl;

Equivalent form

 int a[] = {0,1,2}; 
 int i = -1;
 for(int j =0; j < 3; j = j + 1) {
  i = i + 1; // action first
  std::cout<< a[i] << ' ';
 }
 std::cout<< std::endl; 

Postfix form a[++,--] use value as it is, then increment or decrement variable

For example:

  int a[] = {0,1,2};
  int i = 0;
  std::cout<< a[i++] << ' ';
  std::cout<< a[i++] << ' ';
  std::cout<< a[i] << std::endl;

Equivalent form

 int a[] = {0,1,2}; 
 int i = 0;
 for(int j =0; j < 3; j = j + 1) {
  std::cout<< a[i] << ' ';
  i = i + 1; // action after
 }
 std::cout<< std::endl; 
Victor Gubin
  • 2,782
  • 10
  • 24