cool hit counter Is i=i++ really easy? C/C++, java, php exploration_Intefrankly

Is i=i++ really easy? C/C++, java, php exploration


It took reading Writing Quality Code: 151 Tips for Improving Java Programs today, one of which is the trap of self-adding, to feel that I don't understand enough about self-adding, and see what the results of the code below are.

in java
public static void main(String[] args) {
        int count1 = 0;
        int count2 = 0;
        for(int i = 0; i < 10; i++)
        {
            count1 = count1++;
            count2++;
        }
        System.out.println(" post-cyclecount1="+count1);
        System.out.println(" post-cyclecount2="+count2);
    }

The result of the execution in myeclipse is as follows

java execution results.png

PHP
<?php 
    $count1 = 0;
    $count2 = 0;
    for($i = 0; $i < 10; $i++)
    {
        $count1 = $count1++;
        $count2++;
    }
    echo " post-cyclecount1=".$count1."<br>";
    echo " post-cyclecount2=".$count2;
?>

The result is displayed in the browser as follows

PHP execution results.png

C language
int _tmain(int argc, _TCHAR* argv[])
{
    int count1 = 0;
    int count2 = 0;
    for (int i = 0; i < 10; i++)
    {
        count1 = count1++;
        count2++;
    }
    printf(" post-cyclecount1=%d
", count1);
    printf(" post-cyclecount2=%d", count2);
    getchar();
    return 0;
}

The result of the execution in VS2013 is as follows

C execution results.png

C++
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int count1 = 0;
    int count2 = 0;
    for (int i = 0; i < 10; i++)
    {
        count1 = count1++;
        count2++;
    }
    cout << " post-cyclecount1=" << count1 << endl;
    cout << " post-cyclecount2=" << count2 << endl;
    system("pause");
    return 0;
}

The result of the execution in VS2013 is as follows

C++ execution results.png

As you have found out in java and php, count = count++; The statement does not return to add 1 to the value of count, whereas in C/C++ it does add 1 to the value of count, which is indeed a straightforward interpretation obtained through the execution results.

count++ is an expression, and the mechanism for handling i++ self-incrementing expressions may be different in different languages, starting with the language description of how it is handled in Java

 int temp = count;  // first copy the value of count to the temporary variable area
 count++;  //add 1 to the value of count
 count = temp;  //returns the value of temp to count

So, in java language, the value of count is always 0, no matter how many times the loop is maintained in the initial state, do not assign the same variable more than once in a single expression in java.

And in the C language

 count = count++;  //equivalent to count++, since C treats them the same

The simple way to circumvent this self-increasing trap is to putcount = count++ Write directly ascount++ and do not assign it to itself in front. Some would say it's pointless to simply talk about syntax, but anyone who can explain the sentence count = count++ clearly in a different language is definitely not simple, and there are design principles involved here for different languages. I'm not boasting here haha, I'm bringing this out to share with you because I also had a somewhat simplistic understanding here before, thinking that i++ is assignment followed by operation and ++i is operation followed by assignment, which is mostly my influence from C and C++, which is not the case in Java.


Recommended>>
1、How can technology help seniors live better when we get older
2、A Brief Look at TensorFlow 9 Code Framework Explained
3、To those who are struggling in all walks of life with this you will stand out like a star
4、Making Growth More Possible Our school records success in WRO Robotics Competition
5、7 Common Cloud Data Management Pitfalls Businesses Need to Avoid

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号