Like Tree2Likes

how to come out of double loop

This is a discussion on how to come out of double loop within the C Programming forums, part of the General Programming Boards category; Originally Posted by dwks Setting the loop control variables to their upper bounds seems like a bad idea to me ...

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dwks View Post
    Setting the loop control variables to their upper bounds seems like a bad idea to me though. First, it might require more maintenance, especially if the "this/that" boundary expressions are complicated. And a compiler optimizer which would otherwise see a perfect loop with nicely striding loop variables could get confused by your meddling with the control variables. You might get less optimized code as a result.
    It was just an example of not needing extra values.

    It would probably be better to set them to size -1 so that when the loop increments the final time before the check, that you don't overflow (in the event you are using _MAX or something). It's not really something I see myself using anyway, but you could.

    Honestly, I can't see your compiler ever doing what you are suggesting. There is nothing wrong with setting the value of a loop control variable to something different inside the loop. I have never heard of that being an issue - and it shouldn't. I can't possibly imagine a scenario where that (you setting a loop control value specifically) caused your compiler to break your loop.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Quote Originally Posted by quzah View Post
    Honestly, I can't see your compiler ever doing what you are suggesting. There is nothing wrong with setting the value of a loop control variable to something different inside the loop. I have never heard of that being an issue - and it shouldn't. I can't possibly imagine a scenario where that (you setting a loop control value specifically) caused your compiler to break your loop.
    I wasn't thinking the compiler would break the loop, just that it would be less able to perform optimizations. Actually I was thinking of vectorization operations which usually require a countable loop . . . but using "break" to end a loop makes the loop uncountable just as surely as setting the loop index variable. So I guess my thinking wasn't very straight. You're probably right.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dwks View Post
    I wasn't thinking the compiler would break the loop, just that it would be less able to perform optimizations.
    Ok, I will blame the confusion on not enough coffee. I confused myself for a bit I think with "break;-ing from a loop" with "you broke the loop!".


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    @iMalc : Thats an interesting idea .. It suited my needs

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Hashing Infinite Loop
    By jlangfo5 in forum C++ Programming
    Replies: 4
    Last Post: 03-06-2011, 07:48 PM
  2. Replies: 6
    Last Post: 11-04-2010, 05:33 PM
  3. fprintf in a double while loop
    By wyoui in forum C Programming
    Replies: 2
    Last Post: 06-01-2010, 01:31 PM
  4. a double linked list loop
    By elton_fan in forum C Programming
    Replies: 1
    Last Post: 10-01-2007, 12:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21