This time we are going to talk about the looping statements in Java.
There are three kinds of circular statements: 1, For2, While3, Do--while.
The tasks of the three loop statements are different, and the methods are different. Of course their respective flowcharts are not the same.
3.1 while Statement
The syntax of the while is as follows:
while (expression) {statement block;}
3.2 Do-while Statement
The Do-while statement first executes the loop body and then evaluates the conditional expression, which has the following grammatical form:
do{statement block;}while (conditional expression);
Note: Do it first and then judge
3.3 for Statement
The For statement is suitable for repeating a predetermined number of iterations of a statement block with the following syntax:
for (expression 1; expression 2; expression 3) {statement block;}
Note: The For statement is divided into three cases where the expression 1 or 2 or 3 is empty, regardless of the missing part of the expression, can be supplemented elsewhere in the program, thus maintaining the integrity of the FOR Loop statement, so that the loop is normal.
3.4 foreach Statement
The foreach statement is a simplified version of the for statement, but the foreach statement cannot completely replace for and report, however, the foreach statement can be rewritten as a version of the for statement. It is important to note that foreach is not a keyword, but it is customary to refer to this special for statement as a foreach statement.
The foreach statement is a great way to iterate over arrays, collections, and for developers, with syntax in the following format:
For (type variable name: Collection) {statement block;}
Sample code
public class Program {public static void main (String [] args) {string[] fruits = {"Apple", "orange", "banana", "watermelon", "pear", "other"}; System.out.println ("Hot-selling fruits are:"); for (String fruit:fruits) {System.out.println (fruit+ ",");}} }
Note: The Do--while statement executes at least once.
A conditional expression in the dead loop of the dead loop while and Do-while is a Boolean expression.
for (;;).
If you have comments or suggestions, please give your valuable comments, thank you for your support, your support will encourage me to continue to create!
Java Basic Road (iv)--flow control statement