流程控制语句-------循环结构概述和for语句的格式及其使用(三)

1.循环结构的分类
   for            while             do...while
2.B:循环结构for语句的格式:
        for(初始化表达式;条件表达式;循环后的操作表达式) {
            循环体;
        }
3.执行流程:
    * a:执行初始化语句
    * b:执行判断条件语句,看其返回值是true还是false
        * 如果是true,就继续执行
        * 如果是false,就结束循环
    * c:执行循环体语句;
    * d:执行循环后的操作表达式
    * e:回到B继续。

4.在控制台输出10次"helloworld".

不使用for循环:

class Demo1_For {
	public static void main(String[] args) {
		
		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循环:

class Demo1_For {
	public static void main(String[] args) {
		
		for (int i = 1;i <= 10;i++ ) {  
	           System.out.println("helloworld");
		}
		

	}
}


 

猜你喜欢

转载自blog.csdn.net/mqingo/article/details/81541914