• Home
  • Troubleshoot
  • #Example
    • C
    • C++
    • Python
    • R-Programming
  • DSA
  • Quiz
  • Tutorial Videos
  • Home
  • Troubleshoot
  • #Example
    • C
    • C++
    • Python
    • R-Programming
  • DSA
  • Quiz
  • Tutorial Videos
  • #Deals
  • #News
  • #WiKi
  • #APPS
  • #Events
    • #WWDC
    • #I/O
    • #Ignite
  • #Let’s Talk

MyCodeTips mycodetips-newlogocopy1

  • Home
  • Troubleshoot
  • #Example
    • C
    • C++
    • Python
    • R-Programming
  • DSA
  • Quiz
  • Tutorial Videos
Swift

Conditionals in Swift ( If, Else If, Else )

Conditionals in Swift, Conditionals to make decisions in your Swift code, with if, else if, and else. If this happens, then do that. This is called control flow because you use it to control the way your code flows.

In this tutorial, you’ll learn how to use the if-statement in your Swift code. We’ll get into boolean logic, expressions, operators, and the syntax of if, else if, and else blocks.

if statement can be followed by an optional else if…else statement, which is very useful to test various conditions using a single if…else if statement.

In Swift, there are three forms of the if…else statement.

  • if statement
  • if…else statement
  • if…else if…else statement

When using if, else if, else statements, there are a few points to keep in mind.

  • An if can have zero or one else’s and it must come after any else if’s.
  • An if can have zero to many else if’s and they must come before the else.
  • Once an else if succeeds, none of the remaining else if’s or else’s will be tested.

Swift if Statement

if boolean_expression_1 {
   /* Code here */
} else if boolean_expression_2 {
   /* code here */
} else {
   /* Executes when the none of the above condition is true */
}

#Example

let number = 5

// check if number is greater than 0
if (number > 0) {
  print("Number is positive.")
}
print("The if statement is easy")

we have created a variable named number. Notice the test condition, number > 0
since the number is greater than 0, the condition evaluates true.

Swift if…else Statement

An if statement can have an optional else clause.

The syntax of the if-else statement is

if (condition) {
  // block of code if condition is true
}
else {
  // block of code if condition is false
}

The if…else statement evaluates the condition inside the parenthesis.

If the condition evaluates to true,

  • the code inside if is executed
  • the code inside else is skipped
#Example

let number = 5

if (number > 0) {
    print("Number is positive.")
}

else {
    print("Number is negative.")
}

print("This statement is always executed.")

Swift if…else if…else Statement

The if…else statement is used to execute a block of code among two alternatives.

However, if you need to make a choice between more than two alternatives, then we use the if…else if…else statement.

The syntax of the if…else if…else statement is

if (condition1) {
    // code block 1
}

else if (condition2){
    // code block 2
}

else {
    // code block 3
}

If condition1 evaluates to true, code block 1 is executed.
If condition1 evaluates to false, then condition2 is evaluated.
If condition2 is true, code block 2 is executed.
If condition2 is false, code block 3 is executed.

#Example 

let number = 0

if (number > 0) {
    print("Number is positive.")
}

else if (number < 0) {
    print("Number is negative")
}

else {
    print("Number is 0.")
}

print("This statement is always executed")

Swift nested if Statement

You can also use an if statement inside of an if statement. This is known as a nested if statement.

The syntax of nested if statement is

// outer if statement
if (condition1) {
    // statements

    // inner if statement
    if (condition2) {
    // statements
    }
}

Happy Swift Coding 🙂

  • Click to share on Reddit (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
Written by Ranjan - 716 Views
Tags | Apps, IOS, swift, swiftUI
AUTHOR
Ranjan

I m Ranjan and Sharing my years of experience in Software Development. Love to code in Mobile apps (IOS, Android, Power Apps, Xamarin, Flutter), Machine Learning ( Beginner ), Dot Net, Databases ( SQL Server, MySql, SQLite), WordPress, Cloud Computing ( AWS, Azure, Google, MongoDB) and many more as required on project-specific. Besides this love to travel and cook.

You Might Also Like

iOS Logo

Consuming WCF Web Services in an iOS Application

March 5, 2014
android-12-features

Android 12 Features: So far So Good

August 1, 2021
mycodetips Xcode

Tips to create dropdown list in IOS

December 9, 2013
Next Post
Previous Post

Subscribe for updates

Join 6,915 other subscribers

whiteboard

Whiteboard(PRO)

whiteboard

Whiteboard(lite)

alphabets

Kids Alphabet

techlynk

Techlynk

techbyte

Do2Day

techbyte

Techbyte

Latest Posts

  • YT-Featured-Algorithm
    What is Algorithm?
  • Frameworks of IOS
    Frameworks of IOS – Part ( I )
  • NSFileManager or NSPathUtilities
    NSFileManager or NSPathUtilities in Objective-C
  • Passing data between view controllers in Objective-C
    Passing data between view controllers in Objective-C
  • structures-classes-enum
    Structures and Classes in swift !
  • control-system-swift
    Control Flow in Swift
  • swift-concurrency-await
    Concurrency in Swift
  • time-complexity-dsa
    The Term Time Complexity in DSA
  • objective-c-datatypes1
    Objective-C Data Types
  • Convert-jpeg-word
    Convert JPG to Word – Tips You Should Try!

Quick Links

  • #about
  • #myapps
  • #contact
  • #privacy

Other Websites

  • #myQuestions
  • #myBhojanalaya
  • #gadgetFacts
  • #ifscCodesDB

Tag Cloud

Algorithm Android Android Studio API APP Programming Apps blogging Browser Config CSS DATABASE dsa ecommerce error Features Google IO HTML HTML5 IDE installation Interview Questions IOS iPhone javascript Mac objective-c OneDrive OS Placeholder Programming quicktips SDK SEO Settings SMO SQL swift swiftUI Teams Tips & Tricks Tools UI Web Wordpress Xcode

©mycodetips.com