Control Flow
Conditional Statements
If / else
if (condition)
// Statement
else if (anotherCondition)
{
// Another statement
}
else
{
// Yet another statement
}
Switch / case
switch(variable)
{
case "A":
case "B":
// Do something
break;
case "C":
// Do something
break;
default:
// Do something
break;
}
Conditional operator
bool isGoldCustomer = true;
float price = (isGoldCustomer) ? 19.95f : 29.95f;
Iteration Statements
For loops
for (var i = 0; i < 10; i++)
{
// Do something 10 times
}
Foreach loops
Iterates over elements of a numerical object. In other words, it has a list nature--like arrays or strings.
foreach (var number in numbers)
{
// Do stuff
}
While loops
while (true)
{
// Do something infinitely
}
Do-while loops
The only difference with a do-while loop is that the code block is executed at least once because the condition is always checked after the code block runs.
do
{
// Something
} while (i < 10);
Break and continue
For any of these control flow statements, we have access to the break and continue keywords. break
will break out of the loop or flow. continue
will move onto the next iteration.
Last updated