variable declaration within the while loop C/C++

后端 未结 5 1658
失恋的感觉
失恋的感觉 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:51

    The variable i that checks the condition is the one you declared in main() not the one inside the loop.

    Both are different variables you are confusing them as one, the compiler doesn't get confused as easily as you were.

    Inside the loop i refers to the one you declared inside the { } but outside the { } the i refers to the one declared in main()

提交回复
热议问题