Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why output is 1,1 ?

 
Amisha Shah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
programm :

class Cc{

static int f1(int i) {

System.out.print(i + ",");

return 0;

}

public static void main (String[] args) {

int i = 0;

i = i++ + f1(i);

System.out.print(i);

}}

according to me output of this programm is 1,0 . but output is 1,1. how this programm works ?
 
Graham Walsh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
posted March 15, 2006 07:19 AM
--------------------------------------------------------------------------------
programm :

class Cc{

static int f1(int i) {

System.out.print(i + ",");

return 0;

}

public static void main (String[] args) {

int i = 0;

i = i++ + f1(i);

System.out.print(i);

}}


when you call;

i = i++ +f(i)

you're effectively doing;

i = 0++ + f(1)
=> i = 1 + 0;
=> i = 1;

f(1 ) => outputs "1,"

and the print statement prints the value of i which, is 1

output 1,1

G
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run the code, the output is 1,0
 
Graham Walsh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doh. my fault.

i = 0++ + f(1)
=> i = 1 + 0;
=> i = 1;


is wrong. It should have read;

i = 0++ + f(1)
=> i = 0 + 0;
=> i = 0;


sorry sorry sorry

G
 
Amisha Shah
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right.

i = 0++ + f(1)
=> i = 0 + 0;
=> i = 0;

output is 1,0

thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic