Q:

Swift program to demonstrate the Half-Open range operator

belongs to collection: Swift Basic Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Swift Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Swift program to demonstrate the one-sided range o... >>
<< Swift program to demonstrate the closed range oper...