Difference between revisions of "Selection"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "Selection uses if statements and switch statements. * if statements work by performing an action only if a condition is reached. <syntaxhighlight lang="csharp" line> if (a > b...")
 
Line 7: Line 7:
 
<syntaxhighlight lang="csharp" line>switch(x)
 
<syntaxhighlight lang="csharp" line>switch(x)
 
   { case 1:
 
   { case 1:
         x++
+
         x++;
 
         break;
 
         break;
 
     case 2:
 
     case 2:
         x--
+
         x--;
 
         break;
 
         break;
 
   }</syntaxhighlight>  
 
   }</syntaxhighlight>  
Line 16: Line 16:
 
<syntaxhighlight lang="csharp" line>switch(x)
 
<syntaxhighlight lang="csharp" line>switch(x)
 
   { case 1:
 
   { case 1:
         x++
+
         x++;
 
         break;
 
         break;
 
     case 2:
 
     case 2:
         x--
+
         x--;
 
         break;
 
         break;
    default:
+
    default:
           x*= x
+
           x*= x;
 
         break;
 
         break;
 
}</syntaxhighlight>
 
}</syntaxhighlight>

Revision as of 10:21, 15 December 2016

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 }