Skip to content

Instantly share code, notes, and snippets.

@Gabbrolee
Last active August 8, 2021 09:47
Show Gist options
  • Save Gabbrolee/c0f0288df7c9a23d5a08615d44eb7f7a to your computer and use it in GitHub Desktop.
Save Gabbrolee/c0f0288df7c9a23d5a08615d44eb7f7a to your computer and use it in GitHub Desktop.
dayTwo
import UIKit
//MARKS:- DICTIONARIES This is can be compare with an array in the sense that, it uses keys to address position of element in a collection but array uses index to access the position of element in a collection
var person = ["first":"Kemi", "second":"Bola", "last":"Temi", "month":"December", "website":"www.google.com"]
person["first"] // output: Kemi
person["month"] // output: December
//MARkS:- from the output above the key is first and the value is Kemi
// dictionaries use keys and values unlike array that use index to search
// for position of items in a collection
// ["key": "value" ] which can be in the form of [string: string], [Int: string]
//MARKS:- CONDITIONAL STATEMENTS
// Sometimes we want codes perform operation if a condition is true
// execute another line of code if it is false
// this can be handle by the conditional statement with if else
// if(code is true){perform this action} else {perform this action}
var age = 25
var name = "Shayo"
if age == 25 {
print("Shayo is \(age) years old")
}else {
print("the if condition is false")
}
// since the if statement is true then its statement is printed
if age == 20 {
print("Shayo is \(age) years old")
} else {
print("The if condition is false so I am out")
}
// the if statements goes on like that as if.. elseif ..else
// this continue to happen until one of the condition is meant
// the statement of the condition meant is finally executed
// if none of the condition is satisfy, the else statement will be executed
//MARKS:- LOOPS
//This is use to perform iteration i.e number of times of operation needed
//1. for loop
for i in 1...10{
print("God is good") // this will print the statement 10 times with count from 1
}
var str = "Fakers gonna " // assign string to variable str
for i in 1...4{ // count from 1 to 4
str += " fake" // add fake 4 times to str
}
print(str) // Fakers gonna fake fake fake fake
var people = ["players", "haters", "heartbreakers", "baby"]
var action = ["play", "hates", "break", "cry"]
for index in 0...2{ // this perform the operation 3 times with count
// from zero to 2
print("\(people[index]) gonna \(action[index])") // index of array of people is printed at same index
// of that of action
}
// the total element in the array can be known with the use of count property
//people.count will give 3
//but to get the last index we do people.count - 1 we get 2
// you can put loops inside loop
for i in 0 ..< people.count{ // the loop goes 4 times
for i in 1...3 { // the loop goes 3 times
print("\(people[i]) gonna \(action[i])") // total of 12 times
}
}
//MARKS:- When we dont know the number of
//count an operation will happen we use while loop
var counter = 0
var sum = 0
while true {
print("counter is now: \(counter)")
counter += 1 // inctement counter
if counter == 6 { // stop when it is 6
break
}
sum += counter // continue adding the value of counter into sum
}
var songs = ["woo", "baby girl", "loadinginging"]
for song in songs {
if song == "baby girl"{
continue
}
print("my favorite song is \(song)")
}
//MARKS:- SWITCH CASE
// this test the block of variable one is looking
//for and once it is found it execute and stop
// it ensures the case are exhaustive
//otherwise xcode wont compile
let liveAlbums = 2
switch liveAlbums {
case 0:
"lets go to Dubai"
case 1:
"where are you"
case 2:
"this will be executed"
default:
"this is a default incase the search is not found"
}
// case range operator can also be use as well 0...2
//MARKS:- FUNCTIONS
func getOut(){ // function without parameter
print("lets go home if you don't mind")
}
getOut() // function call
func getOutNow(name:String){
print("lets go home if you don't mind \(name)")
}
getOutNow(name: "Samuel")
func albumRelease(name:String, year: Int){
print("\(name) is release in \(year)")
}
albumRelease(name: "BOO" , year: 2010)
func getMeal(_ food: String) -> Bool {
if food == "rice" {
true
}
else if food == "beans" {
return true
}else {
return false
}
return ( 0 != 0)
}
getMeal( "yam")
@Gabbrolee
Copy link
Author

Gabbrolee commented Aug 5, 2021 via email

@Gabbrolee
Copy link
Author

Gabbrolee commented Aug 5, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment