C# Programming - Operators - Discussion

Discussion :: Operators - General Questions (Q.No.11)

11. 

What will be the output of the C#.NET code snippet given below?

int num = 1, z = 5;

if (!(num <= 0))
    Console.WriteLine( ++num + z++ + " " + ++z ); 
else
    Console.WriteLine( --num + z-- + " " + --z ); 

[A]. 5 6
[B]. 6 5
[C]. 6 6
[D]. 7 7

Answer: Option D

Explanation:

No answer description available for this question.

Ravi said: (Aug 13, 2011)  
How values are get incremented ? I couldn't understand concept. Please explain steps.

Roshni said: (Aug 20, 2011)  
n=1, z=5

++n; n=2 & z++; z=5; Their sum is 7 then z is incremented by one z=6;

Then again ++z means it is incremented by one z=7.

Output : 7 7

Shiva said: (Jan 9, 2012)  
Here num=1
then ++num means first increment valu then perform operation
z++ means after operation result will increment by 1.
after print 7,7.
go to else part
where num=2 and z=7
where -- means num decrement by 1and perform operation
after operation z decrement by 1 and print.

Tulasi Reddy said: (Jun 17, 2012)  
++num + z++ + " " + ++z
num=1, z=5

++num= 1+1=2;//preincrement the num i.e.,increment by 1

z++= 5 because of post increment first it will print result after that it will increment by 1 so now z=6;

++z=6+1=7;//preincrement the z i.e.,increment by 1

So ++num + z++ + " " + ++z
2+5 7
7 7

Sarala said: (Nov 13, 2012)  
!(1<=0) is true.

++num+z++ +" "+ ++z
++num=num+1=1+1=2
z++=,assainment,z=5,(2+5=7),after increment z+1= 5+1=6
++z=(first inc), 6+1=7

So 2+5 7
7 7 Answer.

Eshank said: (Aug 29, 2013)  
Here n=1 firstly and is get increased by 1 (++n) i.e. It become 2 and z=5 so on adding gives 7.

And after that z is increased by 1.

In next step z is previously increased by 1 and then printed so final value is 7 again.

So answer is : 7 7.

Sachin said: (Sep 6, 2013)  
The full flow is clear but from where does the execution of increment happens left or right?

Raghvendra Singh Rajawat said: (Jan 31, 2014)  
If num=1, z=5 then
++num + z++ + " " + ++z.

Now,
num=2 and z=5(coz of post increment ) = 7.

And,
Value of z becomes 6.
++z = 7.

7 7.

Pandurang Patil said: (Sep 11, 2015)  
C language executed increment from right to left. Then what about c#?

Ahmed Shakhoun said: (Sep 18, 2019)  
Firstly!

False = true so if statement is executed.

After this num++ => num=1+num so num = 2 and z++ => z=z+1 so z increment but not is the current step.

So in current result = 2+5.
After this z=6 so num=6+1.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.