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);
        }
    
    }
  • 相关阅读:
    入门OJ 1278【关系网络】
    HDU 1372【Knight Moves】
    ZOJ 1047【Image Perimeters】
    log4J——在Spring中的使用
    实用性很强的文章(不来源于博客园)
    详解AOP——用配置文件的方式实现AOP
    Spring与Web项目整合的原理
    IOC——Spring的bean的管理(注解方式)
    IOC——Spring的bean的管理(xml配置文件)
    关于XML文件
  • 原文地址:https://www.cnblogs.com/1218-mzc/p/12853180.html
Copyright © 2011-2022 走看看