日撸代码300行学习笔记 Day 6

1.基本for语句

顺序结构的程序语句只能被执行一次。如果您想要同样的操作执行多次,就需要使用循环结构,在这里我们主要是讲解for语句的使用。

1.1代码演示

package basic;

/**
 * This is the sixth code. Names and comments should follow my style strictly.
 * 
 * @author WU JUN 2298320301@qq.com.
 */
public class Day6 {
	/**
	 *********************
	 * The entrance of the program.
	 * 
	 * @param args
	 *            Not used now.
	 *********************
	 */
	public static void main(String args[]) {
		test();
		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;
		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;
		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

	// For nested test
	public static void test() {
		int num = 0;

		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= 6; j++) {
				num++;
			} // Of for j
		} // Of for i
		System.out.println("For nested test: " + num);

	}// Of test
}// Of class ForStatement

上述主要演示了for语句的基本用法,代码中我们编写了两个方法,并且对两个方法分别使用了两组数据来进行测试。首先是addToN方法,该方法传入的参数作为我们for语句的一个限制循环的条件,当i小于等于传入的参数时就可以继续重复for循环中的语句,否则就跳出for循环继续执行后面的程序。然后就是addToNWithStepLength方法,该方法需要传入两个参数,第一个参数仍然是for循环的限制条件,而第二个参数表示的是for循环每循环一次i的值就增加多少,如第一个测试用例tempStepLength=1则循环一次i就加一,第二个测试用例tempStepLength=2则循环一次i就加二。

1.2运行结果

 2.总结

对于基本的单层for循环还是比较容易理解的,只要我们理解到for语句中的循环条件机制是如何运行的如在什么条件下才能够进行执行循环语句,而又在什么时候就要跳出循环语句。如果我们想要进一步深入的学习for语句,我们可以尝试一下for语句的嵌套循环。在上述代码中,我创建了一个test的函数用于测试两层for循环语句的嵌套使用,第一层for循环实现了五次循环,第二层循环实现了六层循环。两层循环的关系是外面for循环每循环一次内部的for循环就需要循环五次,最后的输出结果为总的循环次数,外层五次内层六次相乘结果等于30。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值