2
votes

I've been tasked in a homework assignment with converting a loop in C# into Fortran 95.

outerLoop:
  for(row = 0; row < numRows; rows++){
    for(col = 0; col < numCols; col++){
      if(mat[row][col] == 0)
        continue outerLoop;
      sum += mat[row][col];
    }
  }

As some of you can see, this looks similar to the exit statement label specification used in Java and Perl, which, to my understanding, are used to break out of loops that have nested loops or 'if' statements rather than just a single loop/statement. I'm still new to this feature in Java, so I'm not sure if it exists anywhere else, specifically in C# and Fortran 95.

I've looked around on Google, but I haven't found anything for it. I have a bad time formulating search terms to use on Google, so that factors into it, as well.

Please note: I'm not looking for a handout answer; I'm just looking for where to find the answer myself.

Thank you for taking time out to read this post.

2
I think you're looking for goto: msdn.microsoft.com/en-us/library/13940fs2.aspx - Gabe
I don't know what continue outerLoop does exactly, but if you place a break in that place, it will break the inner loop and allow the outer loop to continue. - Cheeso
You really want to read about jump statements in C#- msdn.microsoft.com/en-us/library/d96yfwee.aspx - RichardOD
The question is unclear. Is it about converting the (invalid) C# to Fortran or if the (Java?) code has a corresponding direct C# counterpart? See "...converting a loop in C# into Fortran 95." - user166390
Given the likely outcome of this snippet, the Fortran keyword is probably HALT_AND_CATCH_FIRE. - Hans Passant

2 Answers

3
votes

For examples of loop-control constructs in Fortran 95, see my example code in the previous Fortran question, which has example uses of "cycle" and "exit": Reading comment lines correctly in an input file using Fortran 90, .

1
votes

I believe such a break x feature is not supported in C#. You would need to break out of the nested loops manually using flags.

bool stop = false;
for (row = 0; row < numRows; rows++)
{
    for (col = 0; col < numCols; col++)
    {
        if (mat[row][col] == 0)
        {
            stop = true;
            break;
        }

        sum += mat[row][col];
    }

    if (stop)
        break;
}

Simpler alternatives include the goto statement (which is widely frowned upon); and encapsulating your loop within a dedicated method, in which case, you could just return when you want to stop iterating.