Click here to Skip to main content
15,909,829 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Loop exit Pin
Ravi Bhavnani30-Jul-14 2:29
professionalRavi Bhavnani30-Jul-14 2:29 
GeneralRe: Loop exit Pin
SortaCore30-Jul-14 22:22
SortaCore30-Jul-14 22:22 
GeneralRe: Loop exit Pin
kalberts30-Jul-14 2:13
kalberts30-Jul-14 2:13 
GeneralRe: Loop exit Pin
Roger Wright30-Jul-14 11:21
professionalRoger Wright30-Jul-14 11:21 
GeneralRe: Loop exit Pin
Mark_Wallace30-Jul-14 19:38
Mark_Wallace30-Jul-14 19:38 
GeneralRe: Loop exit Pin
Simon O'Riordan from UK30-Jul-14 22:00
Simon O'Riordan from UK30-Jul-14 22:00 
GeneralRe: Loop exit Pin
Pablo Aliskevicius30-Jul-14 1:35
Pablo Aliskevicius30-Jul-14 1:35 
GeneralRe: Loop exit Pin
kalberts30-Jul-14 2:08
kalberts30-Jul-14 2:08 
Still you must analyze those "ret" values, and values 1, 2, 3 do not syntactically convey the information that you reached the end of the collection (or skipped out of the loop). You need this extra "ret" value, which must be declared for this one-time use. While your proposal might be a starting point for explicitly coding what the compiler might generate, it certainly does not have the readability and syntactical clearness that the exitfor/exitwhile syntax has.

Also, I doubt that the compiler would code it as a function. It would generate one jump label for the exitwhile clause, another for the exitfor (both defaulting to the first statement following the loop). The top line iteration test would jump to the exitfor label when the looping condition fails, the while statements would jump to the exitwhile label when it fails.

If the language would provide block local program labels, visible only within the loop, I could code my example as
C++
for listpointer = listhead:nextfield do
  ...
  if listpointer.keyvalue = desired_key goto exitwhilelabel
  ...
  if listpointer.nextfield = null goto exitforlabel

exitwhilelabel:
  ... object found
  goto endforlabel

exitforlabel:
  ... object not found
  goto endforlabel

endforlabel:
endfor

This is what a reaonable compiler would generate - but I think it ugly when written out in longhand code. Besides, jump labels do not have block local scope in any language I know of, so you would have to invent new labels for every loop using this mechanism ("if listpointer.keyvalue = desired_key goto exitwhilelabel117" - even more ugly!)

I tried to make C macros that would generate unique labels, but the problem was to make the asocciation between the while part (or if test in the code above) and the appropriate exitwhile. A compiler could easily do this.

(Tne "goto endforlabel" in the exitfor clause is redundant and would be optimized away, but it allows the exitfor and exitwhile clauses to be switched around.)
GeneralRe: Loop exit Pin
Dan Neely30-Jul-14 2:26
Dan Neely30-Jul-14 2:26 
GeneralRe: Loop exit Pin
irneb30-Jul-14 20:04
irneb30-Jul-14 20:04 
GeneralRe: Loop exit Pin
Stefan_Lang30-Jul-14 20:11
Stefan_Lang30-Jul-14 20:11 
GeneralRe: Loop exit Pin
harvyk030-Jul-14 20:28
harvyk030-Jul-14 20:28 
GeneralRe: Loop exit Pin
Stefan_Lang30-Jul-14 20:39
Stefan_Lang30-Jul-14 20:39 
AnswerRe: Loop exit Pin
L. Braun31-Jul-14 0:10
L. Braun31-Jul-14 0:10 
GeneralRe: Loop exit Pin
Mark_Wallace31-Jul-14 0:17
Mark_Wallace31-Jul-14 0:17 
GeneralRe: Loop exit Pin
Mark_Wallace31-Jul-14 0:18
Mark_Wallace31-Jul-14 0:18 
GeneralRe: Loop exit Pin
Stefan_Lang31-Jul-14 0:34
Stefan_Lang31-Jul-14 0:34 
GeneralRe: Loop exit Pin
L. Braun31-Jul-14 1:40
L. Braun31-Jul-14 1:40 
GeneralRe: Loop exit Pin
Stefan_Lang31-Jul-14 3:24
Stefan_Lang31-Jul-14 3:24 
GeneralRe: Loop exit Pin
kalberts31-Jul-14 23:33
kalberts31-Jul-14 23:33 
GeneralRe: Loop exit Pin
Stefan_Lang3-Aug-14 23:50
Stefan_Lang3-Aug-14 23:50 
GeneralRe: Loop exit Pin
Eduard Matei31-Jul-14 0:59
Eduard Matei31-Jul-14 0:59 
GeneralRe: Loop exit Pin
Matthew Barnett31-Jul-14 1:20
Matthew Barnett31-Jul-14 1:20 
GeneralRe: Loop exit Pin
kalberts31-Jul-14 23:11
kalberts31-Jul-14 23:11 
GeneralRe: Loop exit Pin
M. Eugene Andrews31-Jul-14 1:53
M. Eugene Andrews31-Jul-14 1:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.