control statements in Java
classification of control statements in Java
There are several control statements in Java
(1) Branch statements
A. If–else (conditional statement)
B. Switch (multi-branch selection statement)
(2) Loop statement
A. While Loop B. Do–while loop C. For loop
(3) Other statements related to the program
A. Break statement B. Continue statement C. Return statement
Conditional statements
The conditional statement here refers to the specific format of the If–else statement as follows:
Format One
IF Statement Format
if (condition) {
EXECUTE statement
}
Format Two
If-else Statement Format
If (condition) {
EXECUTE statement
}else{
EXECUTE statement
}
Format three
If-else if-else if format
If (condition) {
EXECUTE statement
}else if (condition) {
EXECUTE statement
}else if (condition) {
EXECUTE statement statement
}
Looping statements
There are three types of loop statements in Java, namely:
while (type loop)
Do while (until type loop)
for (when type loop)
1.while Loop statement
Syntax format
while (loop condition) {
EXECUTE statement
}
Note : When the value of the loop condition is true, execution statements within curly braces are executed, and if the loop condition is false, the loop does not execute if the first loop condition is false
2.do while loop
Syntax format for Do-while loops
do{
EXECUTE statement
}while (cyclic conditions);
description : note while (); to add a ";", it is different from the while loop. The while loop detects the loop condition first, so its loop body may not be executed. If we want the loop body to be executed at least once, Then use the Do-while loop. This loop executes the statement first, detects the loop condition, then repeats the statement, detects the loop condition, and so on;
3.for Cycle
The For loop is the most common form of looping, and the syntax format is
for (initialized value; loop condition; change loop condition) {
EXECUTE statement
}
such as for (inti = 0 i< i++) {
System.out.print (i);
}
Three elements of the cyclic program are written: the cyclic variable assigns the initial value, the cyclic condition, the cyclic variable increment;
When the For statement executes, the initialization operation is performed first, and then the statement of the change loop condition is executed, and then the statement in the loop body is executed, if the termination condition is met, and then the statements in the cycle are performed. After completing a cycle, the termination condition is re-judged;
initialization, termination, and iteration parts can be empty statements (but semicolons cannot be omitted) when a certain three are empty, equivalent to an infinite loop;
A good statement can be used in both the initialization and iteration sections to perform multiple operations. All good statements use a comma-delimited sequence of statements;
Concepts of Java Control statements