100 Days of Swift – Day 4

Continuation of Hacking With Swift 100 Days of Swift

Day 4 – Looooooooops

For loops
For 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 i…


This content originally appeared on DEV Community and was authored by Davmi Jose Valdez Ogando

Continuation of Hacking With Swift 100 Days of Swift

Day 4 - Looooooooops

  1. For loops For 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.


This content originally appeared on DEV Community and was authored by Davmi Jose Valdez Ogando


Print Share Comment Cite Upload Translate Updates
APA

Davmi Jose Valdez Ogando | Sciencx (2022-04-04T00:09:32+00:00) 100 Days of Swift – Day 4. Retrieved from https://www.scien.cx/2022/04/04/100-days-of-swift-day-4/

MLA
" » 100 Days of Swift – Day 4." Davmi Jose Valdez Ogando | Sciencx - Monday April 4, 2022, https://www.scien.cx/2022/04/04/100-days-of-swift-day-4/
HARVARD
Davmi Jose Valdez Ogando | Sciencx Monday April 4, 2022 » 100 Days of Swift – Day 4., viewed ,<https://www.scien.cx/2022/04/04/100-days-of-swift-day-4/>
VANCOUVER
Davmi Jose Valdez Ogando | Sciencx - » 100 Days of Swift – Day 4. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/04/100-days-of-swift-day-4/
CHICAGO
" » 100 Days of Swift – Day 4." Davmi Jose Valdez Ogando | Sciencx - Accessed . https://www.scien.cx/2022/04/04/100-days-of-swift-day-4/
IEEE
" » 100 Days of Swift – Day 4." Davmi Jose Valdez Ogando | Sciencx [Online]. Available: https://www.scien.cx/2022/04/04/100-days-of-swift-day-4/. [Accessed: ]
rf:citation
» 100 Days of Swift – Day 4 | Davmi Jose Valdez Ogando | Sciencx | https://www.scien.cx/2022/04/04/100-days-of-swift-day-4/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.