【小練習】程式設計基本概念:賦值語句_常用運算子1
阿新 • • 發佈:2018-12-13
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;
}