1. # include <iostream>
  2. using namespace std;
  3. int main(){
  4. int a = 2;
  5. int b = 5;
  6. //变量赋值为整数
  7. cout<< "a="<<a <<" ";
  8. cout<< "b="<<b << endl;
  9. b = b + 3;
  10. cout<< "a="<<a <<" ";
  11. cout<< "b="<<b;
  12. }

数据类型 int表示整形

变量的声明

格式:数据类型 变量名1,变量名2,………;

int a,b,c……(伪代码)

  • 先声明、后赋值
    1. int a;
    2. int b;
    3. a = 2;
    4. b = 5;
    同类型的变量可以一起声明
    1. int a,b
  • 声明时,同时赋值
  1. int a = 2, b = 5;

声明时直接赋值

  1. # include <iostream>
  2. using namespace std;
  3. int main(){
  4. const float g = 9.78;
  5. int t ;
  6. float v;
  7. t=10;
  8. v = g*t;
  9. cout<<"落地速度" <<v <<"米/每秒"<< endl;
  10. }

常量的定义

格式:condt 数据类型 变量名1,变量名2……;

const int a,b,c……;(伪代码)

  1. # include <iostream>
  2. using namespace std;
  3. + const float g = 9.78;
  4. int main(){
  5. - const float g = 9.78;
  6. int t ;
  7. float v;
  8. t=10;
  9. v = g*t;
  10. cout<<"落地速度" <<v <<"米/每秒"<< endl;
  11. }
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int i ;
  6. i=0;
  7. i=i+1;
  8. cout<<i<<endl;
  9. i=i+1;
  10. cout<<i<<endl;
  11. i=i+1;
  12. cout<<i<<endl;
  13. i=i+1;
  14. cout<<i;
  15. return 0;
  16. }
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int m;
  6. float r;
  7. m=9800;
  8. r=m*0.04;
  9. cout<<r<<endl;
  10. }

5/2的值是2,不是2.5。而是5.0/2或5/2.0的值是2.5