Article Directory
1. Common function symbols
Note
//文字 单行注释
/*文字*/ 多行注释
/** 文字 **/ 文档注释=
Arithmetic Operators
+ - * /
/ 整数相除仍是整数向下取整 即 3/2 = 1
Modulus operator
% 求两数相除的余数 5%3 = 2
Increment decrement operator
int a = 2, b = 0;
a++; 先参与运算而后加1 b = a++; 则 b == 2
++a; 先加1后参与运算 b = ++a; 则 b == 3
a--; 先参与运算而后减1 b = a--; 则 b == 2
--a; 先减1后参与运算 b = --a; 则 b == 1;
Relational Operators
== != <= >= > <
Logical Operators
&(与) 1 & 1 = 1 1 & 0 = 0
|(或) 1 | 1 = 1 1 | 0 = 1 0 | 0 = 0
!(非) !1 = 0 0! = 1
(0表示false 1表示true)
&&(短路与) 布尔表达式1 && 布尔表达式2 如果表达式1为假 则 表达式2不计算
||(短路或) 布尔表达式1 || 布尔表达式2 如果表达式1为真 则 表达式2不计算
Ternary operator
x = 布尔表达式 ?表达式1:表达式2;
当布尔表达式为true x的值为表达式1;
当布尔表达式为false x的值为表达式2;
x = 1 < 5 ? 3 : 2;
则 x = 3
2. branch structure
if…else
if(布尔表达式){
表达式; // 布尔表达式为真执行
}
else{
表达式; // 布尔表达式为假执行
}
if…else if … else
if(布尔表达式){
表达式;
}
else if(布尔表达式){
表达式;
}
else if(布尔表达式){
表达式;
}
else{
表达式;
}
从上到下,当有一个布尔表达式为真,则执行相应代码块,其余均不执行。
switch - case
swtich (A) {
Case B1: Expression; BREAK;
Case B2: Expression; BREAK;
...
Case BN: Expression; BREAK;
default: Expression; BREAK;
}
Note that a, b1, b2 ... b2 values can is a byte, short, int, char, String, there are enumerated types, such as:
int a = 3;
swtich(a){
case 1: System.out.print(1);break;
case 2: System.out.print(2);break;
case 3: System.out.print(3);break;
default: System.out.print(4);break;
输出为:3
3. loop structure
while
> while(布尔表达式){
}
唯一和C/C++有区别的是 java中有专门的布尔类型,
所以==int a = 5; while(a--) 这种写法是错误的==,因为整型a不是布尔类型。
do…while
do {
expressions;
} the while (Boolean expressions)
for
for traversing two ways:
- for(int i = 0; i < n; i++){
表达式;
}int[] a = {1,2,3,4};
for(int i : a){
System.out.print(i+" ");
}
结果为:1 2 3 4
也就是for(循环类型:循环类型名)
break
>break 可以直接结束一层循环
continue
The next cycle can continue directly
for(int i = 1; i <= 3; i++){
System.out.print(i);
System.out.print(i);
}
输出为:112233
for(int i = 1; i <= 3; i++){
System.out.print(i);
break;
System.out.printf(i);
}
输出为:1
for(int i = 1; i <= 3; i++){
System.out.print(i);
continue;
System.out.print(i)
}
输出为:123