美文网首页
java while循环

java while循环

作者: 万万_936b | 来源:发表于2019-04-19 15:55 被阅读0次

while常用于循环接收指令,解析指令后或继续或停止循环,常用continue,break,return来控制。
continue:继续下一次循环;
break:停止循环,向下继续执行;
return:跳出所在函数;
为了更好理解三个关键字的含义,结合代码理解:
1.两循环在同一个函数中

public class WhileTest extends Thread {

    @Override
    public void run() {
        int count = 1;
        Log.d("ThreadTest", Thread.currentThread().getName() + "线程开始");
        while (true) {
            Log.d("ThreadTest", "循环A:" + count);
            if (count == 1000) {
                count = 2;
            }
            Log.d("ThreadTest", "循环B1:" + count);
            while (true) {
                Log.d("ThreadTest", "循环B2:" + count);
                count = count * 10;
                if (count == 100) {
                    Log.d("ThreadTest", "循环B continue:" + count);
                    continue;
                }
                Log.d("ThreadTest", "count:" + count);
                if (count == 1000) {
                    Log.d("ThreadTest", "循环B break:" + count);
                    break;
                }

                if (count == 2000) {
                    Log.d("ThreadTest", "循环B return:" + count);
                    return;
                }
            }
            Log.d("ThreadTest", "循环A终止count:" + count);
        }
    }
}
结果:
ThreadTest: getName:Thread-9703
ThreadTest: Thread-9703线程开始
ThreadTest: 循环A:1
ThreadTest: 循环B1:1
ThreadTest: 循环B2:1
ThreadTest: count:10
ThreadTest: 循环B2:10
ThreadTest: 循环B continue:100
ThreadTest: 循环B2:100
ThreadTest: count:1000
ThreadTest: 循环B break:1000
ThreadTest: 循环A终止count:1000
ThreadTest: 循环A:1000
ThreadTest: 循环B1:2
ThreadTest: 循环B2:2
ThreadTest: count:20
ThreadTest: 循环B2:20
ThreadTest: count:200
ThreadTest: 循环B2:200
ThreadTest: count:2000
ThreadTest: 循环B return:2000

return之后会跳出整个函数即run方法。
2.循环B移入新建的函数

public class WhileTest extends Thread {

    private int count = 1;
    @Override
    public void run() {
        Log.d("ThreadTest", Thread.currentThread().getName() + "线程开始");
        while (true) {
            Log.d("ThreadTest", "循环A:" + count);
            if (count == 1000) {
                count = 2;
            }
            if (count ==2000) {
                Log.d("ThreadTest", "循环A2000终止count:" + count);
                break;
            }
            test();
            Log.d("ThreadTest", "循环A终止count:" + count);
        }
    }

    private void test() {
        Log.d("ThreadTest", "循环B1:" + count);
        while (true) {
            Log.d("ThreadTest", "循环B2:" + count);
            count = count * 10;
            if (count == 100) {
                Log.d("ThreadTest", "循环B continue:" + count);
                continue;
            }
            Log.d("ThreadTest", "count:" + count);
            if (count == 1000 || count == 0) {
                Log.d("ThreadTest", "循环B break:" + count);
                break;
            }

            if (count == 2000) {
                Log.d("ThreadTest", "循环B return:" + count);
                return;
            }
        }
    }
}
结果:
ThreadTest: getName:Thread-9727
ThreadTest: Thread-9727线程开始
ThreadTest: 循环A:1
ThreadTest: 循环B1:1
ThreadTest: 循环B2:1
ThreadTest: count:10
ThreadTest: 循环B2:10
ThreadTest: 循环B continue:100
ThreadTest: 循环B2:100
ThreadTest: count:1000
ThreadTest: 循环B break:1000
ThreadTest: 循环A终止count:1000
ThreadTest: 循环A:1000
ThreadTest: 循环B1:2
ThreadTest: 循环B2:2
ThreadTest: count:20
ThreadTest: 循环B2:20
ThreadTest: count:200
ThreadTest: 循环B2:200
ThreadTest: count:2000
ThreadTest: 循环B return:2000
ThreadTest: 循环A终止count:2000
ThreadTest: 循环A:2000
ThreadTest: 循环A2000终止count:2000

return之后会跳出新函数,但会继续执行循环A。

相关文章

网友评论

      本文标题:java while循环

      本文链接:https://www.haomeiwen.com/subject/bygqgqtx.html