Solved

for..for.. if .. continue

Posted on 2014-01-08
9
199 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

732 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