# java operators and if statements

Logical operator
&Indicates logical and, false in case of false
|Indicates a logical or. If true, it is true
^Indicates logical XOR, false if it is the same, and true if it is different
! Indicates that the logic is not. If it is not false, it will be true. If it is not true, it will be false
&&The difference between and
The results obtained by the two operators are the same, but && have the effect of short circuit, that is, when the left is false, the right is not executed
||The difference between and
The final results of the two operators are the same, ðž“œ has the effect of short circuit, that is, when the left is true, the right is not executed

Bitwise Operators
&Indicates and, 0 in case of 0
|Indicates or, in case of 1, then 1
^Indicates XOR, 0 for the same and 1 for the different
The characteristic is that a number is XOR twice by the same number, or the number itself
~Indicates non, non-1 means 0, non-0 means 1
”< < "move left, discard the highest bit on the left, and supplement 0 on the right
Moving a few bits to the left is a power of 2
”>>"Shift right, the highest position is zero, fill zero on the left, the highest position is one, and fill 1 on the left
Moving a few bits to the right is the power of dividing by two
"> > >" moves to the right without sign, and 0 is added to the left no matter what the high order is
The most effective result is 2 * 8
2<<3
Basic usage of ternary operators
Format is
(relationship expression)? Expression 1: expression 2;
Compare whether two integers are the same

```int x = 5;
int y = 10;
boolean b = (x == y) ? true : false;//booolean b = (x == y)
system.out.println("b="+b);
```

Compare three integer maxima

```int a = 10;
int b = 20;
int c = 30;
//First, compare the values of any two numbers to find the maximum value of the two numbers
int temp = (a > b) ? a : b;
//Compare the maximum value of the first two numbers with the third number to obtain the maximum value
int max =(temp > c) ? temp : c;
//Maximum output
System.out.println("max = "+max);
```

Basic format of keyboard entry
In order to make the data of the program more consistent with the developed data and make the program more flexible.
Format:

```import java.util.Scanner;//Import classes in package
class Demo_Scanner {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);//Create keyboard entry object
System.out.println("Please enter an integer");
int x = sc.nextInt();//Store the data entered by the keyboard in x
System.out.println(x);
System.out.println("Please enter the second number:")
int y = sc.nextInt();
System.out.println(y);
}
}
```

practice
Find the sum of two numbers

```import java.util.Scanner;
class test_Scanner {
public static void main(string[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first number:");
int x=sc.nextInt();
System.out.println("Please enter the second number:");
int y = sc.nextInt();
int sum=x+y;
System.out.println(sum);
}
}
```

practice
Gets the maximum value of two numbers

```import java.util.Scanner;
class test_Scanner {
public static void main(string[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first integer:");
int x = sc.nextInt();
System.out.println("Please enter the second integer:");
int y = sc.nextInt();
int max = (x > y) ? x : y;
System.out.println(max);
}
}
```

practice
Compare two numbers for equality

```import java.util.Scanner;
class practice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first integer:");
int x = sc.nextInt();
System.out.println("Please enter the second integer:");
int y = sc.nextInt();
boolean b = (x == y);
System.out.println(b);
}
}
```

practice
Gets the maximum of the three numbers

```import java.util.Scanner;
class practice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first integer:");
int x = sc.nextInt();
System.out.println("Please enter the second integer:");
int y = sc.nextInt();
System.out.println("Please enter the third integer:");
int z = sc.nextInt();
int temp = (x>y)?x:y;
int ma = (temp>z)?temp:z;
System.out.println(ma);
}
}
```

Sequential structure statement
Process control statement: it can control the execution process of the program
Classification of process control statements: sequential structure, selection structure and loop structure
Execution process: execute from top to bottom.
Select structure if
if (comparison expression){
Statement body;
}

```public class IFTEST {
public static void main(String[] args) {
int age=19;
if(age>=18){
System.out.println("Grown up!!!");
}
}
}
```

matters needing attention:
Whether the comparison expression is simple or complex, the result must be boolean
If the statement question controlled by the if statement is a statement, the braces can be omitted
Generally speaking, there is no semicolon if there is a left brace, and there is no left brace if there is a semicolon

if (comparison expression){
Statement body 1;
}

```else {
Statement body 2;
}
```

Note: there is no comparison expression after else.
if statements and ternary operators can achieve the same effect
The difference is:
if statements that can be implemented by ternary operators can be implemented, and vice versa

If (compare expression 1){
Statement body 1;
}else if (compare expression 2){
Statement body 2;
}else{
Statement body 3;
}
swich

```int x;
swich(x){//The basic data type can receive byte, short, char, int and string;
case 1:
Statement body 1;
case 2:
Statement body 2;
break;
default:
Statement body n;
break;
```
```public class SWITCHTEST {
public static void main(String[] args) {
String name ="zhangsan";
String gender = "man";
switch (gender) {
case "man":
System.out.println(name+"It's a"+gender);
case "women":
System.out.println(name+"It's a"+gender);
default:
SyStem.out.println(name+"It's a"+gender);
}
}
}
```

matters needing attention:
case can only be followed by constants, not variables, and the values behind multiple cases cannot be the same
default can be omitted but is not recommended because it can prompt for incorrect conditions
The middle of break cannot be omitted, and case penetration will occur
The operation ends when a break or}switch statement is encountered
The difference between if and switch
switch recommends omitting when judging fixed values
if it is recommended to judge the interval or range
Examples
Input month and output corresponding season

```import java.util.Scanner;

public class IFTEST {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int month = sc.nextInt();
if(month>=1&&month<=3) {
System.out.println("spring");
}
else if(month>=4&&month<=6) {
System.out.println("summer");

}
else if(month>=7&&month<=9) {
System.out.println("autumn");

}
else{
System.out.println("east");
}
}
}

```
```import java.util.Scanner;

public class SWITCHTEST {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int month = sc.nextInt();
switch(month) {
case 1:
System.out.println("spring");break;
case 2:
System.out.println("spring");break;
case 3:
System.out.println("spring");break;
case 4:
System.out.println("summer");break;
case 5:
System.out.println("summer");break;
case 6:
System.out.println("summer");break;
case 7:
System.out.println("autumn");break;
case 8:
System.out.println("autumn");break;
case 9:
System.out.println("autumn");break;
default:
System.out.println("winter");
}
}
}

```

Keywords: Java Back-end

Added by yonta on Tue, 26 Oct 2021 11:41:58 +0300