Home > Java > javaTutorial > Several looping methods in Java

Several looping methods in Java

(*-*)浩
Release: 2019-03-30 09:35:05
Original
5396 people have browsed it



This article mainly introduces several loop methods in java, so that everyone can have more knowledge about loop structures. Understanding allows everyone to be more comfortable in the learning process without reporting bugs.

Several looping methods in Java

    1. for loop

    for loop is the most commonly used loop method in java and its structure Is:

    for (initialization; Boolean expression; update condition) {

              Loop body code

    }

    实例:1到100的和?
    public class Test{
        public static void main(String[] args){
            int n=0;
            for(int i=1;i<=100;i++){
                n=n+i;
            }
            System.out.println(n);
        }
    }
    
    实例:嵌套循环,九九乘法表
    public class Test{
        public static void main(String[] args){
            //后执行外循环
            for(int i=1;i<=9;i++){
                //先执行内循环
                for(int j=1;j<=i;j++){
                    Systeam.out.print(j+"*"+i+"="+i*j+" ");
                }
                System.out.println();
            }
        }
    }
    Copy after login

    2.while loop

    The while loop is mainly used when the number of loops is unknown. Its structure is:

    while(Boolean expression){

    Loop body code

    }

    实例:1到100的和?
    public class Test{
        public static void main(String[] args){
            int j=0;
            int i=1;
            int n=101;
            //如果i<n为true就会一直循环下去
            while(i<n){
                j=j+i;
                i++;
            }
            System.out.println(j);
        }
    }
    Copy after login

    3.do...while loop

    do...while loop is similar to while loop, but different It is a Boolean expression that will execute the loop code at least once even if there is an error. Its structure is:

    do{

    Loop body code

    }while(Boolean expression Formula)

    实例:1到100的和?
    public class Test{
        public static void main(String[] args){
            int j=0;
            int i=1;
            int n=101;
            do{
                j=j+i;
                i++;
            }while(i<n);
            System.out.println(j);
        }
    }
    Copy after login

    4. Enhanced for loop

    After Java5, a loop mainly used for arrays was introduced. Its structure is:

    for (Declaration statement: expression) {

    /* Declaration statement: Declare a new local variable. The type of the variable must match the type of the array element. Its scope is limited to the loop statement block, *its value is equal to the value of the array element at this time.

    * Expression: The expression is the name of the array to be accessed, or a method that returns an array. */

    # Circulation code

    }

    实例:数组循环
    public class Test{
        public static void main(String[] args){
            int[] num={1,3,5,6,8,};
            for(int x : num){
                System.out.print(x+",");
            }
            String[] color={"红","黄","蓝","绿","紫"};
            for(String name : color){
                 System.out.print(name+",");
            }
        }
    }
    Copy after login

    This article is all over here, more other exciting content can follow PHP Chinese websiteJava video tutorial column!



The above is the detailed content of Several looping methods in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template