Swift program to demonstrate the Half-Open range operator

Here, we are going to demonstrate the Half-Open range operator in Swift programming language.
Submitted by Nidhi, on June 01, 2021

Problem Solution:

Here, we will print the square of the number from 1 to 9 using the Half-Open range operator. The Half-Open range operator (X..<Y) specifies the range of numbers from X to Y but it does not include Y.

Program/Source Code:

The source code to demonstrate the Half-Open range operator is given below. The given program is compiled and executed successfully.

// Swift program to demonstrate the
// Half-Open range operator

var res:Int=0;

for val in 1..<10 {
    res = val*val;
    print(res);
}

Output:

1
4
9
16
25
36
49
64
81

...Program finished with exit code 0
Press ENTER to exit console.

Explanation:

In the above program, we imported a package Swift to use the print() function using the below statement,

import Swift;

Here, we created integer variable res that is initialized 0 respectively. Then we iterated the numbers from 1 to 9 using the half-open range operator in the for loop and printed the square of numbers from 1 to 9 on the console screen.

Swift Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.