none
How can I fix my loop inside another loop? D: RRS feed

  • Question

  • Hello ^^

    I have the following loop that I cannot seem to get working properly:

                //Assign Weeks
                for (int colIndex = 2; colIndex <= 44; colIndex= colIndex+6)
                {
                    for (int weekCount = 1; weekCount < colIndex; weekCount = weekCount + 1)
                    oSheet.Cells[6, colIndex] = "Week" + weekCount;
                }

    I'm trying to increment the colIndex until it reaches 44 and weekCount until it reaches 43.

    Starting at cell 6,2 it should insert "Week1", add 6 to the column, then insert "Week2" until it finally reaches Week43.

    Somehow running this I got Week1 Week7 Week13 Week19 and so on in that gibberish manner. If anyone out there is awesome and knows how to fix this, I would be very appreciative of your help.

    I was trying to replace this:

                //oSheet.Cells[6, 2].Value2 = "Week 0";
                //oSheet.Cells[6, 8].Value2 = "Week 1";
                //oSheet.Cells[6, 14].Value2 = "Week 2";
                //oSheet.Cells[6, 20].Value2 = "Week 3";
                //oSheet.Cells[6, 26].Value2 = "Week 4";
                //oSheet.Cells[6, 32].Value2 = "Week 5";
                //oSheet.Cells[6, 38].Value2 = "Week 6";
                //oSheet.Cells[6, 44].Value2 = "Week 7";
                //oSheet.Cells[6, 50].Value2 = "Week 8";
                //oSheet.Cells[6, 56].Value2 = "Week 9";
                //oSheet.Cells[6, 62].Value2 = "Week 10";
                //oSheet.Cells[6, 68].Value2 = "Week 11";
                //oSheet.Cells[6, 74].Value2 = "Week 12";
                //oSheet.Cells[6, 80].Value2 = "Week 13";
                //oSheet.Cells[6, 86].Value2 = "Week 14";
                //oSheet.Cells[6, 92].Value2 = "Week 15";
                //oSheet.Cells[6, 98].Value2 = "Week 16";
                //oSheet.Cells[6, 104].Value2 = "Week 17";
                //oSheet.Cells[6, 110].Value2 = "Week 18";
                //oSheet.Cells[6, 116].Value2 = "Week 19";
                //oSheet.Cells[6, 122].Value2 = "Week 20";
                //oSheet.Cells[6, 128].Value2 = "Week 21";
                //oSheet.Cells[6, 134].Value2 = "Week 22";
                //oSheet.Cells[6, 140].Value2 = "Week 23";
                //oSheet.Cells[6, 146].Value2 = "Week 24";
                //oSheet.Cells[6, 152].Value2 = "Week 25";
                //oSheet.Cells[6, 158].Value2 = "Week 26";
                //oSheet.Cells[6, 164].Value2 = "Week 27";
                //oSheet.Cells[6, 170].Value2 = "Week 28";
                //oSheet.Cells[6, 176].Value2 = "Week 29";
                //oSheet.Cells[6, 182].Value2 = "Week 30";
                //oSheet.Cells[6, 188].Value2 = "Week 31";
                //oSheet.Cells[6, 194].Value2 = "Week 32";
                //oSheet.Cells[6, 200].Value2 = "Week 33";
                //oSheet.Cells[6, 206].Value2 = "Week 34";
                //oSheet.Cells[6, 212].Value2 = "Week 35";
                //oSheet.Cells[6, 218].Value2 = "Week 36";
                //oSheet.Cells[6, 224].Value2 = "Week 37";
                //oSheet.Cells[6, 230].Value2 = "Week 38";
                //oSheet.Cells[6, 236].Value2 = "Week 39";
                //oSheet.Cells[6, 242].Value2 = "Week 40";
                //oSheet.Cells[6, 248].Value2 = "Week 41";
                //oSheet.Cells[6, 254].Value2 = "Week 42";
                //oSheet.Cells[6, 260].Value2 = "Week 43";

    • Edited by _Sniffles_ Saturday, July 28, 2018 12:36 AM
    Saturday, July 28, 2018 12:35 AM

Answers

  • After spending an embarrassing amount of time trying to figure this out, reading up on the net and looking at several books, I finally did. For loop was probably the wrong approach altogether. I think they run the inner part until its done if the outer part is true, then the outer part runs or something similar.

    I needed to define my variables up front, write the cell data, then increment both variables, check the colIndex, then loop again. colIndex was wrong, in order to have it run 43 times, the number in the while check should have been 2+(6*43), because I started at 2 and would increment 6 each time.

                int colIndex = 2;
                int WeekCount = 1;
    
                while (colIndex <= 260)
                {
                    oSheet.Cells[6, colIndex] = "Week" + " " + WeekCount;
                    colIndex = colIndex + 6;
                    WeekCount = WeekCount + 1;
                }

    • Marked as answer by _Sniffles_ Saturday, July 28, 2018 2:15 PM
    Saturday, July 28, 2018 2:15 PM