Description
In the README you say:
Break keyword in Vala
break;
To this day, I am still not entirely sure what the
break
keyword does, but most languages support it.
I'm not sure why you wouldn't know this given your experience, but break
is used for exiting loops or switch statements. So, for example, you could do this absurd while
loop:
int i = 0;
while true {
i++
if (i > 99) {
break;
}
}
(I say this is absurd because you would usually use a for
loop instead.)
Or:
switch(color) {
case "yellow":
print("yellow");
break;
case "red" :
print("red");
break;
case "green":
print("green");
break;
case "purple":
print("purple");
break;
default:
print("dunno");
break;
}
print("done!");
(This might not quite be accurate Vala syntax. I don't use Vala very much.)
Basically without the break
, even after matching, the switch
will "fall through" and check the input variable against the next case. Sometimes you want this, but usually you don't. Incidentally some languages automatically break in switch
statements even without the break
keyword.
The Python equivalent is like this:
#
# with break
#
if color == "yellow":
print("yellow")
elif color == "red":
print("red")
#
# without break
#
if color == "yellow":
print("yellow")
if color == "red":
print("red")
Do you see the difference? break
works kind of like elif
(for additional cases) or else
(for default
).
Anyway personally I hate switch
statements because they use weird syntax and are less intuitive to read. The Python-style series of if
statements is much more legible. break
is still useful for weird loop conditions, though.