Code Monkey home page Code Monkey logo

swift-conditionals-readme's Introduction

Conditionals

Shakespeare

To be, or not to be...

Objectives

  1. Describe the meaning of conditional statements
  2. Use the if statement to conditionally execute a set of statements
  3. Use an else clause to provide an alternative set of statements
  4. Chain multiple if-else statements together

Conditional statements

You already learned how to use the Bool type and various operators to evaluate logical expressions and compare values:

var age = 19

let isOfDrinkingAge = age > 21 // false, because 19 is not greater than 21

As was briefly mentioned in that lesson, those expressions become powerful using conditional statements which allow you to write pieces of code which can be executed (or not executed) based on certain conditions. These are called control flow constructs, because they allow you to control the execution flow of your program, allowing your program's output to react to conditions, instead of always providing the same output.

If statement

The if statement makes the execution of a set of statements (also called a block of code) following it conditional based on a Bool value or expression. The given block of code will be executed only if the expression evaluates to true:

let age = 19

if age < 21 {
	print("You are not allowed to drink, yet.")
}

// prints "You are not allowed to drink, yet."

You can type this block of code in a new Playground file and experiment with giving age different values. You will notice that the print statement will no longer be executed once you increase the value of age to 21 or higher.

Now try to extend the program by adding a second if statement which prints a message if the age is greater than or equal to 21.

You will have ended up with something like this:

let age = 55

if age < 21 {
	print("You are not allowed to drink, yet.")
}

if age >= 21 {
	print("You are old enough to drink. ๐Ÿป")
}

// prints "You are old enough to drink. ๐Ÿป"

Else clause

This seems hard to read and will also lead to duplicated work should the condition ever change in the future. Thankfully, Swift provides a better way to provide an alternative block of code to an if statement, an else clause:

let age = 55

if age < 21 {
	print("You are not allowed to drink, yet.")
} else {
	print("You are old enough to drink. ๐Ÿป")
}

// prints "You are old enough to drink. ๐Ÿป"

The code block following an else statement will only be executed if the condition provided to the corresponding if statement is false.

Nested conditionals

Sometimes the execution flow of a program is not as clear cut as an either-or, there may be a third option or a default case. If that happens, you can chain multiple if-else statements together like this:

let temperatureInFahrenheit = 100

if temperatureInFahrenheit < 32 {
    print("Snow! ๐ŸŒจ")
} else if temperatureInFahrenheit >= 90 {
    print("Very hot! โ˜€๏ธ")
} else {
    print("Just right. ๐ŸŒป")
}

// prints "Very hot! โ˜€๏ธ"

This will print the corresponding message if one of the two conditions are true and it will print "Just right. ๐ŸŒป" when both of them are false. You should try typing this into your Playground again and see which statements are executed depending on the value of temperatureInFahrenheit.

You should again keep readability in mind and not nest if-statements too deeply. Swift provides an additional conditional statement called switch to handle bigger sets of conditionals, this will be introduced in another lesson.

View this lesson on Learn.co

swift-conditionals-readme's People

Contributors

jimcampagno avatar neonichu avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.