Page 1 of 1

Loops with "continue"

PostPosted: 21 Feb 2013, 10:44
by Ursego
Use continue in loops instead of indenting the subsequent code with one more tab.

This method is a heavy weapon in the war against over-indenting:

*** BAD code: ***

PB:
Code: Select all
do while [loop condition]
   if [condition 1] then
      if [condition 2] then
         if [condition 3] then
            [code fragment with its own indenting levels]
         end if
      end if
   end if
loop

C#:
Code: Select all
while ([loop condition])
{
   if ([condition 1])
   {
      if ([condition 2])
      {
         if [condition 3])
         {
            [code fragment with its own indenting levels]
         }
      }
   }
}

*** GOOD code: ***

PB:
Code: Select all
do while [loop condition]
   if not [condition 1] then continue
   if not [condition 2] then continue
   if not [condition 3] then continue
   [code fragment with its own indenting levels]
loop

C#:
Code: Select all
while ([loop condition])
{
   if (![condition 1]) continue;
   if (![condition 2]) continue;
   if (![condition 3]) continue;
   [code fragment with its own indenting levels]
}