when to use while loop rather than for loop

前端 未结 14 1040
清酒与你
清酒与你 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:18

    int counter = 0;
    while (counter < 10) {
        //do some task
        if(some condition){
            break;
        }
    }
    useTheCounter(counter); // method which use that value of counter do some other task
    

    Hi I repeat your code because it is incorrect. You forget to increase your counter so it will remains on 0

    int counter = 0;
    while (counter < 10) {
      //do some task
      if(some condition){
          break;
      }
      counter++;
    }
    useTheCounter(counter); // method which use that value of counter do some other task
    

提交回复
热议问题