Nick Kahn
Nick Kahn

Reputation: 20078

Multiple For-Loops

EDIT: I have updated my question and I know that I can increase the loop instead of 4 to 12 but the reason I have separate three for-loop is due to that; after iterating first loop i have to click carousel in order to go to the next next iteration as you can see my updated code. - sorry somehow i missed the code while copy/pasting.

I want to iterate through the rows, the total rows I have is 3 and every row has 4 columns, for an example:

row 1 has 4 columns
row 2 has 4 columns
row 3 has 4 columns

I have created 3 separate for-loop and my code works but I believe there is room to improve my code, can it have in one loop instead of three?

here is the code:

//first row

for (int j = 0; j < 4; j++) 
{  
   string _image = String.Format("id('gen{0}')/a[{1}]/img", j, "1");                 
   string _text = String.Format("id('gen{0}')/a[{1}]", j, "2");
   ...........
   WaitForElement(By.XPath(_text)).Click();
}

_carouselDot = String.Format("id('HomePage1_Carousel1')/div/ul/li[{0}]/a", 2);
WaitForElement(By.XPath(_carouselDot)).Click();

//second row
for (int j = 4; j < 8; j++) 
{  
   string _image = String.Format("id('gen{0}')/a[{1}]/img", j, "1");                 
   string _text = String.Format("id('gen{0}')/a[{1}]", j, "2");
   ...........
   WaitForElement(By.XPath(_text)).Click();
}

_carouselDot = String.Format("id('HomePage1_Carousel1')/div/ul/li[{0}]/a", 3);
WaitForElement(By.XPath(_carouselDot)).Click();

//third row
for (int j = 8; j < 12; j++) 
{  
   string _image = String.Format("id('gen{0}')/a[{1}]/img", j, "1");                 
   string _text = String.Format("id('gen{0}')/a[{1}]", j, "2");
   ...........
   WaitForElement(By.XPath(_text)).Click();
}

Upvotes: 0

Views: 244

Answers (5)

ReneSac
ReneSac

Reputation: 531

You can do this neatly and efficiently with two nested for blocks:

int rows = 3;
int cols = 4;
for (int i = 0; i < rows; i++) 
{  
    for (int j = 0; j < cols; j++)
    { 
        int n = (cols * i) + j;
        string _image = String.Format("id('gen{0}')/a[{1}]/img", n, "1");                
        string _text = String.Format("id('gen{0}')/a[{1}]", n, "2");
        ...........
        WaitForElement(By.XPath(_text)).Click();
    }
    if (i < rows - 1) 
    {
        _carouselDot = String.Format("id('HomePage1_Carousel1')/div/ul/li[{0}]/a", 2);
        WaitForElement(By.XPath(_carouselDot)).Click();
    }
}

That way it is clear that you are dealing with three separated lines, and you can do especial processing at the end or start of each line as necessary, with simpler if clauses.

Edited for the new question, showing that you can do a if() after the inner for block.

Also, it is good practice to avoid magic numbers inside your code. With this you avoid bugs, and if you ever need to change the number of rows or columns, for example, you just have to change one variable.

Upvotes: 1

Aaron
Aaron

Reputation: 846

Just like this?

for (int i = 0; i < 3; i++) 
{  
   for (int j = 0; j < 4; j++)
   { 
        code here
   }
}

EDIT:

Because you edited your question, this seems better now:

for (int i = 0; i < 12; i++)
       { 
            string _image = String.Format("id('gen{0}')/a[{1}]/img", j, "1");                 
            string _text = String.Format("id('gen{0}')/a[{1}]", j, "2");
            ...........
            WaitForElement(By.XPath(_text)).Click();

            if (i % 4 == 0)
            {
                _carouselDot = String.Format("id('HomePage1_Carousel1')/div/ul/li[{0}]/a", 2);
                WaitForElement(By.XPath(_carouselDot)).Click();
            }
       }

Upvotes: 5

Rohit Vats
Rohit Vats

Reputation: 81253

What's wrong with this -

for (int j = 0; j < 12; j++) 
{  
   string _image = String.Format("id('gen{0}')/a[{1}]/img", j, "1");                 
   string _text = String.Format("id('gen{0}')/a[{1}]", j, "2");
   ...........
   WaitForElement(By.XPath(_text)).Click();

   if(i % 4 == 3 && i != 11)
   {
      _carouselDot = String.Format("id('HomePage1_Carousel1')/div/ul/li[{0}]/a",
                                                                 (i/4 + 2));
       WaitForElement(By.XPath(_carouselDot)).Click();
   }
}

Upvotes: 3

Roy Dictus
Roy Dictus

Reputation: 33139

I see no difference between the three blocks other than the boundaries for j; so why can you not just do one loop?

for (int j = 0; j < 12; j++) 
...

Upvotes: 4

Mark Byers
Mark Byers

Reputation: 838186

Yes, you can have a single loop that runs from 0 to 12...

for (int j = 0; j < 12; j++) 
{  
   string _image = String.Format("id('gen{0}')/a[{1}]/img", j, "1");                 
   string _text = String.Format("id('gen{0}')/a[{1}]", j, "2");
   ...........
   WaitForElement(By.XPath(_text)).Click();
}

Upvotes: 5

Related Questions