Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 4, 2022 12:09 am GMT

100 Days of Swift - Day 4

Continuation of Hacking With Swift 100 Days of Swift

Day 4 - Looooooooops

  1. For loopsFor loops are done on a python way (maybe swift did it first?) but without the required indentation. So if you want to loop through [1,2,3,4] you'd do:for number in [1,2,3,4] {print(number)}

PS: Discard (the underscore _ ) is accepted if you don't want/need the actual variable

  1. While loops
    Nothing fancy to see here

  2. Repeat Loops
    Basically do whiles

  3. Breaking and skipping in loops
    While the keyword continue is just your everyday reserved word, breaks are really interesting.

Swift supports something called "labeled statements". Imagine the following nested loop:


for option1 in options {
for option2 in options {
// do code here
}
}

Like in other languages, if we use the keyword break it will exit the inner loop only, inside a function we could add a return but that's if we are inside a function, so how do we exit the parent? well you give it a label and then you tell swift to break label_name; example:

outerLoop: for option1 in options {
for option2 in options {
break outerLoop;
}
}

Pretty neat in my opinion.


Original Link: https://dev.to/davjvo/100-days-of-swift-day-4-1gnm

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To