C

From TRCCompSci - AQA Computer Science
Revision as of 07:56, 20 June 2019 by Admin (talk | contribs) (Selection)
Jump to: navigation, search

General Skills

Helloworld

Constants - Variables - Data Types

There are 9 types of data types. These include:

  • boolean
  • string
  • integer
  • double
  • decimal
  • character
  • byte
  • float
  • long

To declare a variable you state the data type followed by the name of the variable:

DataType VariableName;

You can declare and assign a variable at the same time:

DataType VariableName = Value;

Examples

Boolean is a binary value, being either true or false.

bool example = true;


String is used for storing characters in word form (note the double quotes "").

string example = "hello world";


Integer is used to store whole numbers (32 bit values).

int example = 457568368;


Double is used to store numbers where there are numbers before and after the decimal point, using 64 bits of memory.

double example = 3.3;


Decimal is used to store larger numbers with information before and after the decimal point, using 128 bits of memory.

decimal example = 12.593;


Character is used to store single letters (note the single quotes ).

char example = 'A';


Byte is used to store a byte (eight bits). In C#, it is specifically an 8 bit integer (0-255).

byte example = 255;


Float is used to store a 32 bit decimal number, which must be defined as being a float using "F" or "f" to distinguish it from a double, or it may cause compilation errors.

float example = 9.4F;


Long integer is also used to store whole numbers (However these are 64 bit values).

long example = 7846354759340276482;

Local vs Global

It is important to know that a variable declared within a block is only available within that block, it doesn't exist outside of the block. These would therefore be local variables, because they are local to where they are declared.

You can therefore declare variables within your program and declare them outside of any block, these variables will be available anywhere within your program. These are called global variables, because they can be used throughout the program.

Constants

Constants are used to create a variable of a specified data type that can never be changed after declaration.

During runtime, once the constant is set the value cannot change during the execution of the program.

The only way to change it is to edit the code and re-compile.

const int example = 32

Selection

Selection uses if statements and switch statements. These allow your programs to make decisions or change the course of your program based on an input or a variable.

If / If Else

if statements work by performing an action only if a condition is reached. After the if command, the condition should be within round brackets, the contents of which should equate to true or false.

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.

1 if (a > b)
2   {
3     Console.WriteLine("A is greater than B");
4   }
5   else
6   {
7     Console.WriteLine("A is not Greater Than B");
8   }

Else If

 1 if (a > b)
 2   {
 3     Console.WriteLine("A is greater than B");
 4   }
 5   else if (a > c)
 6           {
 7              Console.WriteLine("A is greater than C");
 8           }
 9   else
10   {
11     Console.WriteLine("A is not Greater Than B or C");
12   }

Switch / Case

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 Statements

Both if and switch statements can be nested, meaning that one statement can be contained within another.

1  if (a > b)
2   {
3     if (b > 50)
4       {
5         Console.WriteLine("b is a very large number!");
6       }
7   }

Repetition - Iteration

Operators

Subroutines - Functions

Parameters - Global Variables

Advanced skills

Text Files

Binary Files

Handling Exceptions