Example of a while loop that can't be written as a for loop

前端 未结 6 999
轮回少年
轮回少年 2021-01-18 08:10

I know a while loop can do anything a for loop can, but can a for loop do anything a while loop can?

Please provide an example.

6条回答
  •  爱一瞬间的悲伤
    2021-01-18 09:08

    The while loop and the classical for loop are interchangable:

    for (initializer; loop-test; counting-expression) {
        …
    }
    
    initializer
    while (loop-test) {
        …
        counting-expression
    }
    

提交回复
热议问题