zoukankan      html  css  js  c++  java
  • 自加++

    i++使用后加1

    ++i使用前加1

    那么 int a=++i  +  ++i;

    是怎么样计算的呢?

    ++i;

    ++i;

    a=i+i=2+2=4;

    而i++是如何计算的呢

    a=i++ + i++;

    先执行a=i+i;

    然后i++;

    i++;

    让我们看下面的代码

    // 例1
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    void main()
    {
        int c = 0, a = 0, b = 0;
    
        cout<<"两次次使用 ++a"<<endl;
        c =  (++a) + (++a); 
        cout<<"the value of c is "<<c<<endl;
        c = a + a;
        cout<<"the value of c is "<<c<<endl<<endl;
        
        cout<<"两次次使用 b++"<<endl;
        c = (b++) + (b++);
        cout<<"the value of c is "<<c<<endl;
        c = b + b;
        cout<<"the value of c is "<<c<<endl<<endl;
    
        a = 0;//先重新初始化下 变量a
        cout<<"a++ 与 ++a 的混合使用, a++在前"<<endl;
        c =  (++a) + (a++); 
        cout<<"the value of c is "<<c<<endl;
        c = a + a;
        cout<<"the value of c is "<<c<<endl<<endl;
    
        a = 0;//先重新初始化下 变量a
        cout<<"a++ 与 ++a 的混合使用, a++在后"<<endl;
        c = (a++) + (++a); 
        cout<<"the value of c is "<<c<<endl;
        c = a + a;
        cout<<"the value of c is "<<c<<endl<<endl;
    
        a = 0;//先重新初始化下 变量a
        cout<<"一个 a++ 与 两个 ++a 的混合使用"<<endl;
        c = (a++) +(++a) + (++a); 
        cout<<"the value of c is "<<c<<endl;
        c = a + a + a;
        cout<<"the value of c is "<<c<<endl<<endl;
    
        a = 0;//先重新初始化下 变量a
        cout<<"两个 a++ 与 一个 ++a 的混合使用"<<endl;
        c = (a++) + (a++) + (++a); 
        cout<<"the value of c is "<<c<<endl;
        c = a + a + a;
        cout<<"the value of c is "<<c<<endl<<endl;
    
        system("pause");
    }

    执行结果如下图:

  • 相关阅读:
    Search Insert Position
    lintcode: 最长连续序列
    lintcode:颜色分类
    lintcode: 堆化
    lintcode: 旋转图像
    lintcode: 寻找旋转排序数组中的最小值
    lintcode: 跳跃游戏 II
    lintcode:最小差
    华为:数独填充
    华为:字符集合
  • 原文地址:https://www.cnblogs.com/mu-tou-man/p/3928553.html
Copyright © 2011-2022 走看看