Solved

for..for.. if .. continue

Posted on 2014-01-08
9
195 Views
Last Modified: 2014-01-31
Hi;

I have the following pseudocode:
for {
   for {
     for {
       if(condition) continue;
       some_variable = some_expression   

Open in new window


so if the condition is true; it will continue but continue where? next line? Also if it's false, it will continue to the next line?

Regards.
0
Comment
Question by:jazzIIIlove
9 Comments
 
LVL 7

Expert Comment

by:Steve
ID: 39765639
If the condition is true it will continue on the very next line. If false it would continue after the "end if" following the If(condition) statement.
0
 
LVL 19

Accepted Solution

by:
Ken Butters earned 250 total points
ID: 39765644
when the expression is false the continue statement will not be executed... and so control will pass to the next line (line 5).

A value of true will execute the continue statement.

When the continue statement is executed.. control will be passed to the next evaluation of the in innermost for loop. in this case - (line 3).
0
 
LVL 26

Expert Comment

by:pony10us
ID: 39765657
Logically (LOL) it should go to line 5 if the condition on line 4 = true. If the condition = false it should return to line 3 until it exhausts the number of inputs available to that line.

For example lets say line 3 is actually:

for a=1 to 10

A condition of false on line 4 will return to line 3 until a = 11, without processing line 5 at which point it will return to line 2 and start the process all over again. If the condition is true it wil process line 5 and return to line 3 until a = 11. That is how the code is currently shown.  That may not be reality though since you don't show the rest of the code after line 5.

This description is based on the following assumed code:

for {
   for {
     for a = 1 to 10{
       if(condition) continue;
       some_variable = some_expression   
      }
   }
}

Open in new window

0
 
LVL 14

Expert Comment

by:CPColin
ID: 39765754
sodea63 and pony10us,

You're both missing the "continue" statement at the end of line 4. If the condition is true, the next line that will be executed is line 3, not line 5.

Ken Butters' answer is correct.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Expert Comment

by:pony10us
ID: 39765791
CPColin,

You are correct. I was looking at the "continue" incorrectly as I was working on several things here at work at the same time.  

The logic (there's that word again) in my explanation is backwards for the results of the condition on line 4. The "continue" is an actual statement in the code and therefore evaluates as:

If condition evaluates to true return to the for/while/etc. loop otherwise go on with the next line of code.

This is cleaner and shorter in this case than using an If/Else.

My bad, thank you again for pointing out my slip CPColin.
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 250 total points
ID: 39766267
I always recommend in such cases to write code with debug statements and run. It takes literally 5 minutes at most. And output shows you everything you need.

Try the code (change condition variable to true or false)

        private void button9_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < 5; i++)
            {
                System.Diagnostics.Debug.WriteLine("For - 1, i = " + i);
                for (int j = 0; j < 5; j++)
                {
                    System.Diagnostics.Debug.WriteLine("For - 2, j = " + j);
                    for (int k = 0; k < 5; k++)
                    {
                        System.Diagnostics.Debug.WriteLine("For - 3, k = " + k);
                        bool condition = true;

                        if (condition) continue;

                        System.Diagnostics.Debug.WriteLine("After continue");
                    }
                }
            }
        }

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 39766269
I read this question with some amusement as I have a program that searches a directory looking for excel workbooks (i.e files with a .xls extension), then searches the workbook for certain worksheets, then searches the worksheets a row at a time to perform some operation on certain rows that utilizes a number of for loops each with a condition to determine if the next searched object should be skipped or not (i.e. continue). Given the example of a = 1 to 10 where a represents a row in a worksheet with the criteria I only want to work on even numbered rows (i.e. the condition to continue would be if (a % 2 = 1) continue;)  so where a = 1 or 3 or 5 or 7 or 9, it would not perform the variable = expression statement, but increment a to 2 or 4 or 6 or 8 or 10 where it would perform the variable = expression statement. To compound matters, in certain cases, I actually need it to return not to the preceding for loop but to the one preceding that one or even to the one that precedes that one as well. Hope yours is not as complicated :-)
0
 
LVL 14

Expert Comment

by:CPColin
ID: 39803595
jazzIIIlove,

I'm not sure I deserved points for my comment; I was just agreeing with Ken Butters. Would it be okay if I opened an attention request so a moderator could reassign my points to his comment?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now