while循环C / C中变量声明++ [英] variable declaration within the while loop C/C++

查看:406
本文介绍了while循环C / C中变量声明++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   main()
   {
   int i=3;       
   while(i--)
    {
      int i=100;
      i--;
      printf("%d..",i);
    }
   }

它输出 99..99..99

但根据我,每次控制进入while循环,它获得价值100.so它永远不会达到零它应该运行无限次。
只是尝试我换成 INT I = 100; I = 100; 侧while循环,现在它运行无限times..WHY ???

but according to me it should run infinite times as every time control enters while loop it gets value 100.so it will never reach zero. just to experiment i replaced int i=100; with i=100; in side the while loop and now it runs infinite times..WHY???

推荐答案

变量 I ,检查条件是你在主要申报之一()不是循环内的之一。

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.

在循环内部 I 是指您在 { } <宣布一/ code>但外面的 { } I 指的是一个在的main()

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

这篇关于while循环C / C中变量声明++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