Click here to Skip to main content
12,948,566 members (61,347 online)

Welcome to the Lounge


For discussing anything related to a software developer's life. Technical discussions are encouraged, but click here to ask your programming questions.

The Lounge is rated PG. If you're about to post something you wouldn't want your kid sister to read then don't post it. No flame wars, no abusive conduct, no programming questions and please don't post ads.
GeneralRe: The Office Desk Move Pin
PIEBALDconsult30-Jul-14 5:28
memberPIEBALDconsult30-Jul-14 5:28 
GeneralRe: The Office Desk Move Pin
Roger16531-Jul-14 2:53
memberRoger16531-Jul-14 2:53 
GeneralRe: The Office Desk Move Pin
Simon Lee Shugar31-Jul-14 3:17
memberSimon Lee Shugar31-Jul-14 3:17 
GeneralSSIS Part II Pin
Karen Mitchelle29-Jul-14 23:42
professionalKaren Mitchelle29-Jul-14 23:42 
GeneralRe: SSIS Part II Pin
Shameel30-Jul-14 1:34
professionalShameel30-Jul-14 1:34 
GeneralRe: SSIS Part II Pin
Roger Wright30-Jul-14 11:19
memberRoger Wright30-Jul-14 11:19 
GeneralRe: SSIS Part II Pin
PIEBALDconsult30-Jul-14 12:41
memberPIEBALDconsult30-Jul-14 12:41 
GeneralLoop exit Pin
Member 798912229-Jul-14 23:00
memberMember 798912229-Jul-14 23:00 
Running into a feature-by-feature language comparison made me think back of a feature I saw in one single langugage, but would fit very nicely into a lot of the pascal/c/java/... class of languages: Alternate loop exits.

When iterating through a list, an array or some sort of collection, objects are not all treated equally: You reach a sentinel, find the object you're searching for, reach the capacity of the bucket you are filling up, or whatever. The job has successfully been done, so you exit the loop. Or, you do not complete the job: There is no sentinel (because the buffer is completely filled), the desired object is not found, or the bucket has still some capacitly left.

Running through the collection to the end or not running to the end are different situations, frequently requiring different handling. In most languages, an early exit requires that you set some boolean flag decleared outside the loop, then break (or whatever the keyword is in your favorite language), and after the loop you add an if-statement, syntactically detached from the loop, to provide differnt treatment based on the setting of the flag.

I was programming in this language called Planc - "Programming LANguage for Nord Computers", a vendor specific systems implementation - remmebered by noone today. It had this nice syntactic sugar:
for listpointer in listhead:nextfield do

  ... processing list element as desired

  while listpointer.keyvalue <> desidred_key

  ... porcessing list element as desired

  ... the desired list element was found,
  write("list element was found and processed")

  ... reached end of list without finding the desired element
  write("no element with the desired key was found in the list")


No need for any one-time-use bool cluttering up variable space. No need to introduce a separate block for testing and breaking out. No need for a detached if-statement - the different loop exit handling is syntactically integrated with the loop itself.

I never saw this sort of construct in any other language, but I have been missing it hundred of times. Are there other languages out there with something similar? Certainly not C, C++, Java, C#, Pascal, ...

And, by the way: The above specification of the iteration is a nice syntactic sugar for what would be in C-like languages:
for (listptrtype listpointer = listhead; listpointer != null; listpointer = listpointer->nextfield) {

The Planc syntax is certainly more readable and less error prone, but the two are functionally equivalent.
The exitwhile/exitfor mechanism was valid whether the iteration was over a list, an array or some arbitrary collection, and both were optional: if one (or both) of the loop exits required no special handling, you could leavie that entire clause out.

Gee, I wish I could get that mechanism into, say, C#! The "while" part would be simple: Just add an optional condition after the break ("break (listpointer.keyvalue == desired_key);". Adding exitwhile/exitfor would require two more reserved words, which always causes problems and protests (even though I guess that very few variables out there are named exitwhile or exitfor...).

I once tried to construct C #define macros to emulate it, but never succeeded (if I remember right, the problem was generating jump labels for the gotos). If anyone has tried something similar, give me a hint!
GeneralRe: Loop exit Pin
Mark_Wallace29-Jul-14 23:18
memberMark_Wallace29-Jul-14 23:18 
GeneralRe: Loop exit Pin
Bergholt Stuttley Johnson29-Jul-14 23:26
memberBergholt Stuttley Johnson29-Jul-14 23:26 
GeneralRe: Loop exit Pin
Mark_Wallace29-Jul-14 23:31
memberMark_Wallace29-Jul-14 23:31 
GeneralRe: Loop exit Pin
Bergholt Stuttley Johnson29-Jul-14 23:33
memberBergholt Stuttley Johnson29-Jul-14 23:33 
GeneralRe: Loop exit Pin
Mark_Wallace29-Jul-14 23:38
memberMark_Wallace29-Jul-14 23:38 
GeneralRe: Loop exit Pin
CBadger29-Jul-14 23:54
professionalCBadger29-Jul-14 23:54 
GeneralRe: Loop exit Pin
Bergholt Stuttley Johnson30-Jul-14 0:09
memberBergholt Stuttley Johnson30-Jul-14 0:09 
GeneralRe: Loop exit Pin
Mark_Wallace30-Jul-14 1:08
memberMark_Wallace30-Jul-14 1:08 
GeneralRe: Loop exit Pin
Bergholt Stuttley Johnson30-Jul-14 1:46
memberBergholt Stuttley Johnson30-Jul-14 1:46 
GeneralRe: Loop exit Pin
CBadger30-Jul-14 3:08
professionalCBadger30-Jul-14 3:08 
GeneralRe: Loop exit Pin
Mycroft Holmes30-Jul-14 0:16
professionalMycroft Holmes30-Jul-14 0:16 
GeneralRe: Loop exit Pin
CBadger30-Jul-14 3:09
professionalCBadger30-Jul-14 3:09 
GeneralRe: Loop exit Pin
Mark_Wallace30-Jul-14 1:10
memberMark_Wallace30-Jul-14 1:10 
GeneralRe: Loop exit Pin
CBadger30-Jul-14 3:11
professionalCBadger30-Jul-14 3:11 
GeneralRe: Loop exit Pin
WiganLatics30-Jul-14 0:45
memberWiganLatics30-Jul-14 0:45 
GeneralRe: Loop exit Pin
OriginalGriff29-Jul-14 23:38
protectorOriginalGriff29-Jul-14 23:38 
GeneralRe: Loop exit Pin
Mark_Wallace29-Jul-14 23:40
memberMark_Wallace29-Jul-14 23:40 

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.

Advertise | Privacy | Mobile
Web01 | 2.8.170524.1 | Last Updated 25 May 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid