?
Solved

for..for.. if .. continue

Posted on 2014-01-08
9
Medium Priority
?
203 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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