Solved

for..for.. if .. continue

Posted on 2014-01-08
9
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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
 
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 30

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

691 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