Introducci贸n a Swift y XCode
Swift y el ecosistema Apple
Instalaci贸n y configuraci贸n de XCode
Quiz: Introducci贸n a Swift y XCode
Fundamentos de programaci贸n iOS
Variables, constantes y tipos de datos b谩sicos
Condicionales
Funciones
Tipos de datos opcionales
Arreglos: append, insert, como crearlas
Arreglos: validar, editar y agregar una lista dentro de otra lista
Conjuntos: como agregar o eliminar elementos
Conjuntos: principales operaciones entre conjuntos
Diccionarios
Recorridos parte 1: while
Recorridos parte 2: for
Recorridos parte 3: los retos
Quiz: Fundamentos de programaci贸n iOS
POO en iOS
Programaci贸n orientada a objetos en iOS
Structs
Clases y herencia
Enums
Protocolos
Funciones de arreglos (filter, map, reduce)
Funciones de arreglos parte 2 (filter, map, reduce): playground
Quiz: POO en iOS
Manejo de errores y programaci贸n segura
Manejo de errores y programaci贸n segura
Propagaci贸n de errores
Do, try, catch
Quiz: Manejo de errores y programaci贸n segura
Programaci贸n en el ecosistema de Apple
Siguientes pasos para el desarrollo en iOS
You don't have access to this class
Keep learning! Join and start boosting your career
Conditionals are one of the fundamental pillars of programming. They allow our code to execute certain actions depending on whether or not a condition we define is met. Imagine that you want to display a message to the user depending on whether a student passed an exam based on his or her grade. If the grade is greater than or equal to a certain number, we display a pass message; otherwise, we display a fail message.
In Swift, the basic structure of a conditional begins with the word if
, followed by the condition to be evaluated in parentheses. If the condition is met, the block of code between braces will be executed. If the condition is not met and we want to execute another block of code, we use the word else
.
let note = 3 if note > 3 { print("It is greater than three") } else { print("It is not greater than three") }
Mathematical operators are essential for making comparisons within conditionals. Some examples are:
>
Greater than>=
Greater than or equal to<
Less than<=
Less than or equal to==
Equal to!=
Different fromThese operators allow us to evaluate complex conditions and determine which set of instructions to execute.
if note &gt;= 3 { print("Is greater than or equal to three") } else { print("Is not greater than or equal to three") }
Sometimes, we need to evaluate more than one condition to decide what actions to take. In these cases, it is common to use else if
to include additional conditions.
let exempted = true if exempted || grade &gt;= 3 { print("Congratulations, you passed the exam") } else { print("Sorry, see you next semester") } }
For conditionals that require evaluating within certain ranges or several conditions at once, we can use grouping and logical operators.
let presento = true if presento &amp;&amp; (exempted || grade &gt;= 3) { print("Congratulations, you passed the exam") } else { print("Sorry, you didn't pass") } }
We can construct complex conditionals one after another to, for example, determine the quality of a grade.
let gradeTwo = 4 if gradeTwo &gt;= 0 &amp;&amp; gradeTwo &lt; 2 { print("You did very poorly on the exam.") } else if noteTwo &gt;= 2 &amp;&amp;&amp; noteTwo &lt; 3 { print("Sorry, that was close") } else if noteTwo &gt;= 3 &amp;&amp; noteTwo &lt; 4 { print("You passed, but you can do better") } else if noteTwo &gt;= 4 &amp;&amp; noteTwo &lt;= 5 { print("Congratulations, you did very well") } else { print("System error") }
Test your knowledge by evaluating the equivalence of two strings in Swift. How would you handle sentence comparison? Share your ideas and experiences in the comments.
Contributions 10
Questions 0
Want to see more contributions, questions and answers from the community?