在for循环中初始化的变量是否会在每次迭代时重置?

时间:2014-11-14 00:47:41

标签: c++ initialization

for (i=0;i<=n;i++) {
       fsten[i]=fx(xsten[i]); //fsten[0] = fx(xsten[0]); fsten[1] = fx(xsten[1]); ...; etc. initializing the fsten array up to n times.
} //end of initial for loop

      y=0.0;

      for (i=0;i<=n;i++) {
       L=1.0; //the lagrange basis polynomial
           for (j=0;j<=n;j++) {
                if (i!=j) {
                 L=L*(x-xsten[j])/(xsten[i]-xsten[j]);
                } //end of if statement
           } //end of second for loop
       y=y+fsten[i]*L;
      }//end of first for loop

我正在进行拉格朗日多项式迭代。我们正在研究y = 0.0之后的第二个for循环。在使用j=0的for循环结束时,我们有y = y+fsten[i]*L,其中L显然不再是1。但当它转到i=1时,是否意味着L = 1.0再次成立?

1 个答案:

答案 0 :(得分:1)

是的,它再次成立,因为您再次运行循环体,L = 1.0确实将变量L设置为1.0

相关问题