Home >Java >javaTutorial >Detailed introduction to Java basic operators and logic control (with examples)

Detailed introduction to Java basic operators and logic control (with examples)

不言
不言forward
2019-02-27 11:01:252563browse

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:

  1. Operators

  2. Relational operators

  3. Bitwise operators

  4. Logical operator

  5. Assignment operator

  6. Ternary operator

Operator operation Symbol

##%Remainder (modulo)Auto-increment--Auto-decrement
Operator Description
Addition
- Subtraction
* Multiplication
/ Division
Auto-increment and self-decrement operators are based on position Different, the order of execution is also different.

  • Prefix auto-increment and auto-subtraction method (a,--a): Perform auto-increment or auto-decrement operation first, and then perform expression operation.

  • Suffix auto-increment and self-subtraction method (a,a--): Perform expression operation first, and then perform auto-increment or self-decrement operation.

Relational Operator

OperatorDescription==Check if the values ​​of the two operands are equal, if so the condition is true!=Check if the values ​​of the two operands are equal, if the values ​​are not equal then the condition is true>Check if the value of the left operand is greater than the right The value of the operand, if so then the condition is true<Check whether the value of the left operand is less than the value of the right operand, if so then the condition is True>=Checks whether the value of the left operand is greater than or equal to the value of the right operand, if so then the condition is true<=Check whether the value of the left operand is less than or equal to the value of the right operand, if so then the condition is true ##Bit Operator

Operator&\^~< ;<>>##>>>Bitwise right Shift-padding zero operator. The value of the left operand is shifted right by the number of digits specified by the right operand, and the vacancies obtained by the shift are filled with zerosLogical operations
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

OperatorDescription##&&Logical AND. A condition is true if and only if both operands are true\! Logical negation. Used to invert the logical state of the operand. If the condition is true, the logical NOT operator will give false



##\
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:

使用普通与、或操作时,所有的判断条件都会执行;

使用短路与运算时,只要有一个判断返回了false,后续的判断就不再执行。

使用短路或操作时,只要有一个判断返回了true,后学的判断就不再执行。

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个操作数,并且需要判断布尔表达式的值。该运算符的主要是决定哪个值应该赋值给变量。表达式如下:

variable x = (expression) ? value if true : value if false

逻辑控制

程序逻辑主要分为三种逻辑结构:

  1. 顺序结构

  2. 分支结构

  3. 循环结构

顺序结构

代码均是由上至下,由左至右顺序执行。

分支结构

分支结构是一种判断结构,有两类语法支持:if、switch

if 分支语句

此类语句有多种定义形式

  1. if

if(布尔表达式)
{
   //如果布尔表达式为true将执行的语句
}
  1. if ...else

if(布尔表达式){
   //如果布尔表达式的值为true
}else{
   //如果布尔表达式的值为false
}
  1. if ...else if ... else

if(布尔表达式 1){
   //如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
   //如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
   //如果布尔表达式 3的值为true执行代码
}else {
   //如果以上布尔表达式都不为true执行代码
}
  1. 嵌套的 if…else

if(布尔表达式 1){
   ////如果布尔表达式 1的值为true执行代码
   if(布尔表达式 2){
      ////如果布尔表达式 2的值为true执行代码
   }
}

switch 语句

if的判断支持布尔表达式,switch 语句不支持布尔表达式的判断。
最早只支持整数或者字符判断,jdk1.6支持了枚举判断,jdk1.7支持了String的判断。

语法格式如下:

switch(expression){
    case value :
       //语句
       break; //可选
    case value :
       //语句
       break; //可选
    //你可以有任意数量的case语句
    default : //可选
       //语句
}

switch case 语句有如下规则:

  • switch 语句中的变量类型可以是:整数、char、枚举、String,同时 case 标签必须为字符串常量或字面量。

  • switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。

  • case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。

  • 当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。

  • 当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。

  • switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。

switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。

循环结构

Java中有三种主要的循环结构:

  • while 循环

  • do…while 循环

  • for 循环

while循环

while是最基本的循环,它的结构为:

while( 布尔表达式 ) {
  //循环内容
}

只要布尔表达式为 true,循环就会一直执行下去。

do…while 循环

对于 while 语句而言,如果不满足条件,则不能进入循环。

do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。

do {
       //代码语句
}while(布尔表达式);

注意:布尔表达式在循环体的后面,所以语句块在检测布尔表达式之前已经执行了。 如果布尔表达式的值为 true,则语句块一直执行,直到布尔表达式的值为 false。

for循环

for循环执行的次数是在执行前就确定的。语法格式如下:

for(初始化; 布尔表达式; 更新) {
    //代码语句
}

关于 for 循环有以下几点说明:

  • 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。

  • 然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。

  • 执行一次循环后,更新循环控制变量。

  • 再次检测布尔表达式。循环执行上面的过程。

Java 增强 for 循环

JDK1.5 引入了一种主要用于数组的增强型 for 循环。
Java 增强 for 循环语法格式如下:

for(声明语句 : 表达式)
{
   //代码句子
}

声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

表达式:表达式是要访问的数组、集合,或者是返回值为数组的方法。

demo:

public class Test {
   public static void main(String args[]){
      int [] numbers = {10, 20, 30, 40, 50};
 
      for(int x : numbers ){
         System.out.print( x );
         System.out.print(",");
      }
      System.out.print("\n");
      String [] names ={"James", "Larry", "Tom", "Lacy"};
      for( String name : names ) {
         System.out.print( name );
         System.out.print(",");
      }
   }
}

break/continue

break主要用在循环语句或者 switch,在switch语句中,用来跳出整个语句块。在循环语句中跳出最里层的循环,并且继续执行该循环下面的语句。

continue适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。

在 for 循环中,continue 语句使程序立即跳转到更新语句。

In a while or do...while loop, the program immediately jumps to the judgment statement of the Boolean expression.









left shift assignment operator, C << = 2 Equivalent to C = C << 2
right shift assignment operator, C >> = 2 is equivalent to C = C >> 2

##^=
##\

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete