for 语句是一种循环结构,其形式一般如下:
/* ① 初始条件 ② 循环条件(为 boolean 类型) ③ 循环体 ④ 迭代条件 */ for (①; ②; ④){ ③; }
在上述结构中,程序执行的过程如下:
① → ② → ③ → ④ → ② → ③ → ④ → … → ②
抄写老师的代码:
public class ForStatement { /** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ public static void main(String[] args) { forStatementTest(); }// Of main /** ********************* * Method unit test. ********************* */ public static void forStatementTest() { int tempN = 10; System.out.println("1 add to " + tempN + " is: " + addToN(tempN)); tempN = 0; // 从1加到0,不满足for循环中的条件,故不会执行语句,直接跳出循环 System.out.println("1 add to " + tempN + " is: " + addToN(tempN)); int tempStepLength = 1; tempN = 10; System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: " + addToNWithStepLength(tempN, tempStepLength)); tempStepLength = 2;//将步长设置为2,实现了将1-10中的奇数相加 System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: " + addToNWithStepLength(tempN, tempStepLength)); }// Of forStatementTest /** ********************* * Add from 1 to N. * * @param paraN The given upper bound. * @return The sum. ********************* */ public static int addToN(int paraN) { int resultSum = 0; for (int i = 1; i<= paraN; i++) { resultSum += i; }// Of for i return resultSum; }// Of addToN /** ********************* * Add from 1 to N with a step length. * * @param paraN The given upper bound. * @param paraStepLength The given step length. * @return The sum. ********************* */ public static int addToNWithStepLength(int paraN, int paraStepLength) { int resultSum = 0; for (int i=1; i <= paraN; i += paraStepLength) { resultSum += i; } // Of for i return resultSum; }// Of addToNWithStepLength } // Of class ForStatement
while 语句是另一种循环结构,一般有如下形式:
/* ① 初始条件 ② 循环条件(为 boolean 类型) ③ 循环体 ④ 迭代条件 */ ① while (②){ ③; ④; }
在上述结构中,程序执行的过程如下:
① → ② → ③ → ④ → ② → ③ → ④ → … → ②
跟写老师的代码:
package com.teachercode; public class WhileStatement { /** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ public static void main(String[] args) { whileStatementTest(); }// Of main /** ********************* * The sum not exceeding a given value. ********************* */ public static void whileStatementTest() { int tempMax = 5; int tempValue = 0; int tempSum = 0; // Approach 1 while (tempSum <= tempMax) { tempValue++; tempSum += tempValue; System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);//输出每次循环对应的tempValue和tempSum的值 }// Of while tempSum -= tempValue;//tempSum的值一旦大于tempMax就退出循环,故此时tempSum减去最后那一个tempValue才是不超过tempMax System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum); // Approach 2 System.out.println("\r\nAlternative approach."); tempValue = 0; tempSum = 0; while (true) {// 无限循环的格式,不确定循环多少次时使用 tempValue++; tempSum += tempValue; System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum); if (tempMax < tempSum) { break;//一旦tempSum的值大于tempMax,执行到break语句,就跳出循环 }// Of if }// Of while tempSum -= tempValue; System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum); }// Of whileStatementTest }// Of class WhileStatement
注意:
while (true)
:for(;;)
。这种形式在不明确循环次数的时候使用,一般通过循环内部的条件,来控制循环的结束。比如上面代码中,在不满足tempMax >= tempSum
这个条件时,就通过 break
跳出循环;