赋值运算符用于将表达式的值/结果赋给变量(常量-在常量声明的情况下)。在执行基于赋值运算符的语句时,它将在右侧写的值(或表达式的结果)分配给在左侧写的变量。
语法:
variable = value;
C / C ++语言提供了简单的赋值运算符是“=”,但一些其他的赋值运算(这是分配和其他经营者的组合)都可以使用。
赋值运算符是
编号 | 运算符 | 描述 | 例 |
---|---|---|---|
1 | = | 简单赋值运算符 | x = 10 |
2 | + = | 添加和赋值运算符 | x + = 10 |
3 | -= | 减法和赋值运算符 | x-= 10 |
4 | * = | 乘法和赋值运算符 | x * = 10 |
5 | / = | 除法和赋值运算符 | x / = 10 |
6 | %= | 模块和赋值运算符 | x%= 10 |
7 | << = | 左移和赋值运算符 | x << = 10 |
8 | >> = | 右移和赋值运算符 | x = >> 10 |
9 | &= | 按位与和赋值运算符 | x&= 10 |
10 | | = | 按位或与赋值运算符 | x | = 10 |
11 | ^ | | 按位XOR和赋值运算符 | x ^ = 10 |
注意:在右侧,可以使用值,表达式或任何变量。
它是一个简单的赋值运算符,用于将值和表达式的结果赋值给变量。
语法:
variable = value;
示例
//C ++程序演示 //示例 #include <iostream> using namespace std; int main(){ int x = 0; x = 10; cout << "value of x = " << x << endl; return 0; }
输出:
value of x = 10
它将表达式的值或结果添加到变量的当前值,并将结果分配给变量。
语法:
variable += value;equivalent to: variable = variable + value;
示例
//C ++程序演示 //示例 #include <iostream> using namespace std; int main(){ int x = 10; cout << "Before the operation, x = " << x << endl; x += 5; cout << "After the operation, x = " << x << endl; return 0; }
输出:
Before the operation, x = 10 After the operation, x = 15
它将表达式的值或结果减去该变量的当前值,并将结果分配给该变量。
语法:
variable -= value;equivalent to: variable = variable - value;
示例
//C ++程序演示 //示例 #include <iostream> using namespace std; int main(){ int x = 10; cout << "Before the operation, x = " << x << endl; x -= 5; cout << "After the operation, x = " << x << endl; return 0; }
输出:
Before the operation, x = 10 After the operation, x = 5
它将表达式的值或结果与变量的当前值相乘,并将结果分配给变量。
语法:
variable *= value;equivalent to: variable = variable * value;
示例
//C ++程序演示 //示例 #include <iostream> using namespace std; int main(){ int x = 10; cout << "Before the operation, x = " << x << endl; x *= 5; cout << "After the operation, x = " << x << endl; return 0; }
输出:
Before the operation, x = 10 After the operation, x = 50
它将表达式的值或结果除以变量的当前值,然后将结果(商)分配给变量。
语法:
variable /= value;equivalent to: variable = variable / value;
示例
//C ++程序演示 //示例 #include <iostream> using namespace std; int main(){ int x = 10; cout << "Before the operation, x = " << x << endl; x /= 5; cout << "After the operation, x = " << x << endl; return 0; }
输出:
Before the operation, x = 10 After the operation, x = 2
它将表达式的值或结果除以变量的当前值,然后将结果(余数)分配给变量。
语法:
variable %= value;equivalent to: variable = variable % value;
示例
//C ++程序演示 //示例 #include <iostream> using namespace std; int main(){ int x = 10; cout << "Before the operation, x = " << x << endl; x %= 5; cout << "After the operation, x = " << x << endl; return 0; }
输出:
Before the operation, x = 10 After the operation, x = 0
它将变量的值向左移动给定位数(值),并将结果分配给变量。
语法:
variable <<= value;equivalent to: variable = variable << value;
示例
//C ++程序演示 // example of <<= operator #include <iostream> using namespace std; int main(){ int x = 0x0A; cout << "Before the operation, x = " << x << endl; x <<= 0x02; cout << "After the operation, x = " << x << endl; return 0; }
输出:
Before the operation, x = 10 After the operation, x = 40
它将变量的值向右移动给定位数(值),并将结果分配给变量。
语法:
variable >>= value;equivalent to: variable = variable >> value;
示例
//C ++程序演示 // example of >>= operator #include <iostream> using namespace std; int main(){ int x = 0x0A; cout << "Before the operation, x = " << x << endl; x >>= 0x02; cout << "After the operation, x = " << x << endl; return 0; }
输出:
Before the operation, x = 10 After the operation, x = 2
它对具有给定值的变量的现有值执行按位与(&)操作,并将结果分配给变量。
语法:
variable &= value;equivalent to: variable = variable & value;
示例
//C ++程序演示 // example of &= operator #include <iostream> using namespace std; int main(){ int x = 0x0A; cout << "Before the operation, x = " << x << endl; x &= 0x02; cout << "After the operation, x = " << x << endl; return 0; }
输出:
Before the operation, x = 10 After the operation, x = 2
它对具有给定值的变量的现有值执行按位或(|)操作,并将结果分配给变量。
语法:
variable |= value;equivalent to: variable = variable | value;
示例
//C ++程序演示 //示例 #include <iostream> using namespace std; int main(){ int x = 0x0A; cout << "Before the operation, x = " << x << endl; x |= 0x02; cout << "After the operation, x = " << x << endl; return 0; }
输出:
Before the operation, x = 10 After the operation, x = 10
它对具有给定值的变量的现有值执行按位XOR(^)操作,并将结果分配给变量。
语法:
variable ^= value;equivalent to: variable = variable ^ value;
示例
//C ++程序演示 //^ =运算符的示例 #include <iostream> using namespace std; int main(){ int x = 0x0A; cout << "Before the operation, x = " << x << endl; x ^= 0x02; cout << "After the operation, x = " << x << endl; return 0; }
输出:
Before the operation, x = 10 After the operation, x = 8
C ++程序演示各种赋值运算符的示例
//C ++程序演示 example //各种赋值运算符 #include <iostream> using namespace std; int main(){ int x = 0; //=运算符 x = 20; cout << "x = " << x << endl; //+ =运算符 x += 5; cout << "x = " << x << endl; //-=运算符 x -= 5; cout << "x = " << x << endl; //* =运算符 x *= 5; cout << "x = " << x << endl; /// =运算符 x /= 3; cout << "x = " << x << endl; //%=运算符 x %= 5; cout << "x = " << x << endl; // <<= operator x <<= 5; cout << "x = " << x << endl; // >>= operator x >>= 5; cout << "x = " << x << endl; // &= operator x &= 5; cout << "x = " << x << endl; //| =运算符 x |= 5; cout << "x = " << x << endl; //^ =运算符 x ^= 10; cout << "x = " << x << endl; return 0; }