Table of contents
1.
Introduction
2.
Expressions
2.1.
Prefix Expressions
2.2.
In-Out Expression
2.3.
Infix Expressions
2.4.
Primary Expressions
2.5.
Literal Expression
3.
Statements
3.1.
Loop Statements
3.2.
Branch Statements
3.3.
Labelled Statement
3.4.
Control Transfer Statements
4.
Comments
4.1.
Single Line Comment
4.2.
Multiline Comment
5.
Frequently Asked Questions
5.1.
What is the most basic Swift expression?
5.2.
How are statements declared in Swift?
5.3.
How do you write comments in Swift?
6.
Conclusion
Last Updated: Mar 27, 2024

Swift Expression, Statements and Comments

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will go through how to read the user's standard Swift Expression, Statements and Comments.

In this blog, we'll master some programming fundamentals while using the Swift programming language in a modern, user-friendly environment.

Instead of boring you with theory, you'll get right to coding with Swift Playgrounds, which are sandbox-style environments where you can run code without building a complete programme.

You'll need Xcode 10, the standard development environment for macOS, which you can get here. Some of the code in this lesson will not work if you use Xcode version 9 or lower.

The toolchain refers to the collection of tools you employ to build software. The Integrated Development Environment is the toolchain component where you create your code (IDE). Xcode is your IDE, and it comes with playgrounds.

You'll use playgrounds to practise coding throughout this series, so it's crucial to know how they work. That is exactly what the remainder of this tutorial will teach you.

Expressions

Swift's four types of expressions are prefix expressions, infix expressions, primary expressions, and postfix expressions. You get a value, a side effect, or both when you evaluate a word.

Operators can be applied to smaller expressions using prefix and infix expressions. Primary expressions are the simplest type of expression in terms of notion, and they allow you to access values. Postfix expressions, like prefix and infix expressions, will enable you to combine postfixes like function calls and member access to create more sophisticated expressions. The sections following go over each type of expression in detail.

Variables, operators, literals, and functions are all part of an expression. For instance,

var marks = 80; / assign value to marks

var result = (num1 == num2); / compare num1 and num2

Expressions are as follows:

num1 == num2 - compares num1 and num2 var marks = 80 - indicates that we are assigning 80 to marks

The sections following go over some types of expression in detail.

Prefix Expressions

Prefix expressions combine an expression with an optional prefix operator. The expression that follows the prefix operator has only one argument.

See Basic Operators and Advanced Operators for further information on these operators' behaviour.

See Operator Declarations for further information on the Swift standard library's operators.

 

In-Out Expression

An in-out expression designates a variable supplied as an in-out argument to a function call expression.

&expression

See In-Out Parameters for further information and an example of in-out parameters.

As mentioned in Implicit Conversion to a Pointer Type, in-out expressions are also utilised when delivering a non-pointer argument in a situation where a pointer is required.
 

Infix Expressions

Infix expressions combine an infix binary operator with the left- and right-hand arguments. It is written as follows:

right-hand argument operator, left-hand argument operator

See Basic Operators and Advanced Operators for further information on these operators' behaviour.

See Operator Declarations for further information on the Swift standard library's operators.

NOTE

