C# Conditionals and Loops
Control flow with if statements and loops
If-Else Statements
if (condition) { # check condition
// code if true
} else if (condition2) { # additional check
// code
} else { # default case
// code
}
Switch Statement
switch (variable) { # evaluate variable
case 1: # if variable is 1
// code
break; # exit switch
case 2: # if variable is 2
// code
break;
default: # if no match
// code
break;
}
For Loop
for (int i = 0; i < 10; i++) { # loop 10 times
Console.WriteLine(i); # print index
}
foreach (var item in collection) { # iterate collection
Console.WriteLine(item); # print each item
}
While and Do-While
while (condition) { # repeat while true
// code
}
do { # execute at least once
// code
} while (condition); # check after
Loop Control
break; # exit loop
continue; # skip to next iteration
return; # exit method