---
title: "浅谈while ; do-while ; for ; fro : ;"
author: "aiyanbo"
date: "2009-08-12T20:29:39Z"
tags: []
slug: "while-do-while-for-fro"
draft: false
lastmod: "2009-08-27T23:28:07Z"
id: 445872
url: "https://aiy.iteye.com/blog/445872"
---

while 适用于不定数循环

for 适用于定数循环，两者可以交换使用

do-while 先执行一次循环体，再判断是否继续判断

for : 适用于遍历

int i=0;
do{
    System.out.println("我是艾彦波");
    i++;
}while(i<=-10);
//循环1次
用while 代替 do-while
 int j=0;
 int k=0;
 while(j<=k){
      System.out.println(j+"我是艾彦波");
      if(true){//放表达式
        	k=-10;
      }
        	j++;
 }
//循环一次
同样原理,for循环也可以代替do-while 循环

for循环与for : 循环

int num=new int{1,2,3,4,5,6}

for(int i=0;i<num.length;i++;){
    System.out.println(num*);
}
这时可以用for : 代替for 循环
for(int i : num){
    System.out.println(i);
}