Operators
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators
First, arithmetic operators
Second, relational operators
Third, the logical operators
Fourth, bitwise operators
Bitwise operators acting on the bit , and bit by bit to perform operations
If assumed that A = 60, and B = 13, now expressed in binary form:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Fifth, the assignment operator
Six, miscellaneous operator
sizeof
Returns the type of variables or data byte size ---- Size of int : . 4
Condition? X: Y
? Condition is true if the value is X: otherwise, the value Y
, Comma operator
It will perform a series of sequential operations. Value of the last expression of a comma-separated list of values for the entire comma expression is
. (Dot) and -> (arrow)
It used to refer to members of the class, and the common structural body
Cast cast
Convert one data type to another data type. For example, int (2.2000) returns 2
Operator & Pointer
The return address of a variable
Pointer operator *
Pointing to a variable
Seven, operator precedence
supplement:
1. division / modulo% and if both operands are positive , then the result is positive
If two operands is negative , then the division result is positive , the modulo result is negative
若只有一个操作数是负数,求模的结果取决于机器,除法可以确定结果是负数
2. “--> x” 在 x 的进行自减运算前,会先使用比较符号 > 与右边表达式 0 进行比较,然后返回结果再进行自减运算
循环
// while 循环执行 while( a < 20 ) { cout << "a 的值:" << a << endl; a++; } // for 循环执行 for( int a = 10; a < 20; a = a + 1 ) { cout << "a 的值:" << a << endl; } // 等效于while循环 for ( ;i<=100; ) { sum+=i; i++; } // do 循环执行 ———— 不断循环,直到条件为 假 do { cout << "a 的值:" << a << endl; a = a + 1; }while( a < 20 );
一、基于范围的for循环(C++11)
int my_array[5] = {1, 2, 3, 4, 5}; // 每个数组元素乘于 2 for (int &x : my_array) { x *= 2; cout << x << endl; } // auto 类型也是 C++11 新标准中的,用来自动获取变量的类型 for (auto &x : my_array) { x *= 2; cout << x << endl; }
":"之后的第二区块,代表将被迭代的范围
string str("some string"); //也可以遍历字符串 // range for 语句 for(auto &c : str) { c = toupper(c); }
二、无限循环
由于构成 for 循环的三个表达式中任何一个都不是必需的,您可以将某些条件表达式留空来构成一个无限循环
for( ; ; ) { printf("This loop will run forever.\n"); }
三、循环控制语句
break;
continue;
goto语句
goto 语句允许把控制无条件转移到同一函数内的被标记的语句。
不建议使用
但对于跳出深层嵌套 有很好的作用
for(...) { for(...) { while(...) { if(...) goto stop; . . . } } } stop: cout << "Error in program.\n";
判断
if( xxx ) { ... }else if ( xxx ){ ... }else{ ... }
switch
char grade = 'D'; switch(grade) { case 'A' : cout << "很棒!" << endl; break; case 'B' : case 'C' : cout << "做得好" << endl; break; case 'D' : cout << "您通过了" << endl; break; case 'F' : cout << "最好再试一下" << endl; break; default : cout << "无效的成绩" << endl; }
- switch 语句中的 表达式 必须是一个整型或枚举类型,或者是一个 class 类型,其中 class 有一个单一的转换函数将其转换为整型或枚举类型。
- 在一个 switch 中可以有任意数量的 case 语句。每个 case 后跟一个要比较的值和一个冒号。
- case 的 constant-expression 必须与 switch 中的变量具有相同的数据类型,且必须是一个常量或字面量。
- 当被测试的变量等于 case 中的常量时,case 后跟的语句将被执行,直到遇到 break 语句为止。
- 当遇到 break 语句时,switch 终止,控制流将跳转到 switch 语句后的下一行。
- 不是每一个 case 都需要包含 break。如果 case 语句不包含 break,控制流将会 继续 后续的 case,直到遇到 break 为止。
- 一个 switch 语句可以有一个可选的 default case,出现在 switch 的结尾。default case 可用于在上面所有 case 都不为真时执行一个任务。default case 中的 break 语句不是必需的。