for loop to while loop

后端 未结 4 1001
北海茫月
北海茫月 2021-01-17 01:53

I am using a for loop to print the backwards alphabet in uppercase but I would like to know how to do the same thing with a while loop, which I am

4条回答
  •  广开言路
    2021-01-17 02:19

    for (initialization; condition; increment) {
    
    }
    

    is same as:

    {
        initialization;
        while(condition) {
           // body
           increment;
        }
    }
    

    The outer block creates a block scope for the initialized parameter, that we get in a for loop also. But, if you are declaring your for-loop variable outside the loop, then you can omit that outer block.

    Now you can map your for loop to the while loop.

提交回复
热议问题