[hard study] Java notes 0.3

Java program flow control Procedure flow control Sequential structure Branching structure if statement switch Stateme...
Sequential structure
Branching structure
Cyclic structure

Java program flow control

Procedure flow control

Sequential structure

The program is executed line by line from top to bottom without any judgment or jump.

Branching structure

Selectively execute a piece of code according to the conditions.
if Two branch statements, else and switch.

if statement

//if(Boolean value) , when the Boolean value is True, the code in braces will be executed, and false will not // if(x++==2 & ++y==2) { // //x + + takes the value first and then calculates, 1 = = 2, the result is false, and then calculates the value of x after + + is 2 // //++The value of y is calculated first and then the value is taken. The value of y is calculated as 2 by + + first, then the logical judgment is 2 = = 2, and the result is true // // x=7; // } // System.out.println("x=" + x + ",y=" + y);

switch Statements

int i = 3; switch(i) { case 1:// 1 is equivalent to if (i == 1) System.out.println("Monday"); break; case 2:// 2 is equivalent to if (i == 2) System.out.println("Tuesday"); break; default://else System.out.println("I don't know what day of the week"); break; }


Cyclic structure

Execute a piece of code repeatedly according to the loop condition.
There are while, do There are three loop statements: while and for.
Note: after jdk1.5, foreach loop is provided for convenient traversal of set and array elements.

Loop statement function:
The function of repeatedly executing specific code when certain conditions are met

There are four components of a loop statement:
Initialization part (init_statement)
Loop condition part (test_exp)
body_statement)
Iterative part (alter_staerment)

1.for loop

for (initialization expression; Boolean test expression; change expression) Statement or statement block }

Narcissistic number for(int i = 100 ; i <= 999;i++) { //145/100=1;(145-1*100)/10=4;145-1*100-4*10=5 int m = i/100;//Get hundreds int n = (i-m*100)/10;//Get tens int k = i - m*100 - n*10;//Get single digits int res = m*m*m + n*n*n + k*k*k;//The sum of cubes in each digit if(res == i) {//Judge whether the number of Narcissus System.out.println(res); }

2.while loop

int i = 1; while(i <= 100) { //Get the calculation result of variable I < = 100, whether it is true or false. If it is true, execute the code in the while brace. If it is false, do not execute System.out.println(); i++;//Changing the value of i }

3.do while loop

int m = 1; do { System.out.println(m); m++; }while(m <= 100);

4. Nested loop

multiplication table for(int i = 0;i <= 9;i++) {//Every loop will execute all the code in braces for(int j = 0;j <= i;j++) {//The number of loops multiplied by the number of loops is the number of times the code is executed in the braces of the loops System.out.print(i + "*" + j + "=" + (i*j) + " ");//print does not wrap } System.out.println();//println wrap } //Note: when writing nested loops, try to ensure that the number of loops in the outer loop is less than that in the inner loop //Prime number //All prime numbers between 1-100 (prime number is a natural number greater than 1 and can only be divided by 1 and itself) for(int i = 1; i <= 100;i++) { int k=0;//The number of times to divide an integer. The variable is valid within the braces it is in for(int j = 1;j <= i;j++) {//Cycle 1 to i, moduli with the number between i and cycle 1 to i if (i % j == 0) { k++; } } if(k == 2) {//If the number of cycles is 2, i of the current large cycle is a prime number System.out.println(i); } }

5. Special process control statement

5.1 break

break statement
break statement is used to terminate the execution of a statement block

{ break; }

Break terminate the current cycle

5.2 continue

continue statement is used to skip one execution of a circular statement
When the continue statement appears in the multi nested loop statement body, you can indicate which level of loop to skip through the label
continue is to work on the current cycle. End the current cycle. The code under the current cycle will not execute and enter the next cycle directly

5.3 return

Return: it is not designed to end a loop. Its function is to end a method. When a method executes to a return statement, the method is terminated.

Unlike break and continue, return directly ends the entire method, no matter how many levels of loops the return is in

9 June 2020, 23:00 | Views: 4653

Add new comment

For adding a comment, please log in
or create account

0 comments