This will help improve code maintainability quite a bit. That's where everyone will be looking for it, so that's where it should go. Put either i++ or ++i as the "increment" expression in the parens for the for statement() ![]() Currently, the only advantage this has is that when you see the ++ before the i, it's easier to read it as "increment i".ĥ. The performance difference between this and the previous choice was small back in the 1980s and is, if I understand correctly, non-existent today. Get rid of the explicit assignment and just use ++i alone on a line. Get rid of the explicit assignment and just use i++ alone on a line.Ĥ. Don't use an increment operator on the right-hand side of your assignment and instead use a plain mathematical expression that has no side-effects: i = i + 1 ģ. This solution is correct but redundant.Ģ. When that gets executed, i is incremented to 5 and the expression value of 5 is then assigned to i. Merely change the i++ to ++i in the assignment statement. You have (at least) five "correct" choices (from worst to best):ġ. So i has the same value before and after the statement is executed, which is what leads to the endless loop. The value of the right-hand expression, 4, is assigned to i. This has the effect of incrementing i to 5, but the value of expression is the old value: 4.Ģ. i++ has a value equal to the old, not-yet-incremented value of i.ġ. ![]() ++i has a value equal to the new, incremented value of i. ![]() In both cases, the effect is to increment i by one. As with any expression, these each have a value and a possible effect. There are two unary (single operand) increment operators: ++i and i++. You should use ++i instead and you will be fine "first assigi(right) to i(left) then plus" Greg Sully wrote:this problem has been answered many times buddy.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |