Control Flow
Branches, loops, and exceptions, without parentheses around the condition clause (as in Python, Swift, Go, Ruby).
Conditional Branches
if a > b {
// ...
}
if a > b {
// ...
} else {
// ...
}
if a > b {
// ...
} else if a > c {
// ...
} else {
// ...
}
Chained comparison as in Cpp2 (Herb Sutter), Python, Julia.
if 1 <= x <= 10 { ... }
Loops
While
while a > b {
// ...
}
Do-While
do {
// ...
} while a > b
For-In
for str in ["a", "b", "c"] {
// ...
}
As in Swift, Rust.
Instead of AKA range-for in C++, for (... : ...) in C++/CLI, or for each in C#.foreach
Use the range operator to write:
for i in 1..10 { ... }
instead of ,for (Int i = 1; i <= 10; ++i) { ... }
translates to for i in Range(1, 10) { ... }.
for i in 0..<10 { ... }
instead of ,for (Int i = 0; i < 10; ++i) { ... }
translates to for i in RangeExclusiveEnd(0, 10) { ... }.
for i in 10..1:-1 { ... }
instead of ,for (Int i = 10; i >= 1; --i) { ... }
translates to for i in RangeByStep(10, 1, -1) { ... }.
I find this for-loop-syntax so intriguing that I accept the somewhat complex details of the range operator (with all its variants).
The variable is declared “with the loop”, with its type inferred from the range, array, etc. used as source (similar to var, but only with the options in, inout, copy, move; see Parameter Passing).
So for i in start..<end { <body> } is equivalent to:
{
var i = start
while i < end {
<body>
++i
}
}
Not every C/C++ for-loop can be expressed as a Cilia for-loop,
but then it (and in general, any C/C++ for-loop) can be converted into a while-loop.
for (<initialization>; <condition>; <increment>) {
<body>
}
can be written as
{
<initialization>
while <condition> {
<body>
<increment>
}
}
IMHO the code is even more clear when written as while-loop (though not so dense).
Note
When the<condition>is empty, then it needs to be replaced withTrue, sofor (;;) { ... }is translated towhile True { ... }.
Switch / Case
With implicit (like in Swift), i.e breakbreak is the default, and it is not necessary to explicitly write it. Use fallthrough if necessary.
switch i {
case 1:
print("1")
// implicit break
case 2, 3:
print("Either 2 or 3")
// implicit break
case 4:
// do something
fallthrough
case 5:
// do something more
print("4 or 5")
// implicit break
default:
print("default")
}
Exceptions
try {
// ...
} catch Exception ex {
print(ex)
} catch {
print("An unknown exception has occurred")
}
No , use RAII/RROD instead.finally