Basic Java syntax and java syntax
The last time we talked about basic type conversion and some common operators in Java, today we will talk about the basic syntax in Java.
Basic Java Syntax:
Condition Statement ------ execute different statements based on different conditions;
If
If .. else
If .. else if
If... else... if... else
Switch
Loop statement ----- repeats some actions;
For (JDK1.5 syntax)
While
Do... while;
If statement:
If
If .. else
If .. else if
If.. else if... else if;
If.. else if. else if. else;
Only one statement to be executed can be omitted {} but not recommended;
Do not add ";" behind if ";";
For Loop statement:
For (expression 1; expression 2; expression 3) {statement ;...;}
For Loop statement example:
Output 101 ~ Prime number within 200:
While & do while statement:
While statement:
While (logical expression) {statement ;...;}
Do while statement:
Do while {statement;...;} while (logical expression );
Break & continue
The break statement is used to terminate execution of a statement block in the loop statement body. For example:
Result:
The continue statement is used in the loop body to terminate a loop,
Skip the loop that is not executed under the continue statement in the loop, for example;
Result:
Switch Statement (conditional statement supplement ):
Switch (){
Case xx:
......
Case xx:
......
Case xx:
.....
}
Note: We recommend that you use the break statement for case-based penetration;
Default can be omitted, but not recommended;
The values in switch "()" must be int type, but byte, short, and char can be automatically converted to int type.