Java basics 3-basic statements and java3-statements
1.Condition StatementA conditional statement is a selective code execution.
Including the if and switch condition statements.
If Condition Statement:
Syntax:
(1) if (Condition Statement ){
// Code to be executed when the condition is set
}
(2) if (Condition Statement ){
} Else {
}
(3) if (Condition Statement ){
} Else if (){
}....
(4) if (){
} Else if (){
} Else {
}
Note:
Else if and else cannot appear separately. They must match if !!!
Switch Condition Statement, Syntax:
Switch (expression ){
Case Constant Value: Operation statement
Break;
Case Constant Value: Operation statement
Break;
}
In the switch Condition Statement, the expression value can only be byte, short, char, int, but in JDK, you can use String.
2. Arithmetic Operators
+,-, *,/, %, ++ ,--
++ And -- features:
++ Adds 1 before output, ++ adds 1 after output
-- First subtract 1 and then output, -- then output and then subtract 1
3. Value assignment operator
Is to assign the value to a variable.
=, + =,-=, * =,/=. % =
+ = Indicates that two variables are added and assigned to another variable.
For example: int num1 = 10; int num2 = 20; num1 + = num2; it is equivalent to num1 = num1 + num2;
4. Comparison Operators
>,<, >=, <=, == ,! =
5. logical operators
Logical operators are used to calculate the expressions of boolean results. The calculation results are always boolean.
&:
|: Or
^: Exclusive or description: When the expressions on both sides are consistent, the output result is false; otherwise, the output result is true.
! : The opposite result.
&: Short Circuit and
If the first condition is false, the result is directly output.
|: Short circuit or
If the first condition is true, the result is directly output.
6. + join Operators
Any data type connected by a plus sign (+) is a new string.
7. Three-object Operator
Syntax:
X? Y: Z
A result of the X expression must be of the boolean type. If the value of X is true, the value of Y is output; otherwise, the value of Z is output.
8. Expression
The result obtained by performing operations on the operands in the expression is called the value of the expression. The data type of the expression value is the expression type.
9. Loop statements
(1). for Loop
(2). while Loop
(3). do/while LOOP
For Loop Syntax:
For (initialization variable; cycle condition; changing the cycle condition ){
}
While loop Syntax:
While (loop condition ){
Change cycle conditions
}
Do/while LOOP Syntax:
Do {
Change cycle conditions
} While (Cyclic condition );
Note: do/while will be executed at least once no matter whether the condition is true or not.
10. break and conitune
- The break statement is used to terminate the execution of a statement block.
- The continue statement is used to skip one execution of a loop statement block.