Adnan_Sarker

Swift- LooPs nd Sort

Oct 27th, 2020 (edited)
986
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Foundation
  2.     //LOOP 1
  3.  
  4. for i in (0..<5) {
  5.     print(i)
  6. }
  7. // prints 0, 1, 2, 3, 4
  8.  
  9. for i in (0..<5).reversed() {
  10.     print(i)
  11. }
  12. // prints 4, 3, 2, 1, 0
  13.  
  14.     //LOOP 2
  15.  
  16. for i in (0...5) {
  17.     print(i)
  18. }
  19. // prints 0, 1, 2, 3, 4, 5
  20. for i in (0...5).reversed() {
  21.     print(i)
  22. }
  23. //prints 5, 4, 3, 2, 1, 0
  24.  
  25.     //LOOP 3
  26. for i in stride(from: 0, to: 5, by: 1) {
  27.     print(i)
  28. }
  29. // prints 0, 1, 2, 3, 4
  30. for i in stride(from: 5, to: 0, by: -1) {
  31.     print(i)
  32. }
  33. //prints 5, 4, 3, 2, 1
  34.  
  35.     //LOOP 4
  36. for i in stride(from: 0, through: 5, by: 1) {
  37.     print(i)
  38. }
  39. // prints 0, 1, 2, 3, 4, 5
  40. for i in stride(from: 5, through: 0, by: -1) {
  41.     print(i)
  42. }
  43. //prints 5, 4, 3, 2, 1, 0
  44.  
  45.  
  46.         //SORT
  47.  
  48. var names = ["Jemima", "Peter", "David", "Kelly", "Isabella"]
  49. names.sort()
  50.  
  51. var num = [2,9,7,0,6,4]
  52. num.sort()
  53.  
  54. print(names)
  55.  
  56.  
  57.  
  58.  
RAW Paste Data