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

What is the follow of this code?

 
rex tony
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class C{
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);
}}

The answer is 1,0
What is the follow of this code?
 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your question is not clear... the answer 1,0 is correct what is it that you want to know about the program...??
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi tony,

following is the sequence of execution for

int i = 0;
i = i++ + f1(i);


i++ --> I value is substituted for expresseion (0), and i value is incremented . so i value is 1.

f1(i) -- > f1(1) is passed , which is returning 0.

but i = 0 + 0 . so the result is i =0 will be printed.
 
Kishore Kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i = 0
i = i++ + f1(i) is evaluated as:
i = 0 + f1(i) //i value(0) is used first. And then i is incremented to 1
i = 0 + f1(1) // i = 1 here.
f1(1) function is called. So 1 is printed. and 0 is returned.
now i = 0 + 0(returned value from f1(int) function)
so, i = 0
Finally you are printing the value of i which is 0.

i hope i explained clearly..
 
rex tony
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello kesava narayana
1st i wanna thanks ,cause really i understand that program follow thor your explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic