Never seen before C++ for loop

后端 未结 12 2070
独厮守ぢ
独厮守ぢ 2020-12-07 10:58

I was converting a C++ algorithm to C#. I came across this for loop:

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

It gives no

相关标签:
12条回答
  • 2020-12-07 11:26

    The condition of the for loop is in the middle - between the two semicolons ;.

    In C++ it is OK to put almost any expression as a condition: anything that evaluates to zero means false; non-zero means true.

    In your case, the condition is u--: when you convert to C#, simply add != 0:

    for (u = b.size(), v = b.back(); u-- != 0; v = p[v]) 
        b[u] = v; //                     ^^^^ HERE
    
    0 讨论(0)
  • 2020-12-07 11:27

    The condition is u--;, because it is in the second position of the for instruction.

    If the value of u--; is different from 0, it will be interpreted as true (i.e., implicitly casted to the boolean value true). If, instead, its value is 0, it will be casted to false.

    This is very bad code.

    Update: I discussed the writing of "for" loops in this blog post. Its recommendations can be summarized in the following paragraphs:

    A for loop is a practical, readable (once you get used to it) and terse construct, but you need to use it well. Because of its uncommon syntax, using it in a too imaginative way is not a good idea.

    All parts of the for loop should be short and readable. Variable names should be chosen to make it easy to understand.

    This example clearly violates these recomendations.

    0 讨论(0)
  • 2020-12-07 11:27
    for (u = b.size(), v = b.back(); u--; v = p[v]) 
       b[u] = v;
    

    In above code, u and v are initialized with b.size() and b.back().

    Every time the condition is checked, it executes decrement statement too i.e. u--.

    The for loop will exit when u will become 0.

    0 讨论(0)
  • 2020-12-07 11:28

    all the answers are correct :-

    for loop can be used in a variety of ways as follows :

    Single Statement inside For Loop
    Multiple Statements inside For Loop
    No Statement inside For Loop
    Semicolon at the end of For Loop
    Multiple Initialization Statement inside For
    Missing Initialization in For Loop
    Missing Increment/Decrement Statement
    Infinite For Loop
    Condition with no Conditional Operator.
    
    0 讨论(0)
  • 2020-12-07 11:31

    This will be the C# form of your loop.

    // back fetches the last element of vector in c++.
    for (u = b.size(), v = b.back(); (u--) != 0; v = p[v]) 
    {      
      b[u] = v;      
    }
    

    Just replace equivalent for size() and back().

    What it does is reverses the list and stores in an array. But in C# we directly have system defined function for this. So you don't need to write this loop also.

    b = b.Reverse().ToArray();
    
    0 讨论(0)
  • 2020-12-07 11:34

    The condition is the result of u--, which is the value of u before it was decremented.

    In C and C++, an int is convertible to bool by implicitly doing a != 0 comparison (0 is false, everything else is true).

    b.back() is the last element in a container, which is b[b.size() - 1], when size() != 0.

    0 讨论(0)
提交回复
热议问题