-2

I was writing a small piece of code wherein I had to increment value of a variable 'j' if the condition matched.

for(int i=0;i<input.length();i++){//input and base are of type string
        for(int j=0;j<base.length();j++){
            if(input[i]==base[j])input[i]=base[j+1];
        }
    }
cout<<input;

When I executed the code, for every input I got a blank string as an output. But if I used ++j instead of j+1, the code seems to work fine. What am I missing here ? Also, the code for j-1 as well as --j works fine. The only problem I face is with j+1. Is it something related to precedence of operators ?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Backspace
  • 292
  • 1
  • 4
  • 17
  • 4
    j++ and j+= 1; are the same thing. j+1 does not increment j but adds one and then throws away the sum so j never gets incremented. – Richard Chambers Dec 17 '14 at 05:00
  • Well, if it was just `j+1` no wonder it didnt work. It should be `j=j+1` – Eugene Sh. Dec 17 '14 at 05:02
  • If j+1 didnt work then how j-1 did ? – Backspace Dec 17 '14 at 05:05
  • 2
    @agr_vin Your code invokes undefined behavior (out-of-bounds access) on the last iteration if the last items are equal. So given that, your code doesn't "work" with `j+1`. So your observations are due to an incorrectly written loop using `j+1`, not because of `++j` or any other variation. – PaulMcKenzie Dec 17 '14 at 05:15
  • @PaulMcKenzie Okay I will try to correct my loop's boundary conditions. Thanks for the help – Backspace Dec 17 '14 at 05:22

3 Answers3

2

There are two diferences related to these expressions: what will be the value of the expression and whether a will be changed. For expression

a + 1

its value is the sum of operands a and 1 and a itself will not be changed.

For expression

a++

its value is the value of a before the increment and a will be changed. For example if a is defined the following way

int a = 0;

and there is another variable b defined like

int b;

then after statement

b = a++

b will be equal to 0 and a will be equal to 1. while after statement

b = a + 1;

b will be equal 1 and a will not be changed that is it will be equal as before to 0.

If you mean expression

a += 1;

then if fact it has the same result as expression a + 1 except that a will be changed.

Take into account that a++ is unsequenced relative to other subexpressions in an expression.

Expression

a += 1;

is similar to expression

++a;

So the difference between a++ and a += 1 is the same as between a++ and ++a. Also take into account that the result of a++ is rvalue (you may not assign any value to the expression) while the result of a += 1 is lvalue (you may assign a value to the expression like ( a += 1 ) += 1;)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-1
#include <iostream>
using namespace std;
int main ()
{
    int a=10,b=10;
    cout<<"a++ is \n "<<a++;
    cout<<"\n++b is\n "<<++b;
}

Output:

"a++ is
    10
"++b is   
    11
Dartmouth
  • 1,069
  • 2
  • 15
  • 22
IREX
  • 1
-2

j++; is j = j + 1. It is post increment operator. Value of j will increase after other operation of expression completes.

j+1, is you are adding one to value of j, but it will not affect j's value.

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137