日撸 Java 三百行(06天,For语句)

日撸 Java 三百行(06天,For语句)

注意:这里是JAVA自学与了解的同步笔记与记录,如有问题欢迎指正说明


一、关于循环语句

循环语句是计算机编程中计算大数据的内容的优势之处,是同时也是简化基础编程过程的重要方法。
java的循环语句使用依旧继承类C语言体系,具体循环语句有:
1).while 循环
2).do…while 循环
3).for 循环
今天主要涉及for循环的使用。for循环在while循环的基础上扩充了固定的初始化部分和递增部分,令定长增长的循坏代码编写变得简单。
基本格式如下:

for(初始化; 布尔表达式; 更新) {
    //代码语句
}

二、相关循环模拟的编写内容

本测试依旧通过编写函数来模拟每次算法代码核心,同时也将简单的打印操作与赋值操作交由另一个函数完成。main函数只负责简单的驱动作用。
核心for循环函数构造:

	/**
	 *********************
	 * 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

public class HelloWorld {
	
	public static void main(String[] args) {
		System.out.println("Hello, World !");
	}//of main
}//of class HelloWorld

解释:
这里分别构造的两个函数循环的操作都是求1到N和的操作,分别使用了默认的步长1的递增for循环,以及自定义步长值的for循环操作。
应用的方式是求两个和的算式:
1). 1到N以步长为1求和
在这里插入图片描述
2). 1到N以步长为p求和
在这里插入图片描述

三、数据测试

代码如下

package basic;

/**
 * This is the sixth code. Names and comments should follow my style strictly.
 * 
 * @author Xingyi Zhang 1328365276@qq.com
 */

public class ForStatement {

	/**
	 *********************
	 * The entrance of the program
	 * 
	 * @param args Not used now.
	 *********************
	 */
	public static void main(String args[]) {
		forStatementTest();
	}// Of main

	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
}// Of class ForStatement

正常显示如下:
在这里插入图片描述
注意,这里你可能发现1到0求和不难道是0吗?其实不然,循环有个基本条件,就是循环因子要小于等于限定的N才能进入循环,而我们的代码循环因子是从1开始的,而这里第二案例N=0,不满足循环的条件,自然是没有循环的发生,答案也自然是0。
其余答案带入上述的公式中可以很快验证其正确性。


总结

循环是编写程序的一个非常基本的能力,灵活利用各种循环的嵌套和循环的组合是一个程序员的基本功。for循环在必要的时候可以略去部分功能以提高代码的全体可读性,例如在一个变量因子增加随着本身内部参数变动而变动的案例中,可以用for(int i = X;i<=P;){ }来略去固定的递增(递减)

· 补充 -加强的for循环

另外在Java5 引入了一种主要用于数组的增强型 for 循环。
格式如下:

for(声明语句 : 表达式)
{
   //代码句子
}

这种增强就如同当年C++的C++11扩充一样,无论是使用方法上还是效果上都是类似的。
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配(即与表达式中的数据结构集合中的每个子项的类型匹配)。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
口说无凭,计算机人还是要以“show me the code !”才行

public class Test {
   public static void main(String[] args) {
   		int[] numbers = { 10, 20, 30, 40, 50 }; // Create a int array

		for (int x : numbers) { // Get the elements in the array in order
			System.out.print(x);
			System.out.print(",");
		}
		System.out.print("\n");
		String[] names = { "James", "Larry", "Tom", "Lacy" }; // Create a string array
		for (String name : names) {
			System.out.print(name);
			System.out.print(",");
		}
   }
}

显示结果如下:
在这里插入图片描述
另外通过实际测试,发现了java5的这种增强for与C++11的增强for一个相同点与差异点:

1.相同:for(val : array)的遍历之下的每个val值是仅仅可读的,但是不可写,val只能用来获取而无法通过修改val来修改array对于中的数据。用计算机的话来说,读取的val不是这个array中那个对应数的地址,因此修改val无法改动到array。这个C++与java一致。

2.不同:C++可以利用关键符“&”,用for(&val : array)的形式直接读取array对于元素的地址,这样可以直接修改array中对应的值,而这点java目前我还没有查到可以做到的方法(若有欢迎指正!)


可见相比于一般的for循环,这个方法能更快实现我们的遍历任务,避免我们对于一个未知长度的数组还要进行获取长度并再用循环因子进行索引遍历的麻烦。 但是也得辩证看待这个问题,对于某些需要用遍历因子完成的算法,基本的for又会简单一些,所以不要图简单就一味使用,要针对具体问题具体分析。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值