Increament operator error - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Increament operator error

himanshu124himanshu124 Posts: 2Member
i started learning JAVA a few days back.
i thought of writing my first program on arithmetic operators.
but i m getting compilation error with the "++" operator.
below is my code.
[code]public class IntArithmeticOperations {
public static void main(String args[]){
System.out.println("Arithmetic Operations Sample");

int i=10;

int increamentResult=++i;//This is ok
int increamentResult1=++(i);//This is also ok
int increamentResult2=doit(++i); //This is also ok
int increamentResult3=++(++i); //This gives a compilation
error
int increamentResult4=++(i=i++); //This gives a compilation error
int increamentResult5=++(i+1); //This gives a compilation error

System.out.println("IncreamentResult is"+increamentResult);
System.out.println("IncreamentResult1 is"+increamentResult1);
System.out.println("IncreamentResult2 is"+increamentResult2);
System.out.println("IncreamentResult3 is"+increamentResult3);
System.out.println("IncreamentResult4 is"+increamentResult4);
System.out.println("IncreamentResult5 is"+increamentResult5);
}
public static int doit(int i){
return i;
}
}[/code]

Please help me understand how the "++" operator works.

Comments

  • Katherine_DeloKatherine_Delo Posts: 3Member
    public class IntArithmeticOperations {
    public static void main(String args[]){
    System.out.println("Arithmetic Operations Sample");

    int i=10; You have initialized the code which is good

    int increamentResult=++i;//This is ok
    this is saying add 1 which increments the value i to 11

    int increamentResult1=++(i);//This is also ok
    the brackets doesn't matter in this one its the same as before but you're adding 1 again which makes the value 12

    int increamentResult2=doit(++i); //This is also ok
    this is calling the doit method (which just goes down to see if there's anything in the method doit. You've only returned i so it comes back to this statement and increments it...so now its 13.

    int increamentResult3=++(++i); //This gives a compilation
    error

    this is causing an error because it wants a variable. You're not calling any methods and you can't just put ++ and (++i). make these statements in different lines
    e.g
    int increamentResult3=i++;
    int incrementResult4 = doits(++i);

    int increamentResult4=++(i=i++); //This gives a compilation error
    int increamentResult5=i++;

    //make another method

    int incrementResult5 = doits(i=i++);

    int increamentResult5=++(i+1); //This gives a compilation error

    System.out.println("IncreamentResult is"+increamentResult);
    System.out.println("IncreamentResult1 is"+increamentResult1);
    System.out.println("IncreamentResult2 is"+increamentResult2);
    System.out.println("IncreamentResult3 is"+increamentResult3);
    System.out.println("IncreamentResult4 is"+increamentResult4);
    System.out.println("IncreamentResult5 is"+increamentResult5);
    }
    public static int doit(int i){
    return i;
    }
    }
    hope i didn't confuse you too much :) i'm new to java too
  • Katherine_DeloKatherine_Delo Posts: 3Member
    public class IntArithmeticOperations {
    public static void main(String args[]){
    System.out.println("Arithmetic Operations Sample");

    int i=10; You have initialized the code which is good

    int increamentResult=++i;//This is ok
    this is saying add 1 which increments the value i to 11

    int increamentResult1=++(i);//This is also ok
    the brackets doesn't matter in this one its the same as before but you're adding 1 again which makes the value 12

    int increamentResult2=doit(++i); //This is also ok
    this is calling the doit method (which just goes down to see if there's anything in the method doit. You've only returned i so it comes back to this statement and increments it...so now its 13.

    int increamentResult3=++(++i); //This gives a compilation
    error

    this is causing an error because it wants a variable. You're not calling any methods and you can't just put ++ and (++i). make these statements in different lines
    e.g
    int increamentResult3=i++;
    int incrementResult4 = doits(++i);

    int increamentResult4=++(i=i++); //This gives a compilation error
    int increamentResult5=i++;

    //make another method

    int incrementResult5 = doits(i=i++);

    int increamentResult5=++(i+1); //This gives a compilation error

    System.out.println("IncreamentResult is"+increamentResult);
    System.out.println("IncreamentResult1 is"+increamentResult1);
    System.out.println("IncreamentResult2 is"+increamentResult2);
    System.out.println("IncreamentResult3 is"+increamentResult3);
    System.out.println("IncreamentResult4 is"+increamentResult4);
    System.out.println("IncreamentResult5 is"+increamentResult5);
    }
    public static int doit(int i){
    return i;
    }
    }
    hope i didn't confuse you too much :) i'm new to java too
  • Katherine_DeloKatherine_Delo Posts: 3Member
    public class IntArithmeticOperations {
    public static void main(String args[]){
    System.out.println("Arithmetic Operations Sample");

    int i=10; You have initialized the code which is good

    int increamentResult=++i;//This is ok
    this is saying add 1 which increments the value i to 11

    int increamentResult1=++(i);//This is also ok
    the brackets doesn't matter in this one its the same as before but you're adding 1 again which makes the value 12

    int increamentResult2=doit(++i); //This is also ok
    this is calling the doit method (which just goes down to see if there's anything in the method doit. You've only returned i so it comes back to this statement and increments it...so now its 13.

    int increamentResult3=++(++i); //This gives a compilation
    error

    this is causing an error because it wants a variable. You're not calling any methods and you can't just put ++ and (++i). make these statements in different lines
    e.g
    int increamentResult3=i++;
    int incrementResult4 = doits(++i);

    int increamentResult4=++(i=i++); //This gives a compilation error
    int increamentResult5=i++;

    //make another method

    int incrementResult5 = doits(i=i++);

    int increamentResult5=++(i+1); //This gives a compilation error

    System.out.println("IncreamentResult is"+increamentResult);
    System.out.println("IncreamentResult1 is"+increamentResult1);
    System.out.println("IncreamentResult2 is"+increamentResult2);
    System.out.println("IncreamentResult3 is"+increamentResult3);
    System.out.println("IncreamentResult4 is"+increamentResult4);
    System.out.println("IncreamentResult5 is"+increamentResult5);
    }
    public static int doit(int i){
    return i;
    }
    }
    hope i didn't confuse you too much :) i'm new to java too
Sign In or Register to comment.