2011-10-13 74 views

回答

1

使用if的条件知道该数字是否小于或不。如果是的话,只需在里面使用break声明,这会让你走出循环。 Learn more about break statement from MSDN.

+0

@GregS:正确。但是,让我们不要勺喂OP。让他从链接或其他来源阅读关于“break”声明的想法后再想一想。 – Mahesh

+0

好的,我和你在一起。删除。 –

1

那么,什么是负数?这是一个小于零的数字(称为x),或者象征性地为x < 0。如果x小于零,那么这是真的。如果不是,那么它是错误的。

你可以无限循环,当满足此条件时突破:

while (1) { 
    if (x < 0) { 
    break; 
    } 

    ... 
} 

但我更愿意只使用条件相反的while循环本身:

while (x >= 0) { 
    ... 

虽然条件是真的,那么循环继续。当它是假的(并且你的原始条件是真的,因为这两个是相反的),循环中断。

0

不要将变量定义为无符号变量。正如其他答案中所建议,使用if语句并与if语句断开。

例如:

int main(void) 
{ 
int i; 
while(1){ 
/*here write an statement to get and store value in i*/ 
    if(i<0) 
    { 
    break; 
    } 
/*other statements*/ 
    return(0); 
    } 
} 
1
int i = -1; 
do 
{ 
    i = magic_user_input(); 
    //simple enough? get a decent programming book to get the hang of loops 
} 
while(i > -1) 

编辑:对不起,我的错误: '我' 未正确申报:)现在它应该是蛮好用的