Java Basic Road (iv)--flow control statement

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.