Difference between revisions of "Selection"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
Line 1: Line 1:
 
Selection uses if statements and switch statements.
 
Selection uses if statements and switch statements.
 
* if statements work by performing an action only if a condition is reached.
 
* if statements work by performing an action only if a condition is reached.
<syntaxhighlight lang="csharp" line> if (a > b)
+
<syntaxhighlight lang="csharp" line>if (a > b)
            { Console.WriteLine("A is greater than B"); }</syntaxhighlight>  
+
  {
I can also be paired with else, which will perform an action if the "if" condition is not met.
+
    Console.WriteLine("A is greater than B");
 +
  }</syntaxhighlight>
 +
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.
 
* switch is basically a combined if and if else statement, and is used for a lot of different options.
 
<syntaxhighlight lang="csharp" line>switch(x)
 
<syntaxhighlight lang="csharp" line>switch(x)
   { case 1:
+
   {  
        x++;
+
    case 1:
        break;
+
      x++;
 +
      break;
 
     case 2:
 
     case 2:
        x--;
+
      x--;
        break;
+
      break;
 
   }</syntaxhighlight>  
 
   }</syntaxhighlight>  
 
Default is an optional part of the switch method, which is used in case none of the other conditions are met.
 
Default is an optional part of the switch method, which is used in case none of the other conditions are met.
 
<syntaxhighlight lang="csharp" line>switch(x)
 
<syntaxhighlight lang="csharp" line>switch(x)
   { case 1:
+
   {  
        x++;
+
    case 1:
        break;
+
      x++;
 +
      break;
 
     case 2:
 
     case 2:
        x--;
+
      x--;
        break;
+
      break;
 
     default:
 
     default:
          x*= x;
+
      x*= x;
        break;
+
      break;
}</syntaxhighlight>
+
  }</syntaxhighlight>
 +
 
 +
<h2>Nesting</h2>
 +
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!");
 +
      }
 +
  }

Revision as of 18:49, 16 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   {
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!");
     }
 }