Introduction to Swift: Constants, Variables & Data Types

Overview

If you are new to programming and have never been exposed to programming, rest assured that this tutorial is created just for you so that you have the basic idea of programming languages. The concept for most of the programming languages are the same, but the way the codes are written are sometimes different. Swift continues to become my favorite programming language and there are a lot of things that you could do with programming language. You could invent whatever you want. This why I love programming.

Words that you need to be familiar with:

  • Variable
  • Constant
  • Value

Let’s begin with the basic

First, we need to understand that data plays a great role in programming. However, there are many different datas being represented in programming. If you look at the table below, these are the most common data used. With Swift, you could immediately declare a variable value without indicating the type name.

It’s always good to practice what you learn. If you have Xcode, create your first playground and start coding. Bear in mind that Xcode is only available on Mac. If you do not have a Mac and have the desire to learn, feel free to visit this online site to program in Swift without a Mac.

String

Let’s do something with string:

Always remember that when you assign a string value/data to a variable, always include opening parentheses and closing parentheses just like below or else you will receive error.

By writing the code with var, we are creating a variable called number in this case and we are storing a value of Four in it. Four is considered as string at this point because it is a text. In short, the variable number contains a value of Four. To verify this, simply insert the following command which will indicate the value of the variable.

Now let’s do something with integer:

Integer

With integer, we do not any opening or closing parentheses. Here, we are storing 5 into a variable called five. To verify that 5 is indeed in five, we could do the same step as previous which is to print it out.

 

Double

Isn’t programming very fun? I hope you are having fun now. You could definitely try exploring a little more. However, we are not done yet. Let’s find out what double could do. Double is somewhat similar to integer with the exception that with double, you could see more numbers behind those point.

For example, given a number of 2.56. With integer type, it will show 2. But with double type, it will show 2.56. Still don’t get it? Let’s dive in.

By printing the value of the data, we get the following

Boolean

Boolean only contains two value, it is either true or false. You couldn’t assign any value except for either assigning a true value or a false value. This is often used to determine cases. Here I am assigning admitted to true and it goes through the logic operator to see if the admitted is true or false.

 

 

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*