Branches
Intro
Branches in a computer program help to build a logic process.
So we can do something like "If this case is true, than execute this piece of code".
if
bool condition = true;
if (condition == true) {
// some code
}
Please notice that a compare operator ( == ) , as used in line 2, always returns a boolean value. The if satement always requires an expression, that returns a boolean in the end:
bool condition = true;
if (condition) {
// some code
}
int[] numbers = { 3, 4, 5 };
if (3 > 4 || numbers.Count() == 3) {
// some code
}
static bool IsOnline() {
// some code
return true;
}
if (IsOnline()) {
// some code
}
Visualization in a flow chart diagramm:
if else
Example:
bool isCooked = true;
if (isCooked == true) {
Console.WriteLine("Meal is cooked");
} else {
Console.WriteLine("Meal is not cooked");
}
Line 3 or Line 5 is executed
switch
Visualization in a flow chart diagramm:
Example:
int number = 12;
switch (number) {
case 1:
Console.WriteLine("number is 1");
break;
case 2:
case 3:
case 4:
case 5:
Console.WriteLine("number is between 2 and 5");
break;
default:
Console.WriteLine("undefined");
break;
}
Shortcuts
Description | Keybinding (english layout!) |
---|---|
Autocomplete If Case | if, Tab, Tab |