6
Answers

Prefix and Postfix operator

Ask a question
Maha

Maha

11y
1.7k
1
Continue from
http://www.c-sharpcorner.com/Forums/Thread/220857/code-snippet-functionality.aspx

j is initialized to 1 therefore 1st operand (j++) takes the value of 1. Concurrently 2nd operand also takes the value of 1 but it as a prefix(++j) operator value 1 is quickly convert into 2. Therefore k (1+2) must be 3, but in the output k=4 how can this be explained.

using System;
namespace Code
{
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


Answers (6)