C# Programming - Operators - Discussion

Discussion Forum : 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 ); 
5 6
6 5
6 6
7 7
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
10 comments Page 1 of 1.

Tulasi Reddy said:   1 decade ago
++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
(1)

Shiva said:   1 decade ago
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.

Eshank said:   1 decade ago
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.

Ahmed Shakhoun said:   6 years ago
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.
(2)

Sarala said:   1 decade ago
!(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.

Roshni said:   1 decade ago
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

Raghvendra Singh Rajawat said:   1 decade ago
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.

Sachin said:   1 decade ago
The full flow is clear but from where does the execution of increment happens left or right?

Ravi said:   1 decade ago
How values are get incremented ? I couldn't understand concept. Please explain steps.

Pandurang patil said:   1 decade ago
C language executed increment from right to left. Then what about c#?

Post your comments here:

Your comments will be displayed after verification.