Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of 'break' in q#?

Tags:

break

q#

How would I break out of a loop when I meet a condition? For example:

for (i in 0..10){
    if (i==3){
        // equivalent of break
     }
}
like image 955
Mahathi Vempati Avatar asked Jul 07 '18 17:07

Mahathi Vempati


1 Answers

There is no break in Q#; however, you can implement this behavior using repeat-until-success loop.

Q# is not a general-purpose language, and is designed to allow a lot of optimizations for when a program will be executed on a quantum device. Loops are one example of such design: if you know beforehand how many iterations your loop will do, use a for loop, if you need to iterate until some condition is met, use repeat-until-success loop.

Your example (which is not really a good example of why you'd need a break) would be written as something like this:

mutable i = 0;
repeat {
    set i = i + 1;
} until (i == 10 || i == 3)
fixup {
    ();
}
like image 89
Mariia Mykhailova Avatar answered Nov 10 '22 08:11

Mariia Mykhailova