Selection

From TRCCompSci - AQA Computer Science
Revision as of 10:21, 15 December 2016 by Robbie Rotten (talk | contribs)
Jump to: navigation, search

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             { Console.WriteLine("A is greater than B"); }

I 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   { case 1:
3          x++;
4         break;
5     case 2:
6          x--;
7         break;
8   }

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   { case 1:
 3          x++;
 4         break;
 5     case 2:
 6          x--;
 7         break;
 8     default:
 9           x*= x;
10         break;
11 }