【小练习】程序设计基本概念:赋值语句_常用运算符1

1.练习源码

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int x=2, y, z;
	x *= (y=z=5); cout << x << endl;
	z = 3;
	x == (y=z); cout << x << endl;
	x = (y==z); cout << x << endl;
	x = (y&z); cout << x << endl;
	x = (y&&z); cout << x << endl;
	y = 4;
	x = (y|z); cout << x << endl;
	x = (y||z); cout << x << endl;
	return 0;
}

2.关键点分析

2.1 运算符作用

符号 作用
x*=y x = x*y
x==y x等于y,返回1;x不等于y,返回0
x&y x和y按位与
x&&y x和y都为真,返回1;否则返回0
x|y x和y按位或
x||y x和y都为假,返回0;否则返回1

2.2 计算过程及答案

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int x=2, y, z;
	x *= (y=z=5); cout << x << endl;
	//2*5等于10
	z = 3;
	x == (y=z); cout << x << endl;
	//x没有重新被赋值,还是10
	x = (y==z); cout << x << endl;
	//y和z相等都是3,逻辑判断返回1,x等于1
	x = (y&z); cout << x << endl;
	//y和z相等都是3,0011&0011等于0011,还是3
	x = (y&&z); cout << x << endl;
	//y和z相等都是3都不为0,均为真,y&&z返回值1,x等于1
	y = 4;
	x = (y|z); cout << x << endl;
	//y是4,z是3,0100|0011=0111,x等于7
	x = (y||z); cout << x << endl;
	//y是4,z是3,均不为假,y||z返回1,x等于1
	return 0;
}

【小练习】程序设计基本概念:赋值语句_常用运算符1

相关文章:

  • 2021-10-30
  • 2021-11-25
  • 2021-10-07
  • 2021-12-24
  • 2022-02-28
  • 2021-07-21
  • 2021-11-16
猜你喜欢
  • 2021-08-27
  • 2021-10-27
  • 2021-05-22
  • 2021-05-01
  • 2021-09-21
  • 2021-12-07
  • 2022-12-23
相关资源
相似解决方案