[swift-evolution] C-style For Loops

Michel Fortin michel.fortin at michelf.ca
Sat Dec 5 07:17:41 CST 2015


Le 5 déc. 2015 à 2:54, Roland King <rols at rols.org> a écrit :

> Convert that to a while(), if you have a continue in the body, you have a good chance of not incrementing at all, or duplicating the increment code before every continue. 

Well... you can do that:

	var x = 0
	while x < 10 {
		defer { x += 1 } // warning: also increment x if an exception is thrown
		continue
	}

Or maybe the C-style for loop could just be made more readable. You could replace the semicolons with words:

	for var x = 0 while x < 10 defer x += 1 {
	}

But that might be slightly misleading. "defer" would imply that this part is executed when an exception is thrown, which would be unlike a regular for loop.

-- 
Michel Fortin
michel.fortin at michelf.ca
https://michelf.ca



More information about the swift-evolution mailing list