简体   繁体   English

在do while循环中跳过给定值时进行ac迭代

[英]a c iterations while skipping given values in a do while loop

I have a very simple problem in C. I am trying to write a simple program that outputs multiples of 10 between 10 and 100 , inclusive (ie: on the closed interval [10,100] ) that skips 30 and 70 and outputs the values vertically. 我在C.一个非常简单的问题我试图编写一个简单的程序,其输出的倍数10之间10100 ,包括端值(即:在闭区间[10,100]该跳过3070并垂直输出该值。

Here is my code: 这是我的代码:

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

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

  return 0;
}

The program stops at 29 skips 30 and continues into a never ending loop. 程序在2930处停止并继续进入永无止境的循环。 What is wrong? 怎么了?

The problem is that when you hit the if statement, you are now skipping the increment of i . 问题在于,当您单击if语句时,现在正在跳过i的增量。 So you never reach 100! 因此,您永远不会达到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;
}

I recommend a for loop: 我推荐一个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;
}

mbratch correctly pointed out your problem, but you might want to consider a for loop for this sort of thing. mbratch正确指出了您的问题,但是您可能需要考虑使用for循环来处理此类问题。 It would have prevented this particular problem, since the increment is automatic. 因为增量是自动的,所以本可以避免这个特殊的问题。

I won't do the whole thing for you, since you're obviously trying to learn, but this ought to get you started: 我不会为您做全部,因为您显然正在尝试学习,但这应该可以帮助您入门:

for (i=0; i<100; i+= 1)

You'll have to change some of the numbers in that line, but hopefully you'll understand what they mean when you change them. 您必须更改该行中的一些数字,但是希望您会理解更改它们时的含义。

When i reaches 30 the continue statements moves back to the start of the loop. i达到30岁时,continue语句将返回到循环的开始。

And so the loop continues endlessly as i is not incremented from this point. 因此循环不断地继续,因为i没有从这一点开始增加。

Your code's doing exactly what it's written to do. 您的代码完全按照编写的方式执行。 The continue skips the increment instruction, so the value hits 30 and gets stuck there. 因为continue跳过了增量指令,所以该值达到30并停留在该位置。 Move the increment to the start of the loop body, or better yet, use a for instead of a while . 将增量移动到循环主体的开头,或者更好的是,使用for代替while

It loops forever because you continue but don't increment i. 它永远循环,因为您continue但不增加i。

if(i==30||i==70) {
  i++;
  continue;   
}

or you could use a for loop like so, 或者您可以像这样使用for循环,

#include<stdio.h>
int main()
{ 
  int i=10;

  for (; i < 100; i++)
  {
    if(i==30 || i==70) {
      continue;
    }
    printf("\n %d",i);
  }

  return 0;
}

The reason is that i is never incremented after 30 inside the body of do..while. 原因是在do..while内, i从不增加30。 You'd need to increment it. 您需要增加它。

if (i == 30 || i == 70){
    i++;
    continue;
}

Don't use continue . 不要使用continue Instead print out the value as long as != to 30 and 70 . 而是只要!=3070打印该值。 Also iterate by 10 instead of 1 to output multiples 10 . 还以10而不是1进行迭代以输出10倍数。

#include<stdio.h>
main()
{
    int i = 10;

    do
    {
        if (i != 30 && i != 70)
            printf("\n %d", i);

        i += 10;

    }
    while (i <= 100); // if you want to print 100

    return 0;
}

Output: 输出:

 10
 20
 40
 50
 60
 80
 90
100

Use while (i <= 100); 使用while (i <= 100); if you need to also print 100 . 如果您还需要打印100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM