Thread: Breaking out of a While loop with Nested If statements

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    Breaking out of a While loop with Nested If statements

    Code:
    while(x==1)
    {
        for (i=0;i<j;i++)
        {
            if (word1[i] == word2[i])
            {
                prefix[i]= word2[i];
                counter++;
            }
            else
    
    
            x=2;
        }
    Basically after the 3rd run of the for loop, it encounters a contradiction. I want it to exit right there and then. Instead it continues to run the for loop.

    What can I do?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    You can change the loop to be
    for (i=0;i<j && x == 1 ;i++)

    Or just use the break statement.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    I can't really answer without seeing the whole code .
    I suspect the j , but again - we need to see the code.

    anyway, I want to talk about SMART coding vs SYNTAXLY-CORRECT programming. although many lines of code are correct syntaxly - there will be better way to write them.

    I work in programming company , and although I'm not a programmer, and the programming there is done with Java or Javascript/PHP , I get to talk with programmers with years of experience about programming.
    nobody uses while loop. yes , I know I'll get crucified here with this - but it's true. while is uses very rarely, with very sparse situations , like wait for a socket to open , wait for something to finish download etc. almost 99% of the time you can exchange while loop with for loop .
    for loop is a smarter decision because you know exactly how you start , what happens incrementally every loop and when to finish , it is also more readable.
    let's try writing your code with for loop instead of while loop. as far as I understand, you want an array which will get the first letters which are the same in both strings. so we want to start with i=0, and we want to finish when the strings become different. in between we want to incrementally increase i and counter.
    so we will get:

    Code:
    for (i=0,counter=0;word1[i]==word2[i];i++,counter++){
    prefix[i]=word2[i];}
    see? with 2 lines of code we wrote exactly the same as 14(!) lines of code, we better understood with what we start w, when we end and what happens in between.
    isn't it much better now?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dave11
    nobody uses while loop. yes , I know I'll get crucified here with this - but it's true. while is uses very rarely, with very sparse situations , like wait for a socket to open , wait for something to finish download etc. almost 99% of the time you can exchange while loop with for loop .
    You contradicted yourself

    Quote Originally Posted by Dave11
    for loop is a smarter decision because you know exactly how you start , what happens incrementally every loop and when to finish , it is also more readable.
    Rather, if your loop satisfies these conditions, then you should choose to use a for loop. If not, then perhaps a while loop or some other construct would be a "smarter decision". Choosing the appropriate loop construct makes your code "more readable", e.g., if you use a for loop when a while loop is more appropriate, then your code would not be "more readable".

    Quote Originally Posted by Dave11
    let's try writing your code with for loop instead of while loop. as far as I understand, you want an array which will get the first letters which are the same in both strings. so we want to start with i=0, and we want to finish when the strings become different. in between we want to incrementally increase i and counter.
    so we will get:
    Code:
    for (i=0,counter=0;word1[i]==word2[i];i++,counter++){
    prefix[i]=word2[i];}
    see? with 2 lines of code we wrote exactly the same as 14(!) lines of code, we better understood with what we start w, when we end and what happens in between.
    isn't it much better now?
    The line count of 14 includes lines with braces only and a blank line, so that is quite an exaggeration, especially when your code is poorly formatted (or rather unformatted).

    More importantly, your version is not "exactly the same" in net effect:
    • You set counter=0, but that does not happen in the code shown, so it is an assumption that might not hold.
    • Your loop condition is solely word1[i]==word2[i], leaving out the i < j part. This could lead to a buffer overflow.

    Suitably amended, I think this would come closer:
    Code:
    for (i = 0; i < j && word1[i] == word2[i]; i++)
    {
        prefix[i] = word2[i];
    }
    counter += i;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Dave11 View Post
    nobody uses while loop. yes , I know I'll get crucified here with this - but it's true. while is uses very rarely, with very sparse situations , like wait for a socket to open , wait for something to finish download etc. almost 99% of the time you can exchange while loop with for loop.
    I don't know how else to put it, but you're dead wrong.

    A good programmer uses whichever is most appropriate, and best expresses the intentions.

    e.g. It is typically poor choice to use a for loop when you have no initial statement to execute, and no increment expression. It is also typically poor choice to use a for-loop for something that has no upper bound on the number of iterations.

    For loops are best used when there is a logical maximum number of iterations, and usually where there is an initialisation statement, and where there is an increment expression.

    There are always exceptions to the above, but if you find that most of the time you're not following such guidelines, then you're doing it wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Breaking out of a loop
    By theitsmith in forum C Programming
    Replies: 3
    Last Post: 02-09-2012, 12:30 PM
  2. nested switch statements error
    By DaleZ in forum C# Programming
    Replies: 1
    Last Post: 11-16-2010, 07:02 PM
  3. Breaking out of a loop
    By kiros88 in forum C Programming
    Replies: 5
    Last Post: 09-01-2009, 11:49 AM
  4. nested for statements
    By EvilPickles in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2006, 05:36 AM
  5. Nested if statements
    By jdinger in forum C Programming
    Replies: 3
    Last Post: 02-11-2002, 01:31 PM