Is the Notes Wrong? Prefix vs Postfix Increment in Programming

  • Thread starter Thread starter chetzread
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
8 replies · 2K views
chetzread
Messages
798
Reaction score
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: 568
Physics news on Phys.org
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   Reactions: chetzread
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: 526
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   Reactions: chetzread
why the author doesn't establish a new variable for the incremented value ? By using x , it's confusing ...
 
no, it's the notes... It's stated on the top of photo in 396 , it's post-fix ... Is the notes wrong ?
 
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.
 
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.
 
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.