Java foundation of the cycle

There are cycles in life, such as the cycle of life and work, in class, such as duplicate check passwords, but there are also recycling program in the world.

Features loop:
repeat something similar, but there are termination condition.
Like this case, Java already provides related technology solutions for us.
for while do while

A, For cycle
1 syntax:
for (initialization statement; conditional statement; conditional control statements) {
loop statement;
}

2. Perform flow
a, performs initialization statement
B, performs conditional statement, to see the result is true or false
If false, the end of the cycle;
if it is true, proceed;
C, execution loop statement
d, performs conditional control statements
e back to 2 continue
Write pictures described here

public static void main(String[] args){

        //循环,重复做事情
        /*
            for(初始化语句;条件判断语句;条件控制语句) {
                循环体语句;
            }
            初始化语句:从哪开始 1
            条件判断语句:到哪结束 10
            条件控制语句:怎么从开始走到结束的
            循环体语句:重复做的事情
        */
        //定义在for循环里面的变量,其作用域就仅限在for循环里面

        for(int i = 1;i<=1;i++){
            System.out.println(i+":只要我努力,就一定能成功!!!");
        }

        //System.out.println(i);//101
        //10---1
        for(int i=10;i>=1;i--){
            System.out.println(i);
        }




    }

Summary:
for loop characteristics suitable for the following cycle conditions:
1. regular
2. repeating
3 cycles foreseeable

Two, While loop
1.while loop format:
syntax
initialization statement;
the while (judgment condition statement) {
loop statement;
controlling conditional statement;
}

//while 循环次数是不可预见

        //我的梦想,
        /*
        int target = 10000000;
        //关系表达式?表达式1:表达式2
        int month = target%3000==0?target/3000:target/3000+1;
        System.out.println(month);
        */
        //如果每个月都比上个月多存1000,又怎么求解
        //month--需要存几个月
        /*
        int month = 0;
        //目前存的钱
        int sum = 0;
        //初始的基数
        int basic = 3000;
        while(sum<10000000){
            sum += basic;
            month++;
            //下个月存的基数会比上个月多1000
            basic += 1000;
        }

        System.out.println(month);
        */

Three, do While loop
do ... while loop format:
initialization statement:
do {
loop statement;
controlling conditional statement;
} the while (judgment condition statement);

//do-while
        //案例一:实现可以无限次录入学员信息的功能,通过指令来控制是否继续(y/n)
        //重复做的事:录入学员信息的功能
        //循环的条件:输入的指令是y
        /*
            do{
                重复做的事
                条件控制语句
            }while(条件);
        */
        /*
        Scanner input = new Scanner(System.in);
        int isContinue = 0;
        do{
            System.out.println("请输入学员信息");
            System.out.println("模拟正在录入。。。");

            System.out.println("是否继续录入?(y/n 1/0)");
            isContinue = input.nextInt();
        }while(isContinue == 1);

        System.out.println("欢迎下次继续使用");
        */

Fourth, the nested loop

public static void main(String[] args){
        /*
            打印一个3行4列的长方形,如下图。逐步来实现
            ****
            ****
            ****
        */

        for(int j=1;j<=8;j++){//外层循环:控制行数
            for(int i=1;i<=6;i++){//内层循环:控制列数
                System.out.print("*");
            }
            System.out.println();
        }

        }

五、break,continue,return

break: out of the current loop
continue: the end of the current cycle continue to the next cycle
return: transfer function is returned to the main function to continue, may be provided with a return value returned.

Published 31 original articles · won praise 4 · Views 3524

Guess you like

Origin blog.csdn.net/qq_29074261/article/details/78770480