Is the Notes Wrong? Prefix vs Postfix Increment in Programming

  • Thread starter Thread starter chetzread
  • Start date Start date
Click For Summary
The discussion centers around confusion regarding the distinction between post-fix and pre-fix increment operators in programming. Participants clarify that in the example provided, the assignment of `ans` to `num++` results in `ans` being 10, while `num` becomes 11 after the increment, illustrating post-increment behavior. There is a debate about the clarity of the notes, with some asserting that they incorrectly label pre-fix increment as post-fix. The consensus is that understanding both increment types is essential for coding proficiency, despite the confusing presentation in the notes. Ultimately, it is concluded that the notes contain inaccuracies regarding the increment terminology.
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: 531
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 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: 496
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
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.
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
1K
Replies
4
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K