Trying If statement

Trying If statement

本文关键字:statement If Trying      更新时间:2023-10-16

此if语句不起作用。

有人能告诉我为什么下面的代码不起作用,并帮助我修复吗?

i=0;
while(1)
{
   if(counter2 < storeanother[0].size())
      break;
 int j=0;
  while(1)
 {
   if(j < handler)
      break;
     outputFile2 << storeanother[0][counter2] << "   +   " << storeanother[1][counter2] << " =   " << z80[i].get(j) << endl;
     counter2+=1;
      j++;
 }
 i++;
}
   outputFile2.close();

您反转条件

for (init; cond, post) { body;}

相当于

init;
while (true) {
    if (!cond) { // and not simply cond
        break;
    }
    body;
    post;
}

甚至

init;
while (cond) {
    body;
    post;
}

所以

if(counter2 < storeanother[0].size())
    break;

应该是

if (counter2 >= storeanother[0].size())
    break;

并且对于另一个循环类似。

如果你用错误的条件破坏了while,你必须将其反转。例如:

if(j == handler)
    break;

因为在for循环中,当条件为true时,只有循环运行在这两种情况下,条件都成立了,所以它只是脱离了循环
把它换成反面,效果会很好。
像这样:

i=0;
// for (int i = 0; counter2 < storeanother[0].size(); i++)
while(1)
{
   if(counter2 >= storeanother[0].size())
      break;
 int j=0;
 //for (j = 0; j < handler; j++)
  while(1)
 {
   if(j >= handler)
      break;
     outputFile2 << storeanother[0][counter2] << "   +   " << storeanother[1][counter2] << " =   " << z80[i].get(j) << endl;
     counter2+=1;
      j++;
 }
 i++;
}
   outputFile2.close();

除了其他人提到的if条件错误之外,我看不出有任何理由使用

while(1)
{
    if(j >= handler)
        break;
    // do the stuff

而不是

while(j < handler)
{
    // do the stuff

我的猜测是你正试图这样做:

int i=0;
while(counter2 < storeanother[0].size())
{
    for (int j = 0; j < handler; j++)
    {
        outputFile2 << storeanother[0][counter2] << "   +   " <<streanother[1][counter2] << " =   " << z80[i].get(j) << endl;
        counter2+=1;
    }
    i++;
}
outputFile2.close();

第二部分:

i=0;
// for (int i = 0; counter2 < storeanother[0].size(); i++)
while(counter2 < storeanother[0].size()))
{
 int j=0;
 //for (j = 0; j < handler; j++)
  while(j < handler)
 {
     outputFile2 << storeanother[0][counter2] << "   +   " << storeanother[1][counter2] << " =   " << z80[i].get(j) << endl;
     counter2+=1;
      j++;
 }
 i++;
}
   outputFile2.close();