<div dir="ltr">On Thu, Dec 10, 2015 at 12:16 PM, Michel Fortin via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">You can't replace C-style for loops with for-in in the general case. Using a "while" loop will always be possible, but also much more verbose. For instance, take this loop iterating downward over i and j in lockstep:<br>
<br>
for var i = 10, j = 20; i > 0 && j > 0; i -= 1, j -= 2 {<br>
... loop body ...<br>
}<br>
<br>
The only fully-compatible while loop that makes sense is this one:<br>
<br>
do {<br>
var i = 10<br>
var j = 20<br>
var first = true<br>
while true {<br>
if first { first = false }<br>
else { i -= 1; j -= 2 }<br>
guard i > 0 && j > 0 else { break }<br>
... loop body ...<br>
}<br>
}<br>
<br>
If the loop body contains a continue, it works. If the loop body mutates one of the loop variables, it works. If the loop throws, it works. If the outer scope has a variable with the same name, it works! There is no code duplication. But oh boy, the boilerplate!<br></blockquote><div><br></div><div>Can't you do the following?</div><div><br></div><div><div> var i = 10, j = 20; while i > 0 && j > 0 { defer { i -= 1; j -= 2 }</div><div> // ...</div><div> }</div></div><div><br></div><div>I adjusted the formatting to more closely match the C-style for loop, so if you were to use a more Swift-y style, it would be slightly more verbose.</div><div><br></div><div>Stephen</div></div></div></div>