Swift: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
No edit summary
Line 24: Line 24:
</syntaxhighlight>
</syntaxhighlight>
==Switch Statements==
==Switch Statements==
No surprises
<syntaxhighlight lang="swift">
<syntaxhighlight lang="swift">
switch a {
var aCharacter = "a"
switch aCharacter {
   case "a":
   case "a":
       print("we are here a")
       print("we are here a")
Line 34: Line 34:
       print("we are here not a or b")
       print("we are here not a or b")
}
}
</syntaxhighlight>
==Loops==
Example for loop
<syntaxhighlight lang="swift">
var sum = 0
for index in 1..5 {
  sum += index
  print(sum)
}
</syntaxhighlight>
Example for while loop
<syntaxhighlight lang="swift">
var counter = 5
while counter  > 0 {
  print("hello")
  counter -= 1 
}
</syntaxhighlight>
Example for repeat while loop
<syntaxhighlight lang="swift">
var counter = 5
repeat {
  print("hello")
  counter -= 1 
} while counter  > 0
</syntaxhighlight>
<syntaxhighlight lang="swift">
</syntaxhighlight>
</syntaxhighlight>

Revision as of 00:16, 24 June 2022

Introduction

Here is my first dip into Apple and swift Swift Cheat Sheet

Data Types

Here we have the following Primatives

  • Int
  • Float
  • Double
  • Str
  • Bool

Control

If Statements

No surprises

if a < 4 {
  print("we are here 1")
}
else if a == 4 && a < 3 {
  print("we are here 2")
}
else {
  print("we are here 3")
}

Switch Statements

var aCharacter = "a"
switch aCharacter {
   case "a":
      print("we are here a")
   case "b":
      print("we are here b")
   default:
      print("we are here not a or b")
}

Loops

Example for loop

var sum = 0
for index in 1..5 {
   sum += index 
   print(sum)
}

Example for while loop

var counter = 5
while counter  > 0 {
   print("hello")
   counter -= 1  
}

Example for repeat while loop

var counter = 5
repeat {
   print("hello")
   counter -= 1  
} while counter  > 0