How to loop through the elements of an array and access them by their index in Q# language since the conventional looping in C++ doesn't work here.
Asked
Active
Viewed 96 times
1 Answers
2
Suppose the array is arr = T[], where T is any type in Q#.
let n = Length(arr);
for (i in 0 .. (n-1)) {
// use the element arr[i]
}
It is to be noted that if arr is defined using 'let' then the values are immutable and can be accessed but not assigned. If it is defined using 'mutable' literal then the elements can also be set using the 'set' keyword. In that case define the array is follows
mutable arr = new T[N]
where N is the length required.

Mariia Mykhailova
- 1,987
- 1
- 9
- 18

Indrajit Banerjee
- 408
- 3
- 12