c - a c 迭代,同时在 do while 循环中跳过给定值

标签 c

我在 C 中有一个非常简单的问题。我正在尝试编写一个简单的程序,输出 10100 之间的 10 的倍数, inclusive(即:在闭区间 [10,100] 上)跳过 3070 并垂直输出值。

这是我的代码:

#include<stdio.h>
main()
{
  int i=10;
  do {
    if(i==30||i==70)
      continue; 

    printf("\n %d",i);
    i++;
  } while(i<100);

  return 0;
}

程序在 29 处停止,跳过 30 并继续进入永无止境的循环。怎么了?

最佳答案

问题是,当您点击 if 语句时,您现在正在跳过 i 的增量。所以你永远达不到 100!

#include<stdio.h>
main()
{
  int i=10;
  do {
    if(i==30||i==70)
      continue;        //!!!! This will skip the i increment

    printf("\n %d",i);
    i++;
  } while(i<100);

  return 0;
}

我推荐一个 for 循环:

main()
{
  for (i = 10; i < 100; i++) {
    if(i==30||i==70)
      continue;          // The for loop will do the i++ on the "continue"

    printf("\n %d",i);
  } 

  return 0;
}

关于c - a c 迭代,同时在 do while 循环中跳过给定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21030780/

相关文章:

将 libc 回溯转换为源代码行号

c - 我如何控制 free() 函数正常工作?

c - 初始化的字符数组指向同一内存

c - 如何判断 STDIN 是否绑定(bind)到套接字,在 C 中

c - 如何在C中的牛顿拉夫森法中检测循环

c - 使用多个自定义信号处理程序在程序中阻止 SIGCHILD

c - 为什么fgetc函数要加回车

c - 将特定位写入二进制 header

c++ - 从我的 llvm pass 发出 llvm-ir 字节码

c - 我如何计算输出