zoukankan      html  css  js  c++  java
  • java-while循环

    while 循环 用于重复程序的一部分几次或重复执行一个代码块。如果迭代次数不固定,建议使用while循环。

    语法:

    while(condition){
        //code to be executed
    }
    //实例:
    package day02;

    public class Test12 {

        /**
         * @param args
         * @author bindu
         */
        public static void main(String[] args) {
            int i = 1;
            while (i <=10){
                System.out.println(i);
                i++;
            }
        }

    }

    无限while循环:

    package day02;
    
    public class Test12 {
    
        /**
         * @param args
         * @author bindu
         */
        public static void main(String[] args) {
            while(true){
                System.out.println("这是个死循环!");
            }
        }
    
    }

    do-while 循环:用于多次迭代程序的一部分或重复多次执行一个代码块。如果迭代次数不固定,必须至少执行一次循环,建议使用do-while 循环

    do{
      //code to be executed.
    }
    while(condition);//后置条件检查
    //实例:
    package day02;

    public class Test12 {

        /**
         * @param args
         * @author bindu
         */
        public static void main(String[] args) {
            int i =1;
            do{
                System.out.println(i);
                i++;
            }while(i<10);
        }

    }

    do-while 死循环:

    package day02;
    
    public class Test12 {
    
        /**
         * @param args
         * @author bindu
         */
        public static void main(String[] args) {
            int i =1;
            do{
                System.out.println(i);
                i++;
            }while(true);
        }
    
    }
  • 相关阅读:
    Jump Game II
    Trapping Rain Water
    First Missing Positive
    Median of Two Sorted Arrays
    noip2012开车旅行 题解
    AC自动机专题总结
    初探数位DP
    斯坦纳树 [bzoj2595][wc2008]游览计划 题解
    [bzoj3244][noi2013]树的计数 题解
    网络流模型小结
  • 原文地址:https://www.cnblogs.com/1218-mzc/p/12853180.html
Copyright © 2011-2022 走看看