Is the Notes Wrong? Prefix vs Postfix Increment in Programming

  • Thread starter chetzread
  • Start date
In summary, the conversation discusses a coding example and the use of post-fix and pre-fix increment operators. The author mistakenly labels the example as post-fix, when it is actually pre-fix. The use of pre-fix increment allows for more compact code and can save lines. However, it is important to learn and understand both pre-fix and post-fix increment when writing code.
  • #1
chetzread
801
1

Homework Statement


i think the example is wrong ( circled part )
. I am not sure whether i posted on the correct sections or not . This is computer science homework , it should involve programming language , right ?

Homework Equations

The Attempt at a Solution


Because I think that it should be num is 10 , ans is 11 , right ? since it's post increment
 

Attachments

  • 23.PNG
    23.PNG
    100.8 KB · Views: 487
Physics news on Phys.org
  • #2
chetzread said:

Homework Statement


i think the example is wrong ( circled part )
. I am not sure whether i posted on the correct sections or not . This is computer science homework , it should involve programming language , right ?

Homework Equations

The Attempt at a Solution


Because I think that it should be num is 10 , ans is 11 , right ? since it's post increment
No.
The code shown is this:
C:
num = 10;
ans = num++;
The value assigned to ans is 10. After that, num is incremented to 11.
For a post-increment or post-decrement operator, the variable is evaluated first and used in the assignment operation.
 
  • Like
Likes chetzread
  • #3
for the result of post-fix increment , why the value of a after increment is x = a ?
shouldnt it be x = a+ 1 ?
 

Attachments

  • 396.PNG
    396.PNG
    8.8 KB · Views: 447
  • #4
You ask about post-fix, BUT your attachment in post #3 illustrates pre-fix. :oldconfused:

In your attachment, x is assigned the new value of a. This leaves x and a having equal values.
 
  • Like
Likes chetzread
  • #5
why the author doesn't establish a new variable for the incremented value ? By using x , it's confusing ...
 
  • #6
no, it's the notes... It's stated on the top of photo in 396 , it's post-fix ... Is the notes wrong ?
 
  • #7
chetzread said:
why the author doesn't establish a new variable for the incremented value ? By using x , it's confusing ...
Because it makes compact code; needs fewer variables, and saves lines.
 
  • #8
chetzread said:
no, it's the notes... It's stated on the top of photo in 396 , it's post-fix ... Is the notes wrong ?
a++ and a–– is postfix ( 'post' means after)
++a and ––a is prefix ( 'pre' means before)

On this topic you need to learn both, not just one, then use whichever is appropriate when writing your own code.

If you find it confusing then you can evade using it in your own code, but you still need to be able to understand code that others have written using this notation.
 
  • #9
chetzread said:
no, it's the notes... It's stated on the top of photo in 396 , it's post-fix ... Is the notes wrong ?
Yes, the notes are wrong. The title of the slide is "Results of Post-fix Increment" -- it should say "Results of Pre-fix Increment". The relevant assignment is x = ++a, which is a pre-fix increment.
 

FAQ: Is the Notes Wrong? Prefix vs Postfix Increment in Programming

What is the difference between prefix and postfix increment?

Prefix increment (i.e. ++x) increments the value of a variable before using it in an operation, while postfix increment (i.e. x++) uses the original value of the variable in the operation and then increments it.

Which one should I use in my code?

This depends on the specific needs of your code. Generally, prefix increment is more efficient because it avoids creating a temporary variable, but postfix increment may be more readable in certain situations. Consider the context and choose the one that makes the most sense.

Can I use prefix or postfix increment with non-numeric variables?

No, prefix and postfix increment operators can only be used with numeric variables. Attempting to use them with non-numeric variables will result in an error.

Are there any other similar operators to prefix and postfix increment?

Yes, there are also prefix and postfix decrement operators (i.e. --x and x--) which decrease the value of a variable by 1. These work in the same way as prefix and postfix increment, but in the opposite direction.

Can I use prefix or postfix increment within an expression?

Yes, prefix and postfix increment operators can be used within an expression as long as they are not the only operator. For example, you can use x = y++ * 2, but not x = ++y.

Similar threads

Replies
17
Views
1K
Replies
2
Views
1K
Replies
3
Views
1K
Replies
8
Views
2K
Replies
2
Views
2K
Replies
1
Views
2K
Replies
1
Views
4K
Back
Top