Thread: increment/decrement questions. I want to clarify this.

  1. #1
    Registered User
    Join Date
    Mar 2018
    Posts
    8

    increment/decrement questions. I want to clarify this.

    What would be a = b-- + c . given b=3 c=7? Would the output be the same as a= --b + c? I wanted to make sure if I have this concept correctly.
    a would always remain the same regardless of what b is right?

    another question is

    to rewrite the pair given statements into a single statement using the increment and/or decrement operator.

    i=i+1;
    h=j+2*i;
    my attempt
    h= j+2*i++ ??

    i=j/3-k;
    k=k-1;
    my attempt
    I=j/3-k- -; ??

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted and answered here.

  3. #3
    Registered User
    Join Date
    Mar 2018
    Posts
    8
    yes
    but a=b-- + c
    a=--b +c

    but a= b-1 + c. so a is 3 b=2 and c is 7
    in this case, since a is not given a value, the answer is 9?

    a=--b + c woud be a=2 b=2 c=7 the answer still remains the same, 9 ?


    what am I doing wrong here?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nedlig
    but a= b-1 + c. so a is 3 b=2 and c is 7
    in this case, since a is not given a value, the answer is 9?
    It sounds like you have a fundamental misunderstanding of what a code snippet like this means:
    Code:
    b = 3;
    c = 7;
    a = b + c;
    In the above snippet, the last statement can be equivalently written like this:
    Code:
    a = (b + c);
    That is, the precedence of the = operator is lower than that of the + operator, hence the grouping of subexpressions is equivalent to the above rather than to:
    Code:
    (a = b) + c;
    Consequently, given this:
    Code:
    a = b-- + c;
    it doesn't make sense to say that "a is 3" because b was equal to 3, because you can only say what a is after evaluating (b-- + c), as it is the result of that subexpression that is assigned as the value of a.

    Hence, in post #2 of that other thread sepp2k observed that the answer is 10, since post-decrement was used to decrement b. Furthermore, it is clearly wrong to say that "a is not given a value", because indeed a was given a value, and that value is the result of (b-- + c).

    Quote Originally Posted by nedlig
    i=i+1;
    h=j+2*i;
    my attempt
    h= j+2*i++ ??
    This is wrong. You need to pay attention to the difference between pre-increment and post-increment. Also, you shouldn't be asking if your attempt is correct when you can verify that it is wrong by testing it. You should only ask if it is correct if after testing, you have found that it passes your tests, but you are not sure if you missed anything.
    Last edited by laserlight; 03-10-2018 at 06:26 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2018
    Posts
    8
    I'm just learning it via available source online. I'm not taking any class. the second question is a question I got from a friend who is taking a class and I wanted to solve it as well.
    So I'm not as well knowledgeable regarding c++ language. Just wanting to learn.

    Now, my understanding is that prefix would be subtracting the value of x by one then put it into y.
    so if x=2 y=--x then x would be 1 y would also be 1.

    Whereas postfix, it assigns the value of x to y then subracts 1 to x.
    so if x=2 y=x-- then x would be 1 and y would be 2.

    this is my understanding. Am I still getting this concept wrong?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, that is correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2018
    Posts
    8
    so then I'm not understanding why a would equal to 10 if a = b-- +c; given b=3 c=7. The output would be 9.
    ok. let's take out +c and solve a=b-- so that would be a=3 and b=2. so then we will add int c here. that would be a=2+7 which would equal 9. what happens to the output of a (3) ?

    Thank you for your help btw

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nedlig
    so then I'm not understanding why a would equal to 10 if a = b-- +c; given b=3 c=7. The output would be 9.
    Because of the post-decrement, this:
    Code:
    a = b-- + c;
    has the same result as:
    Code:
    a = b + c;
    b = b - 1;
    Hence, mathematically: a = 3 + 7 = 10, and b = 3 - 1 = 2.

    Quote Originally Posted by nedlig
    ok. let's take out +c and solve a=b-- so that would be a=3 and b=2. so then we will add int c here. that would be a=2+7 which would equal 9. what happens to the output of a (3) ?
    Refer to my post #4: you cannot "take out +c". At no point does the value of a become 3. The value of b does indeed become 2, but that happens afterwards (post-decrement). Hence, your claim that "a=3" is absurd, and your attempt to "then we will add int c here" is wrong, hence your statement that "a=2+7" is incorrect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help in increment and decrement operator
    By san12345 in forum C Programming
    Replies: 4
    Last Post: 10-29-2015, 04:23 AM
  2. LCD Increment and Decrement For Display
    By AJITnayak in forum C Programming
    Replies: 13
    Last Post: 02-27-2014, 10:22 PM
  3. pre/post increment/decrement
    By riemann in forum C Programming
    Replies: 7
    Last Post: 09-18-2013, 02:40 PM
  4. Increment and Decrement
    By Johnathan1707 in forum C Programming
    Replies: 4
    Last Post: 07-25-2009, 02:20 PM
  5. increment and decrement operator
    By jaipandya in forum C Programming
    Replies: 5
    Last Post: 10-20-2004, 06:54 AM

Tags for this Thread