This article brings you a detailed introduction to the basic operators and logic control of Java (with examples). It has certain reference value. Friends in need can refer to it. Hope it helps.
Operators and logic control
Operators
Operators in java can be divided into the following types:
Operators
Relational operators
Bitwise operators
Logical operator
Assignment operator
Ternary operator
Operator | Description |
---|---|
Addition | |
- | Subtraction |
* | Multiplication |
/ | Division |
Description | ||
---|---|---|
And, if the corresponding bits are all 1, the result is 1, otherwise it is 0 | ||
Or, if the corresponding bits are all 0, the result is 0, otherwise it is 1 | ||
XOR, if the corresponding bit values are the same, the result is 0, otherwise it is 1 | ||
Negation, the bitwise negation operator flips each bit of the operand, that is, 0 becomes 1, and 1 becomes 0 | ||
Bitwise left shift operator. The left operand is shifted left by the number of digits specified by the right operand | ||
Bitwise right shift operation symbol. Shift the left operand bitwise right by the number of digits specified by the right operand | ||
##\ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Logical OR. If either of the two operands is true, the condition is true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The order of logical judgment is from left to right. When making logical judgments, there are ordinary AND (&), ordinary OR (|) and short-circuit AND (&&), short-circuit OR (||). Their differences are:
Copy after login Assignment operation##OperatorDescription=Simple assignment operator, assigns the value of the right operand to the left operand =Additional assignment operator, which adds the left operand and the right operand and assigns them to the left operand-=Subtraction and assignment operator, which subtracts the left operand and the right operand and assigns them to the left operand*=The multiplication and assignment operator, which multiplies the left operand and the right operand and assigns them to the left operand/=Division and assignment operators, which divide the left operand and the right operand and assign the value to the left operand(%)=modulus and assignment operator, which modulo the left and right operands and then assigns the value Give the left operand ##<<=>>=##&=bitwise AND Assignment operator, C&=2 is equivalent to C=C&2 bitwise XOR assignment operator, C ^ = 2 is equivalent to C = C ^ 2=bitwise OR Assignment operator三目运算符三目运算符也被称为条件运算符,该运算符有3个操作数,并且需要判断布尔表达式的值。该运算符的主要是决定哪个值应该赋值给变量。表达式如下:
Copy after login 逻辑控制程序逻辑主要分为三种逻辑结构:
顺序结构代码均是由上至下,由左至右顺序执行。 分支结构分支结构是一种判断结构,有两类语法支持:if、switch if 分支语句此类语句有多种定义形式
Copy after login
Copy after login
Copy after login
Copy after login switch 语句if的判断支持布尔表达式,switch 语句不支持布尔表达式的判断。 语法格式如下:
Copy after login switch case 语句有如下规则:
switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。 循环结构Java中有三种主要的循环结构:
while循环while是最基本的循环,它的结构为:
Copy after login 只要布尔表达式为 true,循环就会一直执行下去。 do…while 循环对于 while 语句而言,如果不满足条件,则不能进入循环。 do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。
Copy after login 注意:布尔表达式在循环体的后面,所以语句块在检测布尔表达式之前已经执行了。 如果布尔表达式的值为 true,则语句块一直执行,直到布尔表达式的值为 false。 for循环for循环执行的次数是在执行前就确定的。语法格式如下:
Copy after login 关于 for 循环有以下几点说明:
Java 增强 for 循环JDK1.5 引入了一种主要用于数组的增强型 for 循环。
Copy after login 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。 表达式:表达式是要访问的数组、集合,或者是返回值为数组的方法。 demo:
Copy after login break/continue
在 for 循环中,continue 语句使程序立即跳转到更新语句。 In a while or do...while loop, the program immediately jumps to the judgment statement of the Boolean expression.
|
The above is the detailed content of Detailed introduction to Java basic operators and logic control (with examples). For more information, please follow other related articles on the PHP Chinese website!