Cookbook – SWIFT for Beginners – Chapter 14: Conditionals

Free eBooks for Beginners

Conditional statements are an essential part of programming. They allow you to write code that can make decisions based on certain conditions. In Swift programming, there are two main types of conditional statements: if-statements and switch-statements.

If-statements are used to execute a certain block of code only if a certain condition is met. For example, you can write an if-statement that checks if a number is greater than 5 and print “The number is greater than 5” if it is by writing:

if number > 5 { 
   print("The number is greater than 5") 
}

You can also add an “else” clause to an if-statement to execute a different block of code if the condition is not met. For example, you can write an if-statement that checks if a number is greater than 5 and print “The number is greater than 5” if it is, or “The number is not greater than 5” if it’s not by writing:

if number > 5 { 
   print("The number is greater than 5") 
} 
else { 
   print("The number is not greater than 5") 
}

You can also add an “else if” clause to an if-statement to check multiple conditions. For example, you can write an if-statement that checks if a number is greater than 5, less than 10, or equal to 8 and print the corresponding message by writing:

if number > 5 { 
   print("The number is greater than 5") 
} 
else if number < 10 { 
   print("The number is less than 10") 
} 
else if number == 8 { 
   print("The number is equal to 8") 
}

Switch-statements are similar to if-statements, but they are used to execute a certain block of code based on multiple conditions. With switch-statements, you can write code that is more concise and readable. For example, you can write a switch-statement that checks the value of a number and print the corresponding message by writing:

switch number { 
   case 1: print("The number is 1") 
   case 2: print("The number is 2") 
   case 3: print("The number is 3") 
   default: print("The number is not 1, 2, or 3") 
}

In conclusion, conditional statements are an important part of programming that allow you to make decisions based on certain conditions. In Swift programming, you can use if-statements and switch-statements to write code that is flexible and responsive to different situations. As a beginner in Swift programming, understanding conditional statements is crucial to writing efficient and effective code.

Cookbook – SWIFT for Beginners – Chapter 14: Conditionals

Loader Loading...
EAD Logo Taking too long?

Reload Reload document
| Open Open in new tab

Download PDF

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.