when to use while loop rather than for loop

前端 未结 14 1034
清酒与你
清酒与你 2020-12-06 02:59

I am learning java as well android. Almost everything that we can perform by while loop those things we can do in for loop.

I found a simple condition where using w

14条回答
  •  误落风尘
    2020-12-06 03:16

    You can do something like:

    int counter;
    for (counter = 0; counter < 10; ) {
        //do some task
        if(some condition){
            break;
        }
    }
    useTheCounter(counter);
    

    Anything that a while-loop can do, can also be done in a for-loop, and anything a for-loop can do, can also be done in a while-loop.

提交回复
热议问题