Swift Programming - Zero To Hero - Part Three

Introduction

This is part three of "Swift Programming - Zero to Hero" series. You can read the previous articles in the  series,

In this article, we will learn about conditional statements -

  1. If Statement
  2. Switch Statement

What are statements?

Statements are building blocks of any program. We can perform any function with the help of statement, such as checking values or declaring variables, calling methods, or even creating objects.

If Statements

If Statements allow us to check whether or not the given condition is True. If the condition is true, the code inside the true block is executed. Else, the code block outside the statement is executed.

Syntax

if condition {
// statement(s)
}

Example Code

  1. var a = 4  
  2. if (a % 2 == 0) {  
  3.     print(“The variable is Even”)  
  4. }  

Output

The variable is Even

In the above-given example, the if statement checks variable a’s value. If the value is divisible by 2, then program control enters the if block and displays the result as “The variable is Even”.

If……. Else Statement

In our previous example, we have not specified any code that must be executed when you enter a value which is not divisible by 2. For this purpose, we use else clause with the if statement. Else block is executed when the if block is not fulfilled or if block executes to false.

Syntax

if condition {
// statement(s)
}
else {
// statement(s)
}

Example Code

  1. var a = 5  
  2. if (a % 2 == 0) {  
  3.     print(“The variable is Even”)  
  4. else {  
  5.     print(“The variable is Not Even”)  
  6. }  

Output

The variable is Not Even

In the above-given example, the if statement checks  variable a’s value. If the value is divisible by 2, then program control enters the if block and displays the result as “The variable is Even”. Else, the "else" block is executed. Here, the value is not divisible by 2, so the else block is executed and displays the result as “The variable is Not Even”.

Switch Statements

Another form of selection statement is Switch statement. This is an alternative way for making selections in an easier way. The Switch statement passes the program control to one of the case statements whose value matches with that of the variable specified in the Switch statement.

Syntax

switch (variable name) // test expression
{
Case 1:
Statement(s) // constant expression
Case 2:
Statement(s)
Case 3:
Statement(s)
}

In the above syntax, the Switch statement compares a test expression with different case statements. The program control passes from one case to another until a case statement is found whose constant expression matches with that of test expression and then the code within that case statement is executed.

Example Code

  1. let name: character = ”u”  
  2. switch name {  
  3.     case“ b”:  
  4.         print(“Not a vowel”)  
  5.     case“ c”:  
  6.         print(“Not a vowel”)  
  7.     case“ u”:  
  8.         print(“Is a vowel”)  
  9.     default:  
  10.         print(“Not a character”)  
  11. }  

Output

Is a vowel

In the above code, we have assigned a constant variable character and its values as “u” as a character value. The program control passes the value “u” to the cases. First, the case checks with case “b”, it's not matched. Then, the program control checks with another case, as “c”, it is also not matched; the program control passes to next case as “u”. Here, it matches. The statements inside the case “u” is executed and the result is displayed as “Is a vowel”.

The default case statement is executed when any of the cases does not match with the test expression.

Conclusion

In this article, we have learned about Conditional and Switch statements in Swift programming. Hope it was helpful. In my next article, we will see arrays and ways of accessing array elements using conditional and looping statements.


Similar Articles