Can a for loop be written to create an infinite loop or is it only while loops that do that?

前端 未结 12 1607
时光取名叫无心
时光取名叫无心 2021-01-17 23:58

Can a for loop be written in Java to create an infinite loop or is it only while loops that cause that problem?

12条回答
  •  我在风中等你
    2021-01-18 00:47

    Ofcourse for loops can cause infinite loops. An example is:

    for(int i = 0; i < 99; i /= 2){ ... }

    Because i is never incremented, it will stay in the body of the for loop forever until you quit the program.

提交回复
热议问题