static void Main()
{
int i, j = 1, k;
for (i = 0; i < 5; i++)
{
k =j++ + ++j;
Console.WriteLine(k+" ");
}
Console.ReadKey();
}
In he above code .. i am getting out put as 4 8 12 16 20 ... well its compiler generated output it cant be wrong but the thing is i am confused .. with the block of code
K= j++ + ++J ;
Here acc to pre & post increment operation mechanism say initially j=1 next step j++ it remains 1 only after that ++j it gets 2 so 1+2 should be 3 but y am i getting 4 ..
Hope u got my point TY
In he above code .. i am getting out put as 4 8 12 16 20 ... well its compiler generated output it cant be wrong but the thing is i am confused .. with the block of code
K= j++ + ++J ;
Here acc to pre & post increment operation mechanism say initially j=1 next step j++ it remains 1 only after that ++j it gets 2 so 1+2 should be 3 but y am i getting 4 ..
Hope u got my point TY

VulpesPosted Jul 25, 2013, 4:41 PM
1) j is stored as the first operand of the + operator.
2) j is incremented because of the postfix ++ operator.
3) j is incremented again because of the prefix ++ operator.
4) j is stored as the second operand of the + operator.
5) The addition takes place of the j values in steps 1) and 4) and is assigned to k
So, following those steps through:
1) j is 1 (first operand)
2) j is 2
3) j is 3
4) j is 3 (second operand)
5) k = 4 (printed to console)
and on the next iteration:
1) j is 3 (first operand)
2) j is 4
3) j is 5
4) j is 5 (second operand)
5) k = 8 (printed to console)
and so on.
Posted Jul 27, 2013, 5:10 PM
VulpesPosted Jul 27, 2013, 7:03 AM
Posted Jul 26, 2013, 3:19 PM
SUNIL GUTTAPosted Jul 26, 2013, 2:55 PM
Posted Jul 26, 2013, 1:51 PM
If you use the Step Into(F11) key in the following program, in first iteration J=3 could you please explain the reason.
using System;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int i, j = 1, k;
for (i = 0; i < 5; i++)
{
k = j++ + ++j;
Console.Write(k + " ");
}
Console.ReadKey();
}
}
}
/*
4 8 12 16 20
*/
SUNIL GUTTAPosted Jul 26, 2013, 6:24 AM
The thing is in k =j++ + ++m ...!! if m & j=1 initially right
1) j++ is post increment i.e it will set j=1 initially and right away it increments after setting j=1 & stores j=2 in for the next instance of j lets say virtually :)
As, in your case there is only one J later to it u have ++m .. so the thing is the virtual values i.e j=2 assigned will be applied in reality to next j instance ,,
Thank you ..!!
Here in j++ + ++j ... after j is assigned to j=1 its internally asuming loop is done so next step is just to increment the value i.e j=2 ,,, which is made available to next j instance i,e as our next one is ++j it will be +1 of virtual value what j hold .. so 2+1 =3 .. so finally we get 3+1 =4 ..... continues
Hope you got it
VulpesPosted Jul 26, 2013, 5:13 AM
k = j++ + ++j;
and you hover your mouse over the variables, you see the value of those variables after the line has been executed i.e. j = 2, m = 2 and k = 3 on the first iteration.
The point here is that the expression j++ has a value which is equal to the value of j before the incrementation takes place and it's this value which is stored as the first operand of the + operator. The incrementation then takes place before the second operand is evaluated.
So, in other words, it's the value of j on entering the for statement (i.e. 1) which is used in the addition.
Ravi ShekharPosted Jul 26, 2013, 12:21 AM
Posted Jul 25, 2013, 8:01 PM
SUNIL GUTTAPosted Jul 25, 2013, 5:42 PM
sorry sorry sorry dam dam sometime i suck .. just thinking only in single point of view .. got my answer in simple the post operator make the value incremented i.e can say virtually inside the loop itself and make available ..
now mud is very much clear hahah haaa haa
VulpesPosted Jul 25, 2013, 5:32 PM
The behavior of the C# compiler is in fact completely predictable.
In your example:
1) j is 1 (first operand)
2) j is then incremented to 2 (postfix ++)
3) m gets incremented to 2 (prefix ++)
4) m is 2 (second operand)
5) The addition of 1) and 4) is 3 (printed to console).
On the next iteration:
1) j is 2 (first operand)
2) j is then incremented to 3 (postfix ++)
3) m gets incremented to 3 (prefix ++)
4) m is 3 (second operand)
5) The addition of 1) and 4) is 5 (printed to console).
and so on.
VulpesPosted Jul 25, 2013, 5:24 PM
However, the postfix and prefix ++ operator has higher priority than the + operator and so the ++ operations are performed first.
Now the value of j++ is the value of j before the operation and the value of ++j is the value of j after the operation.
So, on the first iteration of the loop the value of j++ is 1 and this is then stored as the first operand of the + operator.
The incrementation, then takes place giving j a value of 2.
The value of ++j is then 3 and this gets stored as the second operand of the + operator.
Adding 1 and 3 gives k = 4.
Clear as mud, eh :)
If it isn't very clear I wouldn't worry too much as nobody uses stuff like this in practice unless they're an idiot or an entrant for a C obfuscation contest!
Posted Jul 25, 2013, 5:23 PM
3
5
7
9
11
using System;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int i, j = 1, k, m=1;
for (i = 0; i < 5; i++)
{
k = j++ + ++m;
Console.WriteLine(k + " ");
}
Console.ReadKey();
}
}
}
SUNIL GUTTAPosted Jul 25, 2013, 5:06 PM
2) j is incremented because of the postfix ++ operator. as u said but does it mean iteration done ?? loop over .. i'm confused .. just only this point . i didn't find any loop to make j from 1 to 2
my silly view : when j++ is there after loop only the j should increment right so it will be k=1 + 2
/* 1 beocz it cant be 2 as it is post increment j++ , j remains same ?? */
/* 2 beocz of pre-increment of j */
after looping j becomes 3 becoz post increment that we have to apply after circular notion ///
vulpes, what the order of excecution i.e from right to left or left to right haaa in above thing