标签:
//for循环
package cn.itcast.work4;
/*
* A:循环结构的分类
* for,while,do...while 
* B:循环结构for语句的格式:
* 
	for(初始化表达式;条件表达式;循环后的操作表达式) {
		循环体;
	}
* C执行流程:
* a:执行初始化语句
* b:执行判断条件语句,看其返回值是true还是false
	* 如果是true,就继续执行
	* 如果是false,就结束循环
* c:执行循环体语句;
* d:执行循环后的操作表达式
* e:回到B继续。
* D:案例演示
* 在控制台输出10次"helloworld"
*/
class For {
public static void main(String[] args) {
	//在控制输出10次helloworld,这样做不推荐,因为复用性太差
	/*System.out.println("helloworld");
	System.out.println("helloworld");
	System.out.println("helloworld");
	System.out.println("helloworld");
	System.out.println("helloworld");
	System.out.println("helloworld");
	System.out.println("helloworld");
	System.out.println("helloworld");
	System.out.println("helloworld");
	System.out.println("helloworld");*/
	for (int i = 1;i <= 10 ;i++ ) {
		System.out.println("helloworld");
	}
}
}
运行结果:

//while循环
package cn.itcast.work4;
/*
* A:循环结构while语句的格式:
* 		
		while循环的基本格式:
		while(判断条件语句) {
			循环体语句;
		}
		
		完整格式:
		
		初始化语句;
	    while(判断条件语句) {
			 循环体语句;
			 控制条件语句;
		}
* B:执行流程:
	* a:执行初始化语句
	* b:执行判断条件语句,看其返回值是true还是false
		* 如果是true,就继续执行
		* 如果是false,就结束循环
	* c:执行循环体语句;
	* d:执行控制条件语句
	* e:回到B继续。
* C:案例演示
	* 需求:请在控制台输出数据1-10
*/
class While {
	public static void main(String[] args) {
		int x = 1;
		while (x <= 10) {
			System.out.println("x = " +  x);
			x++;
		}
	}
}
运行结果:

//do_while循环
package cn.itcast.work4;
/*
* A:循环结构do...while语句的格式:
* 
		do {
			循环体语句;
		}while(判断条件语句);
		
		完整格式;
		初始化语句;
		do {
			循环体语句;
			控制条件语句;
		}while(判断条件语句);
* B:执行流程:
	* a:执行初始化语句
	* b:执行循环体语句;
	* c:执行控制条件语句
	* d:执行判断条件语句,看其返回值是true还是false
		* 如果是true,就继续执行
		* 如果是false,就结束循环
	* e:回到b继续。
* C:案例演示
	* 需求:请在控制台输出数据1-10
*/
class DoWhile {
	public static void main(String[] args) {
		//while 和do while的区别
		/*int i = 11;
		do {
			System.out.println("i = " + i);
			i++;
		}
		while (i <= 10);
		
		System.out.println("---------------------");
		int j = 11;
		while (j <= 10) {
			System.out.println("j = " + j);
			j++;
		}*/
		/*for (int i = 1;i <= 10 ;i++ ) {
			System.out.println("i = " + i);
		}
		//System.out.println("i = " + i);			for语句执行后变量会被释放,不能再使用
		System.out.println("-------------------");
		int i = 1;
		while (i <= 10) {
			System.out.println("i = " + i);
			i++;
		}
		System.out.println("-------------------");
		System.out.println("i = " + i);				//while语句执行后,初始化变量还可以继续使用*/
		//while语句的无限循环
		/*while (true) {
			System.out.println("hello world");
		}*/
		//System.out.println("hello world");
		//for语句的无限循环
		for (; ; ) {
			System.out.println("hello world");
		}
	}
}
运行结果:

//循环嵌套之九九乘法表
package cn.itcast.work4;
/*
* A:案例演示
	* 需求:在控制台输出九九乘法表。
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
...
*
**
***
*/
class For_99 {
	public static void main(String[] args) {
		for (int i = 1;i <= 9 ;i++ ) {					//行数
			for (int j = 1;j <= i ;j++ ) {				//列数
				System.out.print(j + "*" + i + "=" + (i * j) + "\t" );
			}
			System.out.println();
		}
		//System.out.println("\"");				转义双引号
		//System.out.println(‘\‘‘);				//转义单引号
	}
}
运行结果:

标签:
原文地址:http://www.cnblogs.com/rcy2012/p/5798838.html