Swift If Statement

  Total Views : 263
  Comments : 0
  Keywords : Swift if statement is used to check if given condition is true or not with examples, Swift check if condition to execute set of statements with example, Swift if statement with examples

Here we will learn if condition or statement in swift with examples and how to use swift if condition to execute set of statements if defined condition is TRUE with examples.

Swift If Statement

In swift, if statement or condition is used to execute set of statements if defined condition is TRUE.

 

Whenever we have specific requirement like execute set of block statements only if given condition is TRUE then we can use swift if statement.

 

Now we will see the functionality of if statement in swift programming language using algorithm diagram.

Swift If Statement Flow Diagram

Following is the swift if statement flow diagram to represent how the functionality of if statement will work in swift programming language.

 

Swift If Statement Execution Flow Chart Diagram

 

If you observe above swift if condition flow diagram first it will evaluates the given condition is TRUE or FALSE. If given condition is satisfies (TRUE) then it will execute the code block within if statement. In case the given condition is FLASE then if condition block will skip the execution of given code block and reach the end of statement.

Syntax of Swift If Statement

Following is the syntax of defining if statement in swift programming language.

 

if condition

{

//Expressions will execute only when condition true

}

If you observe above if statement syntax if defined "condition" TRUE then only the statements inside of if statement will execute.

 

Now we will see how to use if statement with examples in swift programming language.

Swift If Statement Example

Following is the simple example using if statement in swift to check the condition before execute the expressions within the condition.

 

var num1: Int = 40

if num1 > 20 {

print("Number Greater than 20")

}

If you observe above example we are checking the condition whether the value of “num1” greater 20 or not.

 

When we execute above code in swift playground we will get result like as shown below.

 

Number Greater than 20

This is how we can use swift if statement to execute defined expressions based on our requirements. 

 

PREVIOUS

Swift Optionals
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User