---
title: "Groovy的循环"
author: "ceasarje"
date: "2019-05-09T15:03:10Z"
tags: ["Groovy"]
categories: ["编程语言"]
slug: "groovy"
draft: true
lastmod: "2019-05-09T15:03:10Z"
id: 2440792
url: "https://ceasarje.iteye.com/blog/2440792"
---

1、for-each循环。
(1) 使用冒号形式的for循环，必须指定name的类型。
names = ["Michael", "James", "Kavin", "Steven"]
for (String name : names) {
	println name
}
返回
Michael
 James
 Kavin
 Steven

(2) 使用in形式的for循环
for (name in names) {
	println name
}
返回
Michael
 James
 Kavin
 Steven

2、整型方法循环
(1) 范围
for (i in 1..3) {
    println i
}
返回
1
 2
 3

(2) upto和downto方法
1.upto(3) {
	println it
}
3.downto(1) {
	println it
}
返回
1
 2
 3
 3
 2
 1
(3) times方法
3.times {
	println it
}
返回
0
 1
 2
(4) step方法
1.step(7, 2) {
	println it
}
返回
1
 3
 5