【问题标题】:WHILE and DO-WHILE loop not executing and gets stuck at the output [closed]WHILE 和 DO-WHILE 循环未执行并卡在输出[关闭]
【发布时间】:2022-01-22 06:46:59
【问题描述】:

FOR 循环正在执行,但是对于 whiledo-while 没有执行此代码,并且在我输入输入后它卡住了,请帮助我,我使用 VS Code 作为我的 IDE,编译器 -- GNU GCC。

P.S :- 我正在 Arch linux 上执行此代码。

P.S.S :- 虽然此代码在 Windows 10 上有效执行。


    #include <stdio.h>
    int main() {
    int i,f = 1,n;
    printf("Enter a number :: ");
    scanf("%d",&n);
    i = 1;
    do
    {
        f *= i;
    }while (i <= n);
     printf("The Factorial of %d is  %d\n\n",n,f);
    return 0;
}

【问题讨论】:

  • 循环如何退出? i 是如何到达 n 的?
  • 循环体中in 都没有变化,因此条件始终为真
  • Do-While 循环内,in 都没有改变。
  • 循环没有退出,因为我说它在输入任何数字输入后卡住了:- 5
  • 哦,对了,我忘了加i++。谢谢你帮助我

标签: c while-loop do-while


【解决方案1】:
    do
    {
        f *= i;
    }while (i <= n);

ni 没有任何变化。所以如果i &lt;= n是不定式循环,否则在第一次比较时退出。

【讨论】:

    最近更新 更多