Is the Notes Wrong? Prefix vs Postfix Increment in Programming

  • Thread starter Thread starter chetzread
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the correctness of notes related to prefix and postfix increment operators in programming, specifically in the context of a computer science homework example. Participants are examining the implications of these operators on variable assignments.

Discussion Character

  • Homework-related
  • Debate/contested
  • Technical explanation

Main Points Raised

  • Some participants believe the example in the notes is incorrect, specifically regarding the behavior of the postfix increment operator.
  • One participant asserts that the value assigned to the variable 'ans' should be 11, as they interpret the operation as a post-increment.
  • Another participant clarifies that in the provided code, the value assigned to 'ans' is actually 10, as the variable is evaluated before the increment occurs.
  • A question is raised about why the result of a postfix increment is represented as 'x = a' instead of 'x = a + 1'.
  • There is a suggestion that the author should have used a new variable for the incremented value to avoid confusion.
  • Some participants emphasize the importance of understanding both prefix and postfix increments for effective coding.
  • One participant claims that the notes are incorrect, stating that the title should refer to pre-fix increment instead of post-fix increment.

Areas of Agreement / Disagreement

Participants express disagreement regarding the correctness of the notes, with some asserting that they are wrong while others provide explanations that support the existing notes. The discussion remains unresolved as multiple competing views are presented.

Contextual Notes

There are unresolved questions regarding the definitions and implications of prefix versus postfix increments, as well as the clarity of the example provided in the notes.

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: 548
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: 513
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.
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K