循环语句是指满足特定条件下,程序重复做某个动作的行为。
一:for循环语句(确切知道循环次数或者循环次数较少)
(1)标准结构
for(变量初始化;变量满足条件;变量变化规律){
语句;
}
(2)死循环结构
for(;;){
语句;
}
(3)多变量for循环语句
for(int i=0,j=100;i<j;i++,j++){
语句;
}
(4)基本结构
for(int i=0;i<3;i++){
System.out.print("Hoole Java");//语句
}
该程序表示,定义一个整型变量i,并且i初始化为1;满足i<3的情况下运行语句,运行结束后,i自增1。直至i<3条件不成立,跳出循环。
所以该程序为
i=0;
执行语句。
i=1;
执行语句。
i=2;
执行语句。
<pre name="code" class="javascript">package cn.java.lhb.syntax;
/**
*
* @author lhb
*
*/
public class ForDemo {
public static void main(String[] args) {
for (int i = 0; i <= 100; i++) {
System.out.print(i + "\t");
}
}
}
该程序输出结果为:0,1,2,3.....99,100
(5)循环套用条件语句
for(变量初始化;变量满足条件;变量变化规律){
if(boolean的值或表达式){
语句;
}
}
该结构表示,在for内,满足if条件时,执行语句。package cn.java.lhb.syntax;
/**
*
* @author lhb
*
*/
public class ForDemo {
public static void main(String[] args) {
for (int i = 0; i <= 100; i++) {
if (i % 2 == 0) {
System.out.print(i + "\t");
}
}
}
}
该程序输出结果为:0,2,4.....98,100
二:while循环语句(确切知道循环次数或者循环次数较少)
(1)标准结构
while(变量满足条件){
语句;
变量变化规律;
}
(2)基本结构
while(i<3){
System.out.print("Hoole Java");//语句
i++;
}
该程序表示,定义一个整型变量i,并且i初始化为1;满足i<3的情况下运行语句,运行结束后,i自增1。直至i<3条件不成立,跳出循环。
所以该程序为
i=0;
执行语句。
i=1;
执行语句。
i=2;
执行语句。
package cn.java.lhb.syntax;
/**
*
* @author lhb
*
*/
public class WhileDemo {
public static void main(String[] args) {
int i = 0;
while (i <= 100) {
System.out.print(i + "\t");
i++;
}
}
}
该程序输出结果为:0,1,2,3.....99,100
(3)循环套用条件语句
while(变量满足条件){
if(boolean的值或表达式){
语句;
}
变量变化规律;
}
该结构表示,在while内,满足if条件时,执行语句。package cn.java.lhb.syntax;
/**
*
* @author lhb
*
*/
public class WhileDemo {
public static void main(String[] args) {
int i = 0;
while (i <= 100) {
if (i % 2 != 0) {
System.out.print(i + "\t");
}
i++;
}
}
}
该程序输出结果为:0,2.4.....98,100
三:do......while循环语句(至少需要执行一次的循环)
(1)标准结构
do{
语句;
变量变化规律;
}while(变量满足条件);
(2)基本结构
do{
System.out.print("Hoole Java");//语句
i++;
}while(i<3);
该程序表示,定义一个整型变量i,并且i初始化为1;执行语句,然后判断是否i<3,满足条件继续执行语句,运行结束后,i自增1。直至i<3条件不成立,跳出循环。
所以该程序为
i=0;
执行语句。
i=1;
执行语句。
i=2;
执行语句。
package cn.java.lhb.syntax;
/**
*
* @author lhb
*
*/
public class WhileDemo {
public static void main(String[] args) {
int i = 0;
do {
System.out.print(i + "\t");
i++;
} while (i <= 100);
}
}
(3)循环套用条件语句
while(变量满足条件){
if(boolean的值或表达式){
语句;
}
变量变化规律;
}
该结构表示,在do.....while内,满足if条件时,执行语句。package cn.java.lhb.syntax;
/**
*
* @author lhb
*
*/
public class WhileDemo {
public static void main(String[] args) {
int i = 0;
do {
if (i % 2 == 0) {
System.out.print(i + "\t");
}
i++;
} while (i <= 100);
}
}
该程序输出结果为:0,2.4.....98,100
四:循环语句的应用:
穷举法是一种“笨办法”,通过程序运行所有的可能,找出需要找出的情况。package cn.java.lhb.syntax;
/**
*
* @author lhb
*
*/
public class CenturyBuyChickenDemo {
public static void main(String[] args) {
for (int a = 0; a < 101; a++) {
for (int b = 0; b < 101; b++) {
for (int c = 0; c < 101; c++) {
if ((a + b + c == 100) && (5 * a + 3 * b + c / 3 == 100)
&& (c % 3 == 0)) {
System.out.println("百元共买了" + a + "只公鸡," + b + "只母鸡,"
+ c + "只小鸡。");
}
}
}
}
}
}
方法2:运用两重循环找出所有可能,用if条件语句判断并输出。
package cn.java.lhb.syntax;
/**
*
* @author lhb
*
*/
public class CenturyBuyChickenDemo {
public static void main(String[] args) {
int x, y, z;
for (x = 0; x <= 100; x++) {
for (y = 0; y <= 100; y++) {
z = 100 - x - y;
if (5 * x + 3 * y + z / 3.0 == 100) {
System.out.println("百元共买了" + x + "只公鸡," + y + "只母鸡," + z
+ "只小鸡。");
}
}
}
}
}
方法1与方法2程序输出结果均为:
百元共买了4只公鸡,18只母鸡,78只小鸡。
百元共买了8只公鸡,11只母鸡,81只小鸡。
百元共买了12只公鸡,4只母鸡,84只小鸡。