-
Notifications
You must be signed in to change notification settings - Fork 0
Adding Conditionals #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
13ddeec
[skip ci] Added new Call struct
leogdion c68750f
more tests
leogdion e1ad6f7
linting fixes
leogdion 05f396f
[skip ci] forgot comments
leogdion 86788d5
adding more complex statements for 'if'
leogdion 5ef1805
Add Guard
leogdion 1eebf61
fixes to switch case statements
leogdion 464d634
adding better tuple support
leogdion 581003f
fixing tests
leogdion 4446279
[skip ci] fixing tests
leogdion 0c3f5a0
Adding Let to case
leogdion 05aad3a
Fixing Linting issue
leogdion bbb60fb
fixing more linting issues
leogdion 010e4ef
[skip ci] starting for on more conditionals
leogdion aee9f9a
[skip ci] fixing SwiftCaseLet
leogdion 6ddc81e
Fixing Tests (#75)
leogdion c340c5d
Fixing linting issue
leogdion 272b264
Fixing linting issue
leogdion 83c67ef
[skip ci] still working on for loops
leogdion bd4b12d
spliting for loops
leogdion 886cc7e
fixed the for loop
leogdion 4b07603
changed where to closure
leogdion f0a2080
fixing file size
leogdion 04e0483
finishing spliting files
leogdion 0ab1adc
more linting fixes
leogdion 8793700
fixing unit tests
leogdion 9a1ed03
fixing indenting
leogdion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
import Foundation | ||
|
||
// MARK: - Basic If Statements | ||
|
||
// Simple if statement | ||
let temperature = 25 | ||
if temperature > 30 { | ||
print("It's hot outside!") | ||
} | ||
|
||
// If-else statement | ||
let score = 85 | ||
if score >= 90 { | ||
print("Excellent!") | ||
} else if score >= 80 { | ||
print("Good job!") | ||
} else if score >= 70 { | ||
print("Passing") | ||
} else { | ||
print("Needs improvement") | ||
} | ||
|
||
// MARK: - Optional Binding with If | ||
|
||
// Using if let for optional binding | ||
let possibleNumber = "123" | ||
if let actualNumber = Int(possibleNumber) { | ||
print("The string \"\(possibleNumber)\" has an integer value of \(actualNumber)") | ||
} else { | ||
print("The string \"\(possibleNumber)\" could not be converted to an integer") | ||
} | ||
|
||
// Multiple optional bindings | ||
let possibleName: String? = "John" | ||
let possibleAge: Int? = 30 | ||
if let name = possibleName, let age = possibleAge { | ||
print("\(name) is \(age) years old") | ||
} | ||
|
||
// MARK: - Guard Statements | ||
|
||
func greet(person: [String: String]) { | ||
guard let name = person["name"] else { | ||
print("No name provided") | ||
return | ||
} | ||
|
||
guard let age = person["age"], let ageInt = Int(age) else { | ||
print("Invalid age provided") | ||
return | ||
} | ||
|
||
print("Hello \(name), you are \(ageInt) years old") | ||
} | ||
|
||
// MARK: - Switch Statements | ||
|
||
// Switch with range matching | ||
let approximateCount = 62 | ||
let countedThings = "moons orbiting Saturn" | ||
let naturalCount: String | ||
switch approximateCount { | ||
case 0: | ||
naturalCount = "no" | ||
case 1..<5: | ||
naturalCount = "a few" | ||
case 5..<12: | ||
naturalCount = "several" | ||
case 12..<100: | ||
naturalCount = "dozens of" | ||
case 100..<1000: | ||
naturalCount = "hundreds of" | ||
default: | ||
naturalCount = "many" | ||
} | ||
print("There are \(naturalCount) \(countedThings).") | ||
|
||
// Switch with tuple matching | ||
let somePoint = (1, 1) | ||
switch somePoint { | ||
case (0, 0): | ||
print("(0, 0) is at the origin") | ||
case (_, 0): | ||
print("(\(somePoint.0), 0) is on the x-axis") | ||
case (0, _): | ||
print("(0, \(somePoint.1)) is on the y-axis") | ||
case (-2...2, -2...2): | ||
print("(\(somePoint.0), \(somePoint.1)) is inside the box") | ||
default: | ||
print("(\(somePoint.0), \(somePoint.1)) is outside of the box") | ||
} | ||
|
||
// Switch with value binding | ||
let anotherPoint = (2, 0) | ||
switch anotherPoint { | ||
case (let x, 0): | ||
print("on the x-axis with an x value of \(x)") | ||
case (0, let y): | ||
print("on the y-axis with a y value of \(y)") | ||
case let (x, y): | ||
print("somewhere else at (\(x), \(y))") | ||
} | ||
|
||
|
||
// MARK: - Compound Cases | ||
// Switch with compound cases | ||
let stillAnotherPoint = (9, 0) | ||
switch stillAnotherPoint { | ||
case (let distance, 0), (0, let distance): | ||
print("On an axis, \(distance) units from the origin") | ||
default: | ||
print("Not on an axis") | ||
} | ||
|
||
// MARK: - Fallthrough | ||
// Using fallthrough in switch | ||
let integerToDescribe = 5 | ||
var description = "The number \(integerToDescribe) is" | ||
switch integerToDescribe { | ||
case 2, 3, 5, 7, 11, 13, 17, 19: | ||
description += " a prime number, and also" | ||
fallthrough | ||
default: | ||
description += " an integer." | ||
} | ||
print(description) | ||
|
||
// MARK: - Labeled Statements | ||
|
||
// Using labeled statements with break | ||
let finalSquare = 25 | ||
var board = [Int](repeating: 0, count: finalSquare + 1) | ||
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02 | ||
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08 | ||
|
||
var square = 0 | ||
var diceRoll = 0 | ||
while square != finalSquare { | ||
diceRoll += 1 | ||
if diceRoll == 7 { diceRoll = 1 } | ||
switch square + diceRoll { | ||
case finalSquare: | ||
break gameLoop | ||
case let newSquare where newSquare > finalSquare: | ||
continue gameLoop | ||
default: | ||
square += diceRoll | ||
square += board[square] | ||
} | ||
} | ||
|
||
|
||
// MARK: - For Loops | ||
// For-in loop with range | ||
print("\n=== For-in with Range ===") | ||
for index in 1...5 { | ||
print("Index: \(index)") | ||
} | ||
|
||
// For-in loop with enumerated() to get index and value | ||
print("\n=== For-in with Enumerated ===") | ||
for (index, name) in names.enumerated() { | ||
print("\(index): \(name)") | ||
} | ||
|
||
// For-in loop with where clause | ||
print("\n=== For-in with Where Clause ===") | ||
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
for number in numbers where number % 2 == 0 { | ||
print("Even number: \(number)") | ||
} | ||
|
||
// For-in loop with pattern matching | ||
print("\n=== For-in with Pattern Matching ===") | ||
let mixedArray: [Any] = [1, "hello", 3.14, "world", 42, nil] | ||
for case let string as String in mixedArray { | ||
print("Found string: \(string)") | ||
} | ||
|
||
// MARK: - Early Exit with Guard | ||
|
||
// Function demonstrating early exit pattern | ||
func processUser(_ user: [String: Any]) -> String { | ||
guard let name = user["name"] as? String, !name.isEmpty else { | ||
return "Invalid user name" | ||
} | ||
|
||
guard let age = user["age"] as? Int, age >= 0 else { | ||
return "Invalid age" | ||
} | ||
|
||
guard let email = user["email"] as? String, email.contains("@") else { | ||
return "Invalid email" | ||
} | ||
|
||
return "Valid user: \(name), age \(age), email: \(email)" | ||
} | ||
|
||
// MARK: - Conditional Compilation | ||
|
||
// Using conditional compilation | ||
#if DEBUG | ||
print("Debug mode is enabled") | ||
#else | ||
print("Release mode is enabled") | ||
#endif | ||
|
||
#if os(iOS) | ||
print("Running on iOS") | ||
#elseif os(macOS) | ||
print("Running on macOS") | ||
#elseif os(Linux) | ||
print("Running on Linux") | ||
#else | ||
print("Running on unknown platform") | ||
#endif | ||
|
||
// MARK: - Testing the Examples | ||
|
||
// Test the examples | ||
print("\n=== Testing Conditional Examples ===") | ||
|
||
// Test if-else | ||
let testScore = 92 | ||
if testScore >= 90 { | ||
print("Test score \(testScore): Excellent!") | ||
} else if testScore >= 80 { | ||
print("Test score \(testScore): Good job!") | ||
} else { | ||
print("Test score \(testScore): Needs improvement") | ||
} | ||
|
||
// Test guard statement | ||
let userData = ["name": "Alice", "age": "25", "email": "alice@example.com"] | ||
print(processUser(userData)) | ||
|
||
let invalidUserData = ["name": "", "age": "invalid"] | ||
print(processUser(invalidUserData)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// Simple if statement | ||
let temperature = 25 | ||
if temperature > 30 { | ||
print("It's hot outside!") | ||
} | ||
|
||
// If-else statement | ||
let score = 85 | ||
if score >= 90 { | ||
print("Excellent!") | ||
} else if score >= 80 { | ||
print("Good job!") | ||
} else if score >= 70 { | ||
print("Passing") | ||
} else { | ||
print("Needs improvement") | ||
} | ||
|
||
// MARK: - Optional Binding with If | ||
|
||
// Using if let for optional binding | ||
let possibleNumber = "123" | ||
if let actualNumber = Int(possibleNumber) { | ||
print("The string \"\(possibleNumber)\" has an integer value of \(actualNumber)") | ||
} else { | ||
print("The string \"\(possibleNumber)\" could not be converted to an integer") | ||
} | ||
|
||
// Multiple optional bindings | ||
let possibleName: String? = "John" | ||
let possibleAge: Int? = 30 | ||
if let name = possibleName, let age = possibleAge { | ||
print("\(name) is \(age) years old") | ||
} | ||
|
||
// MARK: - Guard Statements | ||
func greet(person: [String: String]) { | ||
guard let name = person["name"] else { | ||
print("No name provided") | ||
return | ||
} | ||
|
||
guard let age = person["age"], let ageInt = Int(age) else { | ||
print("Invalid age provided") | ||
return | ||
} | ||
|
||
print("Hello \(name), you are \(ageInt) years old") | ||
} | ||
|
||
// MARK: - Switch Statements | ||
// Switch with range matching | ||
let approximateCount = 62 | ||
let countedThings = "moons orbiting Saturn" | ||
let naturalCount: String | ||
switch approximateCount { | ||
case 0: | ||
naturalCount = "no" | ||
case 1..<5: | ||
naturalCount = "a few" | ||
case 5..<12: | ||
naturalCount = "several" | ||
case 12..<100: | ||
naturalCount = "dozens of" | ||
case 100..<1000: | ||
naturalCount = "hundreds of" | ||
default: | ||
naturalCount = "many" | ||
} | ||
print("There are \(naturalCount) \(countedThings).") | ||
|
||
// Switch with tuple matching | ||
let somePoint = (1, 1) | ||
switch somePoint { | ||
case (0, 0): | ||
print("(0, 0) is at the origin") | ||
case (_, 0): | ||
print("(\(somePoint.0), 0) is on the x-axis") | ||
case (0, _): | ||
print("(0, \(somePoint.1)) is on the y-axis") | ||
case (-2...2, -2...2): | ||
print("(\(somePoint.0), \(somePoint.1)) is inside the box") | ||
default: | ||
print("(\(somePoint.0), \(somePoint.1)) is outside of the box") | ||
} | ||
|
||
// Switch with value binding | ||
let anotherPoint = (2, 0) | ||
switch anotherPoint { | ||
case (let x, 0): | ||
print("on the x-axis with an x value of \(x)") | ||
case (0, let y): | ||
print("on the y-axis with a y value of \(y)") | ||
case let (x, y): | ||
print("somewhere else at (\(x), \(y))") | ||
} | ||
|
||
// MARK: - Fallthrough | ||
// Using fallthrough in switch | ||
let integerToDescribe = 5 | ||
var description = "The number \(integerToDescribe) is" | ||
switch integerToDescribe { | ||
case 2, 3, 5, 7, 11, 13, 17, 19: | ||
description += " a prime number, and also" | ||
fallthrough | ||
default: | ||
description += " an integer." | ||
} | ||
print(description) | ||
|
||
// MARK: - Labeled Statements | ||
// Using labeled statements with break | ||
let finalSquare = 25 | ||
var board = [Int](repeating: 0, count: finalSquare + 1) | ||
board[03] = 8 | ||
board[06] = 11 | ||
board[09] = 9 | ||
board[10] = 2 | ||
board[14] = -10 | ||
board[19] = -11 | ||
board[22] = -2 | ||
board[24] = -8 | ||
|
||
var square = 0 | ||
var diceRoll = 0 | ||
while square != finalSquare { | ||
diceRoll += 1 | ||
if diceRoll == 7 { diceRoll = 1 } | ||
switch square + diceRoll { | ||
case finalSquare: | ||
break | ||
case let newSquare where newSquare > finalSquare: | ||
continue | ||
default: | ||
square += diceRoll | ||
square += board[square] | ||
} | ||
} | ||
|
||
// MARK: - For Loops | ||
// For-in loop with enumerated() to get index and value | ||
print("\n=== For-in with Enumerated ===") | ||
for (index, name) in names.enumerated() { | ||
print("\(index): \(name)") | ||
} | ||
|
||
// For-in loop with where clause | ||
print("\n=== For-in with Where Clause ===") | ||
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
for number in numbers where number % 2 == 0 { | ||
print("Even number: \(number)") | ||
} | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.