preface
All codes are controlled by logical structure, including sequence structure, branch structure and loop structure. We have systematically studied the C language. This time, we will understand java from the perspective of Java.
Sequential structure
Sequential structure is the simplest logical structure, which is found in every code. For example:
public static void main(String[] args) { System.out.println(1); System.out.println(2); System.out.println(3); System.out.println(4); }
The printing results are: 1, 2, 3 and 4. If the front and rear positions are exchanged, the results will also be exchanged accordingly, such as:
public static void main(String[] args) { System.out.println(1); System.out.println(3); System.out.println(2); System.out.println(4); }
The print results are: 1, 3, 2, 4. The sequential results are compiled and run in the order of the code. Of course, we can also change the running order of the results through branches and loops.
Branch structure (select statement)
if () statement
The if () statement expression contains three cases -- single branch, double branch and multi branch.
Single branch
Form:
if(Boolean type expression){ Execute code if conditions are met; }
Double branch
Form:
if(Boolean type expression 1){ If the condition is met, execute code 1; }else{ Condition code execution code 2; }
Multi branch
if(Boolean type expression 1){ If the condition is met, execute code 1; }else if(Boolean type expression 2){ If the condition is met, execute code 2; }else{ If the condition is met, execute code 3; }
Note 1 - overhang else
Let's take an example:
public static void main(String[] args) { int x=10; int y=10; if(x==10) if(y==10) System.out.println("aaa"); else System.out.println("bbb"); }
From the structure of the code, we can see that else is aligned with the first if (x==10), but in the real sense, else is if(y==10) else, we need to know that else is not aligned with if, that is, the else of if. In the real sense, who is close to is who. So how can we avoid such problems? This requires us to cultivate that whenever any line of code needs to be enclosed, it needs to be written. Don't omit it for convenience. It often brings not convenience, but bug s, I think Adding parentheses can not only increase readability, but also facilitate later maintenance.
So the correct code should be as follows:
public static void main(String[] args) { int x=10; int y=10; if(x==10) { if (y == 10) System.out.println("aaa"); } else System.out.println("bbb"); }
Note 2 - code style
// Style 1 int x = 10; if (x == 10) { // Meet the conditions } else { // Conditions not met } // Style 2 int x = 10; if (x == 10) { // Meet the conditions } else { // Conditions not met }
In C language, we generally use the format of style 2 to write code. Although both methods are legal, style 1 is more recommended in Java, {on the same line of if / else.
Note 3 - semicolon problem
int x = 20; if (x == 10); { System.out.println("hehe"); }
In this code, we added; as a result, the if () statement is just a statement, so no matter what happens, it will be executed after if (). Therefore, the print result of this code is "hehe". This is also the advantage of writing this kind of code in style 1. In this way, we can avoid adding ";" after the if () statement, but add "{".
switch() statement
for instance:
public static void main(String[] args) { int a=10; switch (a){ case 1: System.out.println("1"); break; case 2: System.out.println("2"); break; default: System.out.println("Parameter mismatch"); break; } }
The switch statement has been emphasized in C language, but there are several points to note:
Note 1 - do not omit break, otherwise the effect of "multi branch selection" will be lost
for instance:
public static void main(String[] args) { int day = 1; switch(day) { case 1: System.out.println("Monday"); // break; case 2: System.out.println("Tuesday"); break; } }
We omit the break in case1, so after printing Monday, case1 does not jump out, but continues down and enters case2, so Tuesday still needs to be printed.
Therefore, we found that when we do not write break, the case statements will be executed downward in turn, thus losing the effect of multiple branches.
Note 2 - values in switch can only be integers | enumerations | characters | strings
This is also very different from C language. In C language, we know that switch () can only use integers in parentheses, but switch is different. Switch can only use integers (long is no good), enumerations, characters and strings. In other words, among the eight common basic types, long, double, boolean and float are no good.
Note 3 - switch cannot express complex conditions
What is a complex condition? For example:
public static void main(String[] args) { int num = 15; switch (num > 10 && num < 20){ case true: System.out.println("hehe"); case false: System.out.println("haha"); default: System.out.println("heihei"); } }
We find that the error will be reported because the expression in the parentheses of switch () is too complex. Therefore, we try not to use it if we can not use the switch statement.
Cyclic structure
while Loop
Basic grammatical form:
while(Cycle condition){ Circular statement; }
Let's take a simple example of calculating the sum of 1 to 10:
public static void main(String[] args) { int i = 1; int sum = 0; while (i <= 10) { sum = sum + i; i++; } System.out.println(sum); }
matters needing attention
- Similar to if, the statement under while may not write {}, but only one statement can be supported when it is not written. It is recommended to add {}.
- Similar to if, the {suggestion after while is written on the same line as while.
- Similar to if, do not write more semicolons after while, otherwise the loop may not execute correctly.
break
The function of break is to end the cycle ahead of time. For example:
Print the first number from 1 to 10 divided by 3:
public static void main(String[] args) { int i = 1; while (i <= 10) { if (i % 3 == 0) { System.out.println(i); break; } i++; } }
We can see that when i=3, the condition of i% 3 = = 0 is met. Then, print the value of i, break, jump out of the loop and end the program. If there is no break, i + + will continue until i > 10while, and the loop will stop.
continue
The function of continue is to skip this cycle and immediately enter the next cycle. For example:
Print the number from 1 to 10 divided by 3:
public static void main(String[] args) { int i = 1; while (i <= 10) { if (i % 3 != 0) { i++; continue; } System.out.println(i); i++; } }
When the i-3 remainder is not 0, i + + will be used first, and the loop will be skipped (if i continue first, then i + +, then i + + will not execute after continue, and the loop will be skipped directly). If the i-3 remainder is equal to 0, it will not enter the if () condition, but print the value of i in i + +.
for() loop
Basic grammatical form:
for(Expression 1;Expression 2;Expression 3){ Circulatory body; }
Let's look at a for loop that simply finds 1 to 100:
public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } System.out.println(sum); }
matters needing attention:
- Similar to if, the statement below for can not write {}, but only one statement can be supported when it is not written. It is recommended to add {}.
- Similar to if, the {suggestion after for is written on the same line as while.
- Similar to if, do not write more semicolons after for, otherwise the loop may not execute correctly.
do while() loop
Basic grammatical form:
do{ Circular statement; }while(Cycle condition);
Let's take a simple example of calculating the sum of 1 to 10:
public static void main(String[] args) { int i = 0; int sum = 0; do { sum = sum + i; i++; } while (i <= 10); System.out.println(sum); }
matters needing attention:
- Don't forget the semicolon at the end of the do while loop.
- Generally, do while is rarely used, and for and while are more recommended.
There are so many Java language designs for logic control. Most of us have been in contact with C language. If you have any suggestions or questions, you are welcome to send a private letter or leave a message. Thank you.