Coder Social home page Coder Social logo

swift-variables-readme-dc-web-career-040119's Introduction

Variables & Constants

Leo

Simplicity is the ultimate sophistication. —Leonardo da Vinci

Overview

In this lesson we'll define, create, and explain the differences between variables and constants.

Learning Objectives

  • Create a variable in a playground file
  • Change the value of a variable to another value of the same type
  • Explain how a constant is used and why it differs from a variable
  • Recognize the error produced when attempting to change the value of a constant
  • Distinguish when to use a variable and when to use a constant

Variables

Variables are the fundamental building block of any program. A variable associates a name with a value. That value can be almost anything: a number, a string (or sequence of characters), or even an object (which you will learn about in a future lesson). For instance, the following is a variable declaration in Swift:

var favoriteCharacter = "Jon Snow"

In the above example, a variable is declared using the var keyword. The name of the variable is favoriteCharacter. This variable has the value "Jon Snow". "Jon Snow" is a string, or sequence of characters enclosed in double quotation marks.

You can refer to this variable using its name. While the variable's value is initially "Jon Snow", it can change over time. For example, you could change it to "Tyrion Lannister":

favoriteCharacter = "Tyrion Lannister"

From here on out, favoriteCharacter will have the value "Tyrion Lannister", instead of "Jon Snow". Notice, however, that you didn't have to put var in front of favoriteCharacter when you changed the value. This is because you have already declared favoriteCharacter to be a variable, and so Swift knows that it exists. You're not declaring a variable here; you're just changing its value to something else.

In Swift, every variable has a type. The type tells you the kind of value that variable refers to. In the above example, the type of the variable is a String. Other types include Int and Double. You will learn about many more of Swift's data types as you work through these lessons. For now, don't worry much about the type; just be aware that every variable has a type.

The type of a variable is important, because it can only hold values of the same type, even if you change the value. favoriteCharacter must always be a string—it cannot change to an Int or a Double. Remember when you changed favoriteCharacter to be "Tyrion Lannister" instead of "Jon Snow"? You were able to do that because favoriteCharacter is a String and both of those are Strings. What happens if you tried to change favoriteCharacter to a number?

favoriteCharacter = 76

If you tried to do that, Swift would've spit out an error telling you that you cannot assign a variable of type String to an Int, and your program would not run.

Constants

Swift offers a second way to associate a name with a value: a constant. Like a variable, a constant consists of a name and an associated value. Unlike a variable, however, the value of a constant cannot change over time; once you initialize a constant (declare it and set it to a value), you cannot change it. Ever.

A Swift constant is declared like a variable, except it uses the keyword let instead of var:

let ultimateFavoriteCharacter = "Arya Stark"

From here on out, ultimateFavoriteCharacter will always have the value "Arya Stark". If you try to change it:

ultimateFavoriteCharacter = "Daenerys Targaryen"

Like variables, constants also have a type. Since you cannot change the value of a constant, you won't have to worry about changing the value to a different type. However, the type of constants still matters, since it dictates how you can interact with and use the constant, as you'll learn as you progress through your Swift studies.

Using Variables and Constants

Why use variables and constants in your program? Both variables and constants are useful when you want to associate a name that never changes with a value that may change. You can then refer to that variable by a constant name. As shown in the playground associated with this lesson, once you associate the name favoriteCharacter with a value ("Jon Snow", for example), you can use favoriteCharacter to refer to that value for the rest of the program, such as when you pass it to functions like print(). (You will learn more about functions in a future lesson.)

Choosing Between Variables and Constants

When should you use a variable, and when should you use a constant? A variable should be used when a value might change during the course of a single run of a program. For example, when you start running a program, your favorite character may be Jon Snow, but perhaps by the end of the program's run, you may want to switch your allegiance to another character (like when you changed the value of favoriteCharacter to "Tyrion Lannister"). A constant, on the other hand, is a way to bind a name to a value that won't change. This lets you refer to that value throughout your program where you are certain without a doubt that the value will never change.

View Variables and Constants on Learn.co and start learning to code for free.

swift-variables-readme-dc-web-career-040119's People

Contributors

jimcampagno avatar mdippery avatar annjohn avatar ianrahman avatar gj avatar susanlovaglio avatar zyam007 avatar

Watchers

 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.