Control Flow
if
and else
They are the same. But Kotlin support also if-else expression
while
and do-while
, break
and continue
They are the same: for Dart see here. For Kotlin see here
for
loops
standard loop
//standard for loop
for (i in 1..3 step 1) {
println(i) //1 2 3
}
//standard for loop: step 1 can be omitted
for (i in 1..3) {
println(i) //1 2 3
}
//standard reversed loop (note that the step is POSITIVE!)
for (i in 6 downTo 0 step 2) {
println(i) //6 4 2 0
}
//standard reversed loop: step 1 can be omitted
for (i in 6 downTo 0) {
println(i) //6 5 4 3 2 1 0
}
//standard for loop
for (var i=1; i<=3; ++i) {
print(i); //1 2 3
}
//standard reversed loop
for (var i=6; i>=0; i-=2) {
print(i); //6 4 2 0
}
loop variable captured in closure
//Closures inside of Kotlins’s for loops capture the value of the index
var callbacks = mutableListOf<()->Unit>()
for (i in 0..2) {
callbacks.add({ println(i)})
}
// print 0 and then 1
callbacks.forEach{ c-> c() }
//Closures inside of Dart’s for loops capture the value of the index
var callbacks = [];
for (var i = 0; i < 2; i++) {
callbacks.add(() => print(i));
}
// print 0 and then 1
callbacks.forEach((c) => c());
loop over iterable
For Dart see iteration in library-tour. For Kotlin see for loops and iterable in stdlib
var cities = listOf("Rome","Paris","London")
//iterate over elements of iterable
// the same as in Dart see
// see also
for(city:String in cities) //the String type annotation can be omitted
{
println(city)
}
//iterate over elements of iterable
//see
var cities=["Rome","Paris","London"];
for(final city in cities) //instead "final" we can specify "var" or the explicit type (String)
{
print(city);
}
iterable.forEach()
For Dart see forEach. For Kotlin see forEach in stdlib
//iterable has also a forEach method for iterating over the elements
cities.forEach { city-> println(city) }
//or more simply
cities.forEach{ println(it) }
//Iterable classes also have a forEach() method as another option:
cities.forEach((city) {print(city);});
//or more simply
cities.forEach(print);
switch
and case
For Dart see switch and case. For Kotlin see when expression.
var job = "nodejs dev";
val onJavascriptDev = { println("a Javascript dev") }
when (job) {
"nodejs dev" -> {
println("a nodejs dev")
onJavascriptDev(); //no support to jump to another label like in Dart
}
"frontend dev" -> {
println("a frontend deb");
onJavascriptDev() //no support to jump to another label like in Dart
}
"android dev", "iOS dev" -> println("A mobile dev")
"javascript dev" -> onJavascriptDev()
else -> println("Unknown job")
}
var job='nodejs dev';
switch (job) {
case 'nodejs dev':
print("a nodejs dev");
continue javascript_dev;
case 'frontend dev':
print("a frontend deb");
continue javascript_dev;
case 'android dev':
case 'iOS dev':
print("A mobile dev");
break;
javascript_dev:
case 'javascript dev':
print("a Javascript dev");
break;
default:
print("Unknown job");
}
case
fall-through is prohibited
Only empty case
fall-through is allowed.
Dart requires break
at the end of each case
. Other valid ways to end a non-empty case clause are a continue
(continue execution to another clause), throw
, or return
statement.
overriding == for compared object is prohibited
Dart does not allow classes for objects that are compared in a switch
statement to override operator==
assert
in Dart Debug mode use assert(condition, optionalMessage)
to disrupt normal execution if a boolean condition is false.
In Kotlin assert is supported for JVM and Native platforms;
// Make sure the variable has a non-null value.
assert(text != null)
// Make sure the value is less than 100.
assert(number < 100)
// Make sure this is an https URL.
//To attach a message to an assertion, add a string as the second argument to assert
assert(urlString.startsWith("https")){"URL ($urlString) should start with \"https\"."}
// Make sure the variable has a non-null value.
assert(text != null);
// Make sure the value is less than 100.
assert(number < 100);
// Make sure this is an https URL.
//To attach a message to an assertion, add a string as the second argument to assert
assert(urlString.startsWith('https'),'URL ($urlString) should start with "https".');