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

本文详细介绍了Java中的while循环,包括代码演示、解析和运行结果。通过一个实例展示了while循环的用法,强调了如何理解循环条件,并提到了在循环中使用break语句跳出循环的情况。总结部分指出while循环在实际编程中应用广泛,是理解循环语句的重要部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.while语句

while 循环会在指定条件为真时循环执行代码块,其作用与先前讲述的for语句的作用很相似,在今天的内容也会提及前面所讲的break语句跳出循环的用法。

1.2代码演示

package basic;

/**
 * This is the ninth code. Names and comments should follow my style strictly.
 * 
 * @author WU JUN 2298320301@qq.com.
 */
public class Day9 {
	/**
	 *********************
	 * 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 = 100;
		int tempValue = 0;
		int tempSum = 0;

		// Approach 1.
		while (tempSum <= tempMax) {
			tempValue++;
			tempSum += tempValue;
			System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);
		} // Of while
		tempSum -= tempValue;

		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;
			} // Of if
		} // Of while
		tempSum -= tempValue;

		System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);

		// Approach 3.
		System.out.println("\r\nAlternative approach.");
		tempValue = 0;
		tempSum = 0;
		do {
			tempValue++;
			tempSum += tempValue;
			System.out.println("tempValue = " + tempValue + ", tempSum = " + tempSum);

		} while (tempSum <= tempMax);// Of while
		tempSum -= tempValue;

		System.out.println("The sum not exceeding " + tempMax + " is: " + tempSum);
	}// Of whileStatementTest
}// Of class WhileStatement

1.2代码解析

首先我们从main函数入手,主函数中只有一个whileStatementTest函数,所以接下来我们跳转进入whileStatementTest函数进行分析。首先whileStatementTest函数没有传入参数且没有返回值,大致看一下该函数的整体主要是三个while循环。第一个while的循环条件是当tempSum <= tempMax时就执行第一个while循环内部的程序,然后while内部是参数的累加和循环打印输出语句这些没有什么太大的难度,如果实在理解不了可以用昨天将的带值计算的方法来理解该循环。第二个while循环作用都是与第一个是一样的只不过是写法不一样而已,这个while循环的循环条件为true所以此循环如果就这样写的话就是一个无限循环的状态。但是我们在while循环中加入了跳出循环的判断条件,其中的if语句当满足tempMax < tempSum时就能够执行break语句从而跳出循环。第三个循环是do while语句循环,这个循环只不过是while循环的另外一种写法而已,但是我们需要注意的地方是该循环至少会执行一次,即使条件为 false 它也会执行一次,因为代码块会在条件被测试前执行。

1.3 运行结果

tempValue = 1, tempSum = 1
tempValue = 2, tempSum = 3
tempValue = 3, tempSum = 6
tempValue = 4, tempSum = 10
tempValue = 5, tempSum = 15
tempValue = 6, tempSum = 21
tempValue = 7, tempSum = 28
tempValue = 8, tempSum = 36
tempValue = 9, tempSum = 45
tempValue = 10, tempSum = 55
tempValue = 11, tempSum = 66
tempValue = 12, tempSum = 78
tempValue = 13, tempSum = 91
tempValue = 14, tempSum = 105
The sum not exceeding 100 is: 91

Alternative approach.
tempValue = 1, tempSum = 1
tempValue = 2, tempSum = 3
tempValue = 3, tempSum = 6
tempValue = 4, tempSum = 10
tempValue = 5, tempSum = 15
tempValue = 6, tempSum = 21
tempValue = 7, tempSum = 28
tempValue = 8, tempSum = 36
tempValue = 9, tempSum = 45
tempValue = 10, tempSum = 55
tempValue = 11, tempSum = 66
tempValue = 12, tempSum = 78
tempValue = 13, tempSum = 91
tempValue = 14, tempSum = 105
The sum not exceeding 100 is: 91

Alternative approach.
tempValue = 1, tempSum = 1
tempValue = 2, tempSum = 3
tempValue = 3, tempSum = 6
tempValue = 4, tempSum = 10
tempValue = 5, tempSum = 15
tempValue = 6, tempSum = 21
tempValue = 7, tempSum = 28
tempValue = 8, tempSum = 36
tempValue = 9, tempSum = 45
tempValue = 10, tempSum = 55
tempValue = 11, tempSum = 66
tempValue = 12, tempSum = 78
tempValue = 13, tempSum = 91
tempValue = 14, tempSum = 105
The sum not exceeding 100 is: 91

2.总结

今天所讲的while循环语句总体来说是比之前讲到的for循环语句是要好理解一些的,我在平时写代码的时候经常用到的也是for循环语句。不管是for语句还是while语句在对其进行分析时应该先想清楚语句的循环条件,在知道循环条件之后再通过带值计算的方式来理解整个循环语句的原理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值