There are no conditional statements in the for statement in this code. Why?

Asked 2 years ago, Updated 2 years ago, 83 views

I'm changing the C++ algorithm to C#, and I saw a for statement like this.

for (u = b.size(), v = b.back(); u--; v = p[v]) 
b[u] = v;

Errors appear on C# and work well on C++. I don't know what this repeat is doing. I don't think there's a conditional statement, but why is it implemented?

c# c++ for-loop

2022-09-22 11:50

1 Answers

u-- between two semicolons (;) is where you check the condition.

The conditional statement in C++ can accommodate any expression that can be valued, and most expressions in C++ return the value, so there is no error.

Because in terms of the conditional statement, whether it's an address value, a boolean value, or another value, if it's just 0, it's false, or it's true.

In this case, the conditional statement is u--, so if you want to change it to C#, just !Attach =

for (u = b.size(), v = b.back(); u-- != 0; v = p[v]) 
    b[u] = v;


2022-09-22 11:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.