Contents:
Variables:
To use variables you need to declare they type before use:
// <VARIABLE_TYPE> <VARIABLE_NAME>;
int n;
Types:
- int - integer e.g. -1, 0 100,
- bool - logic, can be either true or false,
- double (and float with lower precision) - real numbers, e.g. -1.2, 0.5, 1000.2,
- char - character, e.g. 'a', 'b', 'c', '0', '-',
- string - string of characters, e.g. "word", "a" (single sign string), "" (empty string),
Basic Operations:
// assignment "="
i = 0 ;
// declaration with initialization (create variable with initiale value)
int i = 0;
// addition, multiplication, division
c = a + b/2.0 +(a+1)/4;
// exponentiation (a^b)
Math.Pow(2,3); // gives 8
// square root:
Math.Sqrt(4); // gives 2
if
if(CONDITION)
{
INSTRUCTIONS_IF_TRUE;
}
if/else
if(CONDITION)
{
INSTRUCTIONS_IF_TRUE;
}
else
{
INSTRUCTIONS_IF_FALSE;
}
if/else/if/else/if
You can chain as many i/else statements as you want:
if(C1)
INSTRUCTIONS1;
else if(C2)
INSTRUCTIONS2;
else if(C3)
INSTRUCTIONS3;
else if(C4)
INSTRUCTIONS4;
else
INSTRUCTIONS_ELSE;
for (repeat something n times)
Syntax:
for( INITIALIZATION; CONDITION; STEP)
{
INSTRUCTIONS;
}
Example
for( int i =0; i < 10; i++)
{
Console.WriteLine(i);
}
Result:
0
1
2
3
4
5
6
7
8
9
foreach
Iterate a container (e.g. list or array) without indexing:
Syntax:
foreach( TYPE VARIABLE in CONTAINER)
{
INSTRUCTIONS;
}
Example
List<int> my_list = new List<int>(){0,1,2,3,4,5,6,7,8,9};
foreach(int element in my_list)
{
Console.WriteLine(element);
}
Result:
0
1
2
3
4
5
6
7
8
9
Arrays
- array type is BASE_TYPE[] e.g. int[], char[], string[] (array of integers, chars, strings etc.)
- lists containt elements of one type
- list's size cannot be changed after its creation (you can create a list of arbitrary size)
- to get size of the list use list.Length (no parentheses)
- arrays are initially filled with 0 (or 0 equivallent for nonnumerical types)
Creating a new array of integers (filled with 0) with n elements:
int[] tab = new int[n];
Creating an array with some values ( e.g. 1, 2, 3 )
int[] ar = new int[]{1, 2, 3};
Lists
- lists containt elements of one type
- list's size can be changed (e.g. by adding or removing) elements
- to get current size of the list use list.Count()
Creating an empty list of integers:
List<int> my_list = new List<int>();
Creating a list with some values ( e.g. 1, 2, 3 )
List<int> my_list = new List<int>(){1, 2, 3};
Misc:
Sorting
// Sorts a list in increasing order e.g. 1, 2, 3, 4 ,5 ...1000:
my_list.Sort();
Maximum and minimum value
int minimum = my_list.Mint();
int maximum = my_list.Max();
Printing and Reading (Console)
Write something:
Console.Write("Something")
Write something and end a line:
Console.WriteLine("Something")
Read a single line:
string the_line = Console.ReadLine();
Read a single key:
char key = Console.ReadKey();
Functions
Syntax
public static TYPE NAME([ARGUMENTS])
{
INSTRUCTIONS;
return X; // Required when TYPE is not 'void'
}
Examples
A function with no arguments that rreturns nothing and prints "hello"
public static void PrintHello()
{
Console.WriteLine("hello");
}
A function that adds 2 numbers and returns the result
pblic static int add(int a, int b)
{
return a+b;
}