Unity 3D Training Camp

Coding with C# Part-1

Coding with C# Part-1

1- Data & Computers

2- Data Types

4- Functions and parameters

5- Arrays

6- Loops & Conditions

3- Operations on Data

Coding with C# Part-1

1-Data & Computers

What's Data ?

How computers treat data ?

how to use data ?

1-Data & Computers

Data is a sequence of information that comes in different types 

What's Data ?

1-Data & Computers

How computers treat data ?

1-Data & Computers

how to use a data ?

var name = data ;
var age = 21;
var name = "Youcef Madadi";

Example

Coding with C# Part-1

2- Data Types

Numerical types.

symbolic types.

Reference types.

Boolean type.

2-Data types

Integer: all numbers in Z group. (use 16-32bit)

Float (real) : all numbers in R group (use 32bit)

double (real): same as float just use 64bit of memory.

Numerical types.

2-Data types

How to use a numerical Data

int age =21;
float physic = 15.5f;
double math = 2.25;

The reason why we write f in float is to see the difference between float and double, because both of them represent Real Numbers just the capacities are different .

2-Data types

symbolic types

to use symbols and characters we have a basic table called ASCII represented with numerical data too.

But C# language Uses Unicode .it's similar to ASCII just with more character ,and it's represented on 32bit.

to learn more check this web-page :

https://unicode-table.com/en/#control-character

2-Data types

How to use a symbolic Data

char character = 'A';
char symbole = '@';
string word = "Hello";
string phrase = "Hello everyone";

2-Data types

Boolean type.

bool btrue = true;
bool bfalse = false;

they are used to catch conditions results or as flags.

2-Data types

Reference types.

int age = 10;
ref int _Age = ref age;
_Age = 20;
print(age);
print(_Age);
//output:
//	20
//	20

Coding with C# Part-1

3- Operations on Data

Numerical operations.

symbolic operations.

Boolean operations.

3- Operations on Data

Numerical operations.

int x = 5, y = 6;
int res1 = x + y;	//res1 == 11
int res2 = x - y;	//res2 == -1
int res3 = x * y;	//res3 == 30
int res4 = x / y;	//res4 == 0
int res5 = x % y;	//res5 == 5
x++;	//x == 6
y--;	//y == 5

3- Operations on Data

Numerical operations.

float x = 5.5f, y = 6.2f;
float res1 = x + y;	//res1 == 11.7
float res2 = x - y;	//res2 == -0.7
float res3 = x * y;	//res3 == 34.1‬
float res4 = x / y;	//res4 == 0.887
float res5 = x % y;	//res5 == 5.5
double x = 5.5, y = 6.2;
double res1 = x + y;	//res1 == 11.7
double res2 = x - y;	//res2 == -0.7
double res3 = x * y;	//res3 == 34.1‬
double res4 = x / y;	//res4 == 0.887
double res5 = x % y;	//res5 == 5.5

3- Operations on Data

symbolic operations.

char x = '1', y = 'a';
char res1 = (char)90;	//res1 == 'Z'
char res2 = (char)((int)x + 9);	//res2 == '9'
char res3 = ++y;	//res3 == 'b' and y == 'b'
char res3 = y++;	//res4 == 'b' and y == 'c'
x++;	//x == '2'

3- Operations on Data

symbolic operations.

string x = "hello", y = "Game", z = "Devalopers";
string res1 = x + y;	//res1 == "helloGame"
string res2 = x +" "+ y +" "+ z;	//res2 == "hello Game Devalopers"

3- Operations on Data

Boolean operations.

bool a = true, b = false;
bool res1 = a && b;	//res1 == false (and operator)
bool res2 = a || b;	//res2 == true (Or operator)
bool res3 = !a;	//res3 == false (not operator)

3- Operations on Data

Boolean operations.

int left = 5, right = 8;
bool res4 = left > right;	//res4 == false
bool res5 = left < right;	//res5 == true
bool res6 = left == right;	//res6 == false
bool res7 = left >= right;	//res7 == false
bool res8 = left <= right;	//res8 == true
bool res9 = left != right;	//res9 == true

3- Operations on Data

Boolean operations.

string left = "hello", right = "game";
bool res10 = left == right;	//res10 == false
bool res11 = left != right;	//res11 == true

Coding with C# Part-1

4- Functions and parameters

how to create a functions ?

functions Types.

functions parameters.

What are functions ?

4- Functions and parameters

What are functions ?

functions are a set of instruction that usually used many times.

4- Functions and parameters

how to create a functions ?

type functionName(){
	.
	.
	instractions;
	.
	.
}

4- Functions and parameters

functions Types.

it depends on the return type.

if there is not type we write void

int two(){
	return 2;
}
void two(){
	print("two");
}

4- Functions and parameters

functions parameters.

functions parameters are values passed with the function to use them to get results.

{
  int x=5,y=6,z;
  z=addition(x,y);
}

int addition(int a,int b){
	return a + b;
}

4- Functions and parameters

functions parameters.

we can change the values of the variables called within the function if we use the word ref.

{
  int x=5,y=6,z;
  @switch(ref x,ref y);
}
void @switch(ref int a,ref int b){
	int tmp = a;
    a = b;
    b = tmp;
}

Coding with C# Part-1

5- Arrays

what is an array ?

How to create an array ?

how to use an array?

5- Arrays

what is an array ?

array is a set of data of the same type help us store more data using less variables.

5- Arrays

How to create an array ?

type[] arrayName = new type[size];
int[] ages = new int[25];

Example :

string[] Names = new int[25];

5- Arrays

how to use an array?

int[] ages = new int[24];
ages[0] = 21;
ages[1] = 19;
ages[2] = ages[0];
	.
	.
	.
ages[24] = ages[10] + ages[22];
print(ages[12]);

Coding with C# Part-1

6- Conditions & Loops

what are Conditions ?

if else, if else if. 

switch case default

what are loops ?

for loop

while & do while  loop

6- Conditions & Loops

what are Conditions ?

Conditions are control structures used to check which instruction set to use.

6- Conditions & Loops

if else, if else if

if (x >= y) {
	print(x);
} else {
	print(y);
}
if (x > y) {
	print(x);
} else if(x == y){
	print("equal");
} else {
	print(y);
}

6- Conditions & Loops

switch case default

int x = 5;
switch(x){
	case 0:	print("zero");
    	break;
    case 1: print("one");
    	break;
    .
    .
	case 5: print("five");
    	break;
	.
    .
    default: print("above nine or bellow 0");
}

Switch case statements are a substitute for long if statements that compare a variable to several constant values.

6- Conditions & Loops

what are loops ?

Loops are control structures used to repeat a given section of code a certain number of times or until a particular condition is met.

6- Conditions & Loops

while & do while loop

Used for conditional repetition.

while(condition){ 
	.
	.
	instructions;
	.
	.    
}
int x=5
while(x>0){ 
	x--;
    print(x);
}

Example: 

6- Conditions & Loops

while & do while loop

Used for conditional repetition.

do{ 
	.
	.
	instructions;
	.
	.    
}while(condition);
int x=5
do{ 
	print(x);
	x--;
}while(x>0);

Example: 

6- Conditions & Loops

for loop

Used for iteration.

for(int i = begening; condition ; iteration){ 
	.
	.
	instructions;
	.
	.    
}
for(int i = 0; i<5 ; i++){ 
	print(i);
}

Example: 

int[] numbers = new int[25];
for(int i = 0; i<25 ; i++){ 
	numbers[i] = i;
}

Coding with C# Part-1

Unity chapter 2 part-1

By Youcef Madadi

Unity chapter 2 part-1

  • 273