Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

increment/decrement

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm a relative newbie and am having problem with increment/decrement. I am probably overanalyzing but would appreciate some clarification.

Here is some code followed by the results and questions.

public class ForumQ1 {

public static void main(String[] args)
{
int i = 0;
int a = 0;
System.out.println("Step 1");
i = i++;
System.out.println("i= "+i);
System.out.println("a= "+a);
System.out.println("Step 2");
a = i++;
System.out.println("i= "+i);
System.out.println("a= "+a);
System.out.println("Step 3");
i = i++;
System.out.println("i= "+i);
System.out.println("a= "+a);
System.out.println("Step 4");
a = i++;
System.out.println("i= "+i);
System.out.println("a= "+a);
System.out.println("Step 5");
a = i++;
System.out.println("i= "+i);
System.out.println("a= "+a);
System.out.println("Step 6");
i = --i;
System.out.println("i= "+i);
System.out.println("a= "+a);
}
}

Results:

Step 1
i= 0
a= 0
Step 2
i= 1
a= 0
Step 3
i= 1
a= 0
Step 4
i= 2
a= 1
Step 5
i= 3
a= 2
Step 6
i= 2
a= 2

Questions:

1. In Step 2, does i = 1 because it was post incremented in Step 1?

2. In Step 3, why is i != 2 ?

The order in which expressions are evaluated must have an effect here.

Thanks in advance for any guidance.

:-)

Jerry
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jerry,

i=0;a=0;

Step 1:
i=i++; //This is always i=i; DUMMY STATEMENT.....
This statement first assigns the value of i to i(which is 0), increments the value of i and later it overrides the value of i to 0 with assignment operator.
So, the values are i=0;a=0;

Step 2:
a=i++;
This statement first assigns the value of i to a which is 0 from the first statement and increments the value of i to 1 and assigns it to i.
So, the values are i=1;a=0;

Step 3:
i=i++; //which is i=i
This remains the values of i and a not changed.
So, the values are i=1;a=0;

Step 4:
a=i++;
This assigns the value of a to i(which is 1) and increments i value.
So, the values are i=2;a=1;

Step 5:
i=3;a=2;

Step 6:
i=--i; // This is i=i-1;
In this case, the value of i decrements first and assigns it to i which is 2.
So, the values are i=2;a=2;

Hope this helps you about the increment/decrement operator.

Cheers,
Megan.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
meganchy,

Welcome to Javaranch

We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
 
Jerry Bustamente
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Megan - thank you so much for taking the time for such a careful reply. It really helped!

Really appreciate it.

Jerry

 
Arthur, where are your pants? Check under this tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic