如何在每 1000 个值后停止无限循环?
How can I stop an infinite loop after every 1000 values?
/* 我想看 short 的最大值 int.I 想用循环看它 only.So 我创建了一个无限 loop.The 问题是我想停止循环要查看每 1000 values.But 后的值,它仅在循环达到 1000 时才停止,因为我已经给它停止使用 if 条件,那么它永远不会 stops.What 我可以这样做,以便它在每 1000 后停止values.Program 下面给出*/
#include<stdio.h>
#include<conio.h>
void main()
{
//Short int is declared intentionally
short int d=0;
char i;
//Infinite loop is created intentionally
for(d=0; ;d++)
{
//printing value
printf("\n%d",d);
//stopping a loop when value reaches 1000
if(d==1000)
{
//continuing after pressing a character after 1000
printf("\n press i to continue");
scanf("%d",&i);
continue;
}
}
//see the output
getch();
}
您使用错误的格式说明符来扫描 char
值。它调用 undefined behavior。你应该改变
scanf("%d",&i);
到
scanf(" %c",&i);
也就是说,要在 每 1000 次迭代停止,您需要将 if(d==1000)
更改为 if( (d % 1000) == 0)
。
FWIW,
void main()
至少应该 int main(void)
,以符合标准。
- 有符号整数类型溢出又是UB。你不应该依赖它。使用
<limits.h>
检查范围(如果有)。
您只需检查条件如下
非常重要的一点是你必须与零进行比较,它会正常工作。
if( (d%1000 == 0)
{
//continuing after pressing a character after 1000
printf("\n press i to continue");
scanf("%c",&i);
continue;
}
希望对您有所帮助
你需要改变
if(d==1000)
至
if!(d%1000)
循环将在每 1000 个值后停止。
#include<stdio.h>
#include<conio.h>
void main()
{
//Short int is declared intentionally
short int d=0;
char i;
//Infinite loop is created intentionally
for(d=0; ;d++)
{
//printing value
printf("\n%d",d);
//stopping a loop when value reaches 1000
if(d%1000 == 0 && d != 0)
{
//continuing after pressing a character after 1000
printf("\n press any key to continue...");
scanf("%c",&i);
continue;
}
}
//see the output
getch();
}
并且您使用了错误的格式说明符 (See)。 %c
表示单个字符。
scanf("%d",&i);
这条语句应该是scanf("%c",&i);
。条件将是 if(d%1000 == 0 && d != 0)
.
/* 我想看 short 的最大值 int.I 想用循环看它 only.So 我创建了一个无限 loop.The 问题是我想停止循环要查看每 1000 values.But 后的值,它仅在循环达到 1000 时才停止,因为我已经给它停止使用 if 条件,那么它永远不会 stops.What 我可以这样做,以便它在每 1000 后停止values.Program 下面给出*/
#include<stdio.h>
#include<conio.h>
void main()
{
//Short int is declared intentionally
short int d=0;
char i;
//Infinite loop is created intentionally
for(d=0; ;d++)
{
//printing value
printf("\n%d",d);
//stopping a loop when value reaches 1000
if(d==1000)
{
//continuing after pressing a character after 1000
printf("\n press i to continue");
scanf("%d",&i);
continue;
}
}
//see the output
getch();
}
您使用错误的格式说明符来扫描 char
值。它调用 undefined behavior。你应该改变
scanf("%d",&i);
到
scanf(" %c",&i);
也就是说,要在 每 1000 次迭代停止,您需要将 if(d==1000)
更改为 if( (d % 1000) == 0)
。
FWIW,
void main()
至少应该int main(void)
,以符合标准。- 有符号整数类型溢出又是UB。你不应该依赖它。使用
<limits.h>
检查范围(如果有)。
您只需检查条件如下
非常重要的一点是你必须与零进行比较,它会正常工作。
if( (d%1000 == 0)
{
//continuing after pressing a character after 1000
printf("\n press i to continue");
scanf("%c",&i);
continue;
}
希望对您有所帮助
你需要改变
if(d==1000)
至
if!(d%1000)
循环将在每 1000 个值后停止。
#include<stdio.h>
#include<conio.h>
void main()
{
//Short int is declared intentionally
short int d=0;
char i;
//Infinite loop is created intentionally
for(d=0; ;d++)
{
//printing value
printf("\n%d",d);
//stopping a loop when value reaches 1000
if(d%1000 == 0 && d != 0)
{
//continuing after pressing a character after 1000
printf("\n press any key to continue...");
scanf("%c",&i);
continue;
}
}
//see the output
getch();
}
并且您使用了错误的格式说明符 (See)。 %c
表示单个字符。
scanf("%d",&i);
这条语句应该是scanf("%c",&i);
。条件将是 if(d%1000 == 0 && d != 0)
.