Write before: Continue to share some basic operations of JAVA today, this series is mainly code-driven.

## Learning Content Arrangement

JAVA Basic Courses: Data Types (1), Operator Expressions and Program Structure Control (2), Object Oriented Basis: Classes and Objects and Interfaces (3), Object Oriented Basis: Inheritance of Abstract Polymorphic Encapsulation (4), Exception Classes and Common Practical Classes (5), Component and Event Processing (6), IO and JDBC (7), Generics and Collection Functions (8).

#### 2. Operator Expression and Program Structure Control

In this section, we will focus on the use of if, switch, for, while, and break and continue by simply saying a few JAVA operators.

JAVA Operator

The common operators of JAVA are arithmetic operators, self-increasing and self-decreasing operators, relational operations, logical operators and expressions, and assignment operators.

category | operator | Meaning |
---|---|---|

Arithmetic Operators | + | Addition operation |

Arithmetic Operators | - | Subtraction operation |

Arithmetic Operators | * | Multiplication |

Arithmetic Operators | / | Division |

Arithmetic Operators | % | Remainder operation |

Self-increasing and self-decreasing operators | ++，– | Self-increasing, self-decreasing |

Relational Operators | >，>= | Greater than, greater than or equal to |

Relational Operators | <，<= | Less than, Less than or equal to |

Relational Operators | ==，!= | Equal to, not equal to |

Logical operators | &&，||，! | Logical And, Logical Or, Logical Not |

Assignment Operators | = | assignment |

It is worth noting here that there are two types of self-increasing and self-decreasing operations respectively, ++ X and x++, the former increasing first and then using x, the latter using X first and then increasing itself.Another notable thing is logic or judgment. Here are two examples.

package tt; public class Test2 { public static void main(String[] args) { //Enter psvm Press tab to quickly generate statement //Self-increasing test int x = 10; int y = x++; //x++ means to use x before adding 1, that is, to assign and add //int y = ++x; //++x means add 1 before assigning System.out.println("x= "+x); System.out.println("y= "+y); //Logical or Operational Test int s, z = 10; if(((s=0) == 0)||(z=20) == 20) { System.out.println("Current z The value of is: "+z); } } }

x= 11 y= 10 Now the value of z is: 10

It can be seen from the result that ++ x assigns y first, then increases X by itself.Then the logic or judgment below is that because the first condition is satisfied, the second condition is not judged, so z equals 10.

if statement for JAVA

There is little difference between if statements in other languages, mainly in the input format. Here is a simple if statement code to determine which level x=51 belongs to.

//Used to determine which grade x belongs to package tt; public class Test2 { public static void main(String[] args) { int x =51; if (x >= 90){ System.out.println("A"); }else if (x >= 80){ System.out.println("B"); }else if (x >= 60){ System.out.println("C"); }else{ System.out.println("D"); } } }

switch statement in JAVA

A switch statement is a so-called switch statement. When a condition occurs, it executes a case, otherwise default is executed. The following is also an example to enter randomly and draw a prize.

//Random Draw package tt; public class Test2 { public static void main(String[] args) { int number = 0; System.out.println("Enter a positive integer (Enter OK)"); Scanner reader = new Scanner(System.in); number = reader.nextInt(); switch(number){ case 23: System.out.println(number+"Is Third Prize"); break; case 24: System.out.println(number+"Is Second Prize"); break; case 55: System.out.println(number+"Is First Prize"); break; default: System.out.println(number+"Not winning"); break; } } }

The output of this example is not shown here.

for loop in JAVA

In the for loop, starting conditions, judgment conditions and incremental conditions are usually required. In daily life, the for loop also encounters a lot.

//Achieve the sum of Item 11 of 8+88+888+... package tt; public class Test2 { public static void main(String[] args) { long sum = 0, a = 8, item = a; for(int i=1; i<12; i++){ sum = sum + item; item = item*10 + a; } System.out.println(sum); } }

Of course, for loops also have a more convenient way to calculate arrays, which can be indexed automatically after directly declaring variables.

//Sum of arrays package tt; public class Test2 { public static void main(String[] args) { int s[] = {23,34,12,45,3,4,6,123,456}; int sum = 0; for (int i = 0; i < s.length; i++){ sum = sum + s[i]; } System.out.println(sum);//General cycle sum = 0; for (int x : s){ sum = sum + x; } System.out.println(sum);//Cycle of Arrays } }

while operation in JAVA

The while operation is used for loops like for, but when we can't know the number of loops, we can use while to control by conditions.

//while Loop package tt; public class Test2 { public static void main(String[] args) { int sum = 0; for(int i = 0; i <= 100; i = i + 2){ sum = sum + i; } System.out.println(sum);//for calculates the sum of even numbers within 100 int sum1 = 0; int j = 0; while(j <= 100){ sum1 = sum1 + j; j = j + 2; } System.out.println(sum);//while calculates the sum of even numbers within 100 } }

Use of break and continue

In a loop, we often get a result and want to end the loop. We use break, or end the current loop after a result and start the next time, we use the continue function.

//Calculate the sum of odd numbers within 10 and find prime numbers within 100 package tt; public class Test2 { public static void main(String[] args) { int sum = 0, i, j; for (i = 1; i <= 10; i++){ if(i%2==0){ continue;//Satisfies the condition even, enters the next cycle } sum = sum + i; } System.out.println(sum);//Calculates the sum of odd numbers within 10 for(j = 2; j <= 100; j++){ for(i = 2; i <= j/2; i++){ // I <= j/2 is the criterion if (j%i == 0) break; //Exit the current loop layer if the condition is not prime }//Consider whether there is a factor if(i > j/2){ System.out.println(""+j+"prime number");//Count how many prime numbers are within 100 } } } }

Small exercises

So let's find out how many perfect numbers are within 1,000. The perfect number is the sum of the factors of this number (excluding itself) equal to itself, such as 6=1+2+3.

//Find perfect numbers within 1000 package tt; public class Test2 { public static void main(String[] args) { for(int i = 2; i <= 1000; i++){ int total = 0; for(int j = 1; j < i; j++){ if((i%j)==0){ total = total + j; } } if(i == total){ System.out.println(i); } } } }

6 28 496

epilogue

This section shares the use of JAVA operators and if, switch, for, while, and break and continue.

Thank you for reading.