Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

break in For Loop (have some issues)

So im currently using EPPLUS in C#, trying to get some data from an excel file. It’s working BUT, break; makes my head go "boom-boom". There are segments in my for loop that if i put the break; or get rid of it, stops my programm from going through and just freezes it. So i’m stuck at this segment of code and it’s just freezes my app. Can you help me somehow? (please dont call me stupid or anything like that, i’m learning)

for (int row = 2; row <= end.Row;)
{ // Row by row...
    for (int col = start.Column; col <= end.Column; col++)
    { // ... Cell by cell...
        string cellValue = ws.Cells[row, col].Text;

        if (String.Compare(cellValue, "Номер Машины", true) == 0)
        {
            for (int row_ = row + 1; row_ <= end.Row;)
            {
                for (int col_ = col; col_ <= end.Column;)
                {
                    string cellValue_ = ws.Cells[row_, col_].Text;
                    MessageBox.Show(cellValue_);


                    for (int row__ = row_ + 1; row__ <= end.Row;)
                    {
                        for (int col__ = col_; col__ <= end.Column;)
                        {
                            string cellValue__ = ws.Cells[row__, col__].Text;
                            if (cellValue__ != string.Empty)
                            {
                                MessageBox.Show(cellValue__);
                                break;
                            }
                            
                        }
                        break;
                    }
                    break;
                }
                break;
            }
            
        }
    }
    break;
}

I was playing with "break;" but it just made everything worse and thank God i had a backup

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

These breaks exit the current loop immediately. So, all of these outer loops will loop only once. You cannot exit all the loops from the innermost one using breaks.

This is rare case where using a goto statement makes sense:

// (Code simplified)
for (int row_ = row + 1; row_ <= end.Row; row_++) {
    for (int col_ = col; col_ <= end.Column; col_++) {
        for (int row__ = row_ + 1; row__ <= end.Row; row__++) {
            for (int col__ = col_; col__ <= end.Column; col__++) {
                if (cellValue__ != string.Empty) {
                    MessageBox.Show(cellValue__);
                    goto exit_all;
                }
            }
        }
    }
}
exit_all:

If you want to return a result from the inner-most loop, you can put this code into a method or local function and exit all the loops and the method with a return statement:

// (Code simplified)
string GetResult()
{
    for (int row_ = row + 1; row_ <= end.Row; row_++) {
        for (int col_ = col; col_ <= end.Column; col_++) {
            for (int row__ = row_ + 1; row__ <= end.Row; row__++) {
                for (int col__ = col_; col__ <= end.Column; col__++) {
                    if (cellValue__ != string.Empty) {
                        return cellValue__;
                    }
                }
            }
        }
    }
}

I also added all the missing loop_variable++ as @jmcilhinney and @RetiredNinja have pointed out in their comments. Note that not incementing the loop variables let’s the loops loop forever (unless they are exited with break). This is what caused the freeze.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading