KnowledgeCity

Swift Beginner: Statements, Loops, and Conditionals

Control flow is how you make your program’s logic operate.

Control flow is how you make your program’s logic operate. It’s one of the most important skill sets in software development. Having your logic move from one condition to another and then evaluating the results of that movement is a core skill. In this module, you’ll learn different methods of control flow. We will cover checking for different conditions in your code with If and If-Else control flow structures. You’ll also learn about logical operators and how you can use them to determine equivalency and comparison in Swift.

You’ll also learn about the switch statement. Switch evaluates a certain condition and then checks for cases within that condition. As your code goes from case to case in the switch statement, you’ll be able to run code depending on the case. This is oftentimes a more efficient way of writing your code compared to a series of nested IF statements. 

Learning Objectives

  • Describe control flow
  • Explain If and If-Else code blocks
  • Identify switch statements and cases

Author: Mark Nair

Duration: 31m · 4 lessons
Level: Beginner
Language: English

Skills you’ll gain

Control Flow DiagramControl LoopsData-Flow AnalysisFlow ControlProgram FlowSwift (Programming Language)

What You'll Learn

  • Describe control flow and how program logic moves from one condition to another
  • Use If and If-Else control flow structures to check for different conditions in your code
  • Apply logical operators to determine equivalency and comparison in Swift
  • Identify switch statements and evaluate cases to run code depending on the case
  • Write loops to control the flow of your Swift programs

Key Takeaways

  • Control flow is how you make your program's logic operate and is one of the most important skill sets in software development.
  • If and If-Else control flow structures let you check for different conditions in your code.
  • Logical operators are used to determine equivalency and comparison in Swift.
  • A switch statement evaluates a condition and checks for cases within it, running code as it moves from case to case.
  • Using a switch statement is oftentimes a more efficient way of writing code compared to a series of nested If statements.

Frequently Asked Questions

What does this Swift course cover?

This module covers different methods of control flow, including checking for conditions with If and If-Else structures, using logical operators for equivalency and comparison, working with switch statements and cases, and using loops.

What will I learn about switch statements?

You'll learn that a switch statement evaluates a certain condition and then checks for cases within that condition; as your code goes from case to case, you can run code depending on the case. This is oftentimes a more efficient way of writing your code compared to a series of nested If statements.

Why is control flow important in software development?

Control flow is how you make your program's logic operate, and it is described as one of the most important skill sets in software development. Having your logic move from one condition to another and then evaluating the results of that movement is a core skill.

What lessons are included in this module?

The module includes lessons on Checking for Conditions, "Else" and "Else If" Statements, Switch Statements, and Loops.

What skills does this course build?

The course builds skills in control flow, control loops, data-flow analysis, flow control, program flow, and the Swift programming language.

Transcript

Show transcript (free preview lesson)

Transcript of the free preview lesson. Remaining lessons unlock with the full course.

Hello, my name is Mark Nair, and in these lessons you'll learn about statements, loops and conditionals. One of the most important things you'll do with your code is control the flow of your logic. You'll do this by using logical operators and checking the conditions of those operations. The way we do this is by using the simple word, if. If something is true, run some code. If it's false, run some other code, it's really that simple. Let's set up a couple of constants and add some basic control flow. All right, so I have, on line one, let firstNumber = 5, and then let secondNumber = 10. So I have two constants right there, one called firstNumber, one called secondNumber, and then I'm making my conditional. So I'm saying if firstNumber is less than secondNumber, then I'm gonna run some code if that is true. And the way we do this is we put this in curly braces. I can double click the curly brace and see all the information that's inside of it here. So the code that runs inside of this curly brace and this curly brace is what's going to happen when the condition evaluates to true. So if firstNumber is less than secondNumber then print firstNumber using string interpolation is less than secondNumber. And down in the debug area, we see 5 is less than 10. So that's true. Now, if I change the value of firstNumber to 55 and run it, nothing happens, because what I've said is if firstNumber is less than secondNumber, print something, but I haven't said what to do otherwise. Now we can use logical operators and comparison operators to help us evaluate our expressions. To check if things are exactly equal, we use two equal signs like this, if firstNumber is equal to secondNumber. So 55 is equal to 55. Two equal signs means is this equivalent, is this exactly equal. This of course works with strings as well. So here I have a constant called let userName = Joanna and then let secondUserName = Joanna, and this one is all in caps right here on line two. Then, I'm saying, if userName lowercased is equal to secondUserName lowercased, so I need to put everything equivalent, remember, everything is case-sensitive. So if I don't have lowercased it will evaluate this as false, but since I've made both lowercased, then I can see what's going on, someone already has that name. To check if things are not equal, we'll use exclamation mark equal. So here, my two constants back, firstNumber, secondNumber, if firstNumber is not equal to secondNumber, print, these numbers are unequal, and 42 is not equal to 55. If firstNumber was 55, nothing happens. We can use regular mathematical expressions such as greater sign, greater equal, less than, less than an equal, the things that you are probably familiar with from grade school. But there are two other operators I do wanna talk more about, and that is the and operator. Let's take a look at that. Now in this expression, I have the constant temperatureInFahrenheit is equal to and, an integer of 72, and isSunny is equal to, is a Boolean type which is equal to true. Then I'm saying if temperatureInFahrenheit is equal to 72, then two ampersands, which means and, isSunny equals true. This means that both sides of this expression, temperatureInFahrenheit equals 42, and isSunny equals true, must be true. And means both, then print, what a great day for a bike ride. If I change the value up here to 79 and run it, nothing happens because temperatureInFahrenheit is not equal to 72, even though isSunny is true. Both have to be the same. We can simplify this by, taking isSunny equals true and just removing, equals true, because the way these values work, the Booleans, is we can just say isSunny. The default in that is true. So if temperatureInFahrenheit equals 72 and isSunny, which means, is Sunny equal to true. And it's the same result. So when you see this that's exactly what that means, it's equal to true. Now, the other one that I'd like to talk about is or. In this example I've established isSkiing is a Boolean with a value of false, isSnorkeling is a Boolean with a value of false. Then I'm saying, if isSkiing, and then these two pipes, that's what the character is called, it's right over the Return key on the keyboard. Two pipes means or, the logical operator or, so if skiing or isSnorkeling. So if skiing is true or if snorkeling is true, print, you'll have a fun time. Now this will print nothing because both are false. If I change isSkiing to true, then I see the result, you'll have a fun time. So if isSkiing is equal to true or isSnorkeling is equal to true, then print, you'll have a fun time, but of course we compressed this. So it's just, if isSkiing or isSnorkeling. So there's a lot of complexity and a lot of things you can do with your control flow inside your code using these logical operators and the if statement. Thanks for watching, stay tuned for the next lesson where I'll show you how to use else and else if to create your if constructions.

Learn on the Go

Take your learning anywhere — the KnowledgeCity mobile app lets you watch lessons on the go.