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

Does a variable initialized in a for loop get reset every iteration?

本文关键字:迭代 是否 循环 for 初始化 变量      更新时间:2023-10-16
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 循环。在 for 循环的末尾,j=0,我们有 y = y+fsten[i]*L L显然不再1。但是当它进入i=1这是否意味着 L=1.0 再次为真?

是的,这又是正确的,因为您再次运行循环的主体,并且L = 1.0确实将变量L设置为 1.0