Selection
Revision as of 18:49, 16 December 2016 by QuantumFluctuator (talk | contribs)
Selection uses if statements and switch statements.
- if statements work by performing an action only if a condition is reached.
1 if (a > b)
2 {
3 Console.WriteLine("A is greater than B");
4 }
This can also be paired with else, which will perform an action if the "if" condition is not met.
- switch is basically a combined if and if else statement, and is used for a lot of different options.
1 switch(x)
2 {
3 case 1:
4 x++;
5 break;
6 case 2:
7 x--;
8 break;
9 }
Default is an optional part of the switch method, which is used in case none of the other conditions are met.
1 switch(x)
2 {
3 case 1:
4 x++;
5 break;
6 case 2:
7 x--;
8 break;
9 default:
10 x*= x;
11 break;
12 }
Nesting
Both if and switch statements can be nested, meaning that one statement can be contained within another. <syntaxhighlight lang="csharp" line>if (a > b)
{ if (b > 50) { Console.WriteLine("b is a very large number!"); } }