for循环while while循环+附加条件
我有一个类似于此的循环。for循环while while循环+附加条件
int total1, total2;
for (total1 = fsize(myfile);;) {
total2 = fsize(myfile);
...
...
total1 = total2;
}
我想要做的是停止循环之前,将其转换为一个while
循环并检查额外的条件。
我愿做这样的事情:
while((total1 = fsize(myfile)) && input = getch() != 'Q') {
total2 = fsize(myfile);
...
total1 = total2;
}
感谢
您可以使用为:
for(total1 = fsize(myfile); (input = getch()) != 'Q';) {
...
}
您可以*总是*使用“for”代替一段时间。只需将“while”改为“for”,并在条件前后添加分号。 – 2010-07-20 22:28:28
@ T.E.D .:当然。我试图猜测OP正在寻找的答案 – 2010-07-20 22:37:34
在while循环测试的条件的for循环total1=fsize(myfile)
已成为部分的“初始化”的一部分。这是你的意图吗?
你确定你不想这样......
total1 = fsize(myfile);
while((input = getch()) != 'Q') {
total2 = fsize(myfile);
...
total1 = total2;
}
在for循环的初始化只执行一次。该while
相当于
for (total1 = fsize(myfile);;) {
是
total1 = fsize(myfile);
while (1) {
你提到添加条件input = getch() != 'Q'
。
注意分配(=
)比对照(!=
)较低的优先级,所以分配到getch()
和input
检查该字符不是Q
你需要括号围绕assignement:
total1 = fsize(myfile);
while ((input = getch()) != 'Q') {
有这里有个问题吗? – bstpierre 2010-07-20 22:15:49
继续前进。你应该将括号括起来((input = getch())!='Q')'。 – AShelly 2010-07-20 22:16:15
其实出于某种原因,它不会进入循环...所以我不太确定我是否正确地执行while循环。 – 2010-07-20 22:17:21