Loops with "continue"

Share this topic:



Link to this posting

Postby Ursego » 21 Feb 2013, 10:44

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]
}
User avatar
Ursego
Site Admin
 
Posts: 112
Joined: 19 Feb 2013, 20:33

Return to Coding Style

Who is online

Users browsing this forum: No registered users and 1 guest


Loops with "continue"

Share this topic:


If you think that this site is not too bad, please LIKE it in Facebook. Thanks!





cron
free counters

eXTReMe Tracker