An expression of infix operators is represented as a flat list at parse time. The operator precedence is used to turn this list into a tree. The statement 2 + 3 * 5 is, for example, interpreted initially as a flat list of five items, 2, +, 3, *, and 5. It becomes the tree (2 + (3 * 5) due to this process.

 

Primary Expressions

The most fundamental type of expression is primary expression. They can be used alone as expressions or coupled with other tokens to form prefix, infix, and postfix expressions.

 

A PRIMARY EXPRESSION'S GRAMMAR

generic-argument-clause opt primary-expression identifier

literal-expression primary-expression

self-expression primary-expression

superclass-expression primary-expression

closure-expression vs primary-expression

parenthesized-expression primary-expression

tuple-expression primary-expression

implicit-member-expression primary-expression

wildcard-expression primary-expression

key-path-expression primary-expression

selector-expression vs primary-expression

key-path-string-expression primary-expression

Literal Expression

A conventional literal (such as a string or an integer), an array or dictionary literal, a playground literal, or one of the special literals listed below make up a literal expression.
 

Literal Type Value

#file String

The file's path.

#fileID String

Its file and module names.

#filePath String

The file's path.

It appears online #line Int.

It starts in column #column Int.

#function String

Its declaration's name.

#dsohandle UnsafeRawPointer

Where it appears, the handle of a dynamic shared object (DSO).

To facilitate migration from the previous #filePath behaviour to the new #fileID behaviour, the string value of #file depends on the language version. #file currently has the same value as #filePath. #file will have the same value as #fileID in a future version of Swift. To switch to the new behaviour, replace #file with #fileID or #filePath as needed.

A #fileID expression's string value is of the form module/file, where the file is the name of the file in which the word appears, and a module is the name of the module in which this file is included. A #filePath expression's string value is the whole file-system path to the file where the word appears.

Statements

In Swift, there are three sorts of statements: basic statements, compiler control statements, and control flow views. Simple words, which might be either an expression or a declaration, are the most prevalent. Compiler control statements, which comprise a conditional compilation block and a line control statement, allow the programme to modify parts of the compiler's behaviour.

Control flow statements are used to manage a program's execution flow. Control transfer statements, loop statements, and branch statements are all examples of control flow statements in Swift. Control transfer statements allow you to change the code's order. Loop statements will enable you to repeat a block of code, branch statements will enable you to execute a block of code only when certain conditions are fulfilled, and loop statements allow you to repeat a block of code. Swift also includes a do statement for introducing scope and handling errors, and a defer statement for performing cleanup activities shortly before the current coverage ends.

Note: 

A semicolon (;) can separate multiple statements on the same line if they occur on the same line.

Loop Statements

Loop statements allow you to run a block of code repeatedly based on the conditions you specify in the loop. The three loop statements in Swift are a for-in statement, a while statement, and a repeat-while statement.

A continue statement and a break statement can affect the control flow in a loop statement, as detailed in Break Statement and Continue Statement below.
 

A LOOP STATEMENT'S GRAMMAR

for-in-statement loop-statement

while-statement loop-statement

repeat-while-statement loop-statement

Branch Statements

Branch statements enable the programme to run specific sections of code based on the results of one or more conditions. The values of the branch statement's requirements determine how the programme branches and, as a result, which block of code is run. An if information, a guard statement, and a switch statement are the three branch statements in Swift.

As the Break Statement below explains, a break statement can change the control flow in an if or switch information.

 

A BRANCH STATEMENT'S GRAMMAR

if-statement branch-statement

guard-statement branch-statement

switch-statement versus branch-statement
 

Labelled Statement

A statement label, which consists of the label's name followed by a colon, can preface a loop statement and, if checked, a switch statement or a do statement (:). As explained in Break Statement and Continue Statement below, use statement labels with break and continue ideas to be specific about how you intend to shift control flow in a loop statement or a switch statement.

A labelled statement's scope is the complete statement after the statement label. Labelled statements can be nested, but each statement label must have its name.

See Labeled Statements in Control Flow for more information and examples of utilising statement labels.

 

A LABELED STATEMENT'S GRAMMAR

statement-label loop-statement labeled-statement

if-statement statement-label labeled-statement

statement-label switch-statement labeled-statement

statement-label do-statement labeled-statement

label-name: statement-label

identifier label-name
 

Control Transfer Statements

By unconditionally moving programme control from one piece of code to another, control transfer instructions can affect the order in which code in your programme is executed. The five control transfer statements in Swift are a break statement, a fallthrough statement, a return statement, a continue statement and a throw statement.

 

STATEMENT OF CONTROL TRANSFER GRAMMAR

break-statement control-transfer-statement

continue-statement control-transfer-statement

fallthrough-statement control-transfer-statement

return-statement control-transfer-statement

throw-statement control-transfer-statement

Comments

Comments are clues that we use in computer programming to clarify our code.

The compiler entirely disregards comments. They're aimed toward other programmers.

In Swift, there are two ways to add comments:

/ - Comments on a Single Line 

/*...*/ - Comments on Multiple Lines

Single Line Comment

A single-line comment in Swift is any line that begins with /. For instance,

var name = "Cartman" / create a variable

/ print out the value (name)

We've made two single-line comments here:

/ Initialize a variable

/ Display the value

Along with the code, we can also use a single-line comment.

/ name is a string var name = "swift"

Multiline Comment

Any content between /* and */ in Swift is a multiline comment. For instance,

/* Create a variable to store employee salaries */

print var salary = 10,000 (salary)

We used /*...*/ to write a comment that spans many lines in the example above.

Frequently Asked Questions

What is the most basic Swift expression?

Primary Expressions The most basic type of expression is a direct expression. They can be used alone as expressions or coupled with other tokens to form prefix, infix, and postfix expressions.

How are statements declared in Swift?

The func keyword declares functions in the following format: return type statements func function name(parameters).

How do you write comments in Swift?

In Swift, you have two options for adding comments:

                  / - Comments on a single line.

      /*... */ - Comments on multiple lines.

Conclusion

In this blog, we have extensively discussed the Swift Inputs and OutputsOperators can be applied to smaller expressions with prefix and infix expressions. Primary expressions are the most basic type of expression, and they allow you to access values. And to learn in-depth about Swift functions, check out the course on our Swift on the Coding Ninjas website. And also, check out the excellent content on the Coding Ninjas Website, Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test Series. Do upvote our blog to help other ninjas grow.

Recommended Readings:

Delighted Programming!

Live masterclass