variable declaration within the while loop C/C++

后端 未结 5 1660
失恋的感觉
失恋的感觉 2020-12-17 06:24

according to me following while loop should be infinite but it runs only thrice

   main()
   {
   int i=3;       
   while(i--)
    {
      int i=100;
               


        
5条回答
  •  太阳男子
    2020-12-17 06:55

    The variable i in while(i--) is different from the variable i defined inside the loop.

    Basically int i = 100 shadows the previous int i = 3 and inside the block of the while you're referring to a new variable.

    At the end of the day, I find no plausible scenario where you would need to do something like this.

提交回复
热议问题