Solved

for..for.. if .. continue

Posted on 2014-01-08
9
192 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 26

Expert Comment

by:pony10us
Comment Utility
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
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

762 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

11 Experts available now in Live!

Get 1:1 Help Now