Control Flow

conditionals, for-loops, while-loops and...just...loops?

If-statements in Rust do not need parentheses around their conditions, like with Java or C:


    

The result of an if-statement can immediately be assigned to a variable in Rust:


    

For-loops in Rust are syntactically similar to if-statements, as they also do not need parentheses:

As can be seen in the above example, ranges in Rust are defined with a double full stop between the lower and upper bounds. Variable i acts as an iterator for all integers between 0 and 10 (including 0, but excluding 10 - Rust ranges exclude the last element by default), while variable j iterates for all elements of arr.


    

Whether a Rust range includes or excludes the ending element can be specified:


    

Rust also has a third kind of loop, simply defined with the keyword loop, that is designed to run infinitely. The only way for the loop to finish execution is manually, with the break keyword:


    

Exercise:
Using a for-loop, while-loop, or the infinite loop explained above (or all three!), make the function below print the fifteen times table up to 50: