if
When script are running, the command that we want to be execute with the condition can be written with the if
statement.
bool condition1 = true;
bool condition2 = false;
if(condition1 && condition2)
{
// commands what you want to execute if `condition` is true;
Console.WriteLine("This is true and true");
}
else if (condition1 || condition2)
{
Console.WriteLine("This is true or false");
}
else
{
Console.WriteLine("This is false and false");
}
switch
Similar with the if
, the switch consist of the case
and default
int cond = 2;
switch (cond)
{
case 0:
Console.WriteLine("0");
break;
case 1:
Console.WriteLine("1");
break;
case 2:
Console.WriteLine("2");
break;
default:
Console.WriteLine("the number is not in the range of 0 ~ 2");
break;
}
for
& while
When we want to repeat the command, the for
and while
can be used for the command loop
For the for
, it needs 3 argument with the separating ;
like below example.
for(int i=0; i<10; i++)
the first argument is the declaration of variable which changes with the loop.
the second argument is the condition for the loop. the loop will be execute when the condition is true
the third argument is the changes. when the commands run inside the loop, the variable would changes according this argument
This is an example for using for
for(int a=0; a<9; a++)
{
Console.WriteLine(a);
}
Similar with the for
, the while
can be used to repeat the command. the differences are the number of arguments. Since the for
needs 3 arguments, it can be facilitate to the independently. The while
, However, needs only condition. With this feature, the while
is adopted the number of loop assosiated outside of loop command. See the example
bool extCond = false;
while(extCond)
{
Console.WriteLine("extCond is true");
}
Main difference of for
and while
is the area of variable. When we used loop for fixed number or independently, the for
loop seems to be clear. In the case of the loop which related to the variable of outside, the while
loop is more appropirate to write clearly.
To create the group of variable which contains same types of data, the Array
can be one of the choice. Usage of the array can be seen at the below example
// declaration of array
int[] arr_int;
// initialization
arr_int = new int[3];
// decl + init
int[] arr_int2 = new int[4];
... /// fill the elements
// call the element of array
Console.WriteLine(arr_int[0]);
Console.WriteLine(arr_int2[0] + arr_int2[1]);
Typically, the array were used with the for loop to access the element with loop index.
Similar with the vector
on C++, it is kind of array whose size is not fixed. To use this container, we should write the type of variable inside of bra-ket<>
. After attaching the indicator of list and declaration sentence, we created List
variable
// create new list variable
List<int> listName = new List<int>();
// insert the valus to the list
listName.Add(0);
listName.Add(1);
listName.Add(2);
listName.Add(3);
listName.Remove(3);
// access each element
foreach(int i in listName)
{
Console.WriteLine(i);
}
vector | List | |
---|---|---|
Environment | C++ | C# |
Similar with the map
on C++, the key-value pair were stored in this variable
// create new dictionary
Dictionary<string,int> dic_var = new Dictionary<string,int>();
// insert the key-value pair
dic_var.Add("Test",0);
// access the element
foreach(KeyValuePair<string,int> pair in dic_var)
{
Console.WriteLine(pair.Key+" " + pair.Value);
}
map | Dictionary | |
---|---|---|
Environment | C++ | C# |
Similar with the set
on C++, the group of variable without overlap can be defined on C#.
// create new hashset
HashSet<int> set_int = new HashSet<int>();
// insert the value on set
set_int.Add(1);
set_int.Add(1);
set_int.Add(2);
// call the element of set
foreach(int ele in set_int)
{
Console.WriteLine(ele);
}
set | HashSet | |
---|---|---|
Environment | C++ | C# |
The group of codes, facilitated for the re-use of code snippet
[access] [return type] [name] ([argument])
{
// commands that you want to execute
}
For example, we can write like below
// define the method(so called as function)
public int AddNumber(int a, int b)
{
int result = a+b;
return result;
}
// overloading
public int AddNumber(int a, int b, int c)
{
int result = a+b+c;
return result;
}
void Main(string[] args)
{
int a=10;
int b= 4;
int c = AddNumber(a,b); // call the method
Console.WriteLine(c);
}
Object-Oriented Programming(OOP) has advantage written on the bottom
method
and data
of the class, modularization was doneif
' and 'switch
'if
:
1. processing the condition sequantially
2. writing if
, if else
and else
shows different length -> worse readability
3. the total processing time is proprtional to the number of conditions
4. advantage comes from the memory usage.
switch
:
1. jump the processing line directly
2. each case
looks like similar length -> good readability
3. fast processing time because unnecessary commands were skipped
4. whole case
were allocated to the memory. the more case written, the less available memory
A.
int [,] arr_2d = new int[4,3]; // create 4x3 array
... // fill the elements
Console.WriteLine(arr_2d[0,1]); // call the element at 0,1 index
int [,,] arr_3d = new int[4,3,5]; // create 4x3x5 array
... // fill the elements
Console.WriteLine(arr_3d[0,1,2]); // call the element at 0,1,2 index
Structure
: value type, used for simple type of data
Class
: With the inheritance, the class
is used to construct complex data structure