Beruflich Dokumente
Kultur Dokumente
Introduction to C#
Objectives
What you will learn...
How to do imperative programming using C#.
C# control structures and data structures
Lesson 2: Introduction to C#
Lesson 2: Introduction to C#
Lesson 2: Introduction to C#
using System;
class MyClass
{
static void Main() {
Console.WriteLine("Hello
World!");
}
}
Lesson 2: Introduction to C#
using System;
class MyClass
{
static void Main() {
Console.WriteLine("Hello
World!");
}
}
Lesson 2: Introduction to C#
using System;
class MyClass
{
static void Main() {
Console.WriteLine("Hello
World!");
}
}
Lesson 2: Introduction to C#
using System;
class MyClass
{
static void Main() {
Console.WriteLine("Hello
World!");
}
}
Lesson 2: Introduction to C#
using System;
class MyClass
{
static void Main() {
Console.WriteLine("Hello
World!");
}
}
Lesson 2: Introduction to C#
Lesson 2: Introduction to C#
10
Variable Declaration
Format
{Datatype} variable_name;
{Datatype} variable_name = value;
{Datatype} variable_name1, variable_name1;
Example
int number1 = 1;
int number1, number2, number3;
string hello = hello world;
Lesson 2: Introduction to C#
11
Char
Holds a Unicode character (i.e. a, 1, _)
Character data types are used to hold 1 ASCII value.
String
A string is a special data type which can hold a group of
characters as text. A string can also be treated as an array of
characters. Arrays will be discussed shortly.
12
Lesson 2: Introduction to C#
13
Lesson 2: Introduction to C#
14
Constants
Constants are special kinds of variable in that their value
cannot be changed and is known at compile time.
Values of constants must be defined explicitly and
cannot be changed during run-time.
To declare constants, we simply place the const keyword
before the variable type.
Ex. public const pi_value = -3.14;
public string hello = hello;
Lesson 2: Introduction to C#
15
16
17
18
What is an array?
An array is a collection of variables of 1 specific data
type.
It can be likened to a train, each cart being a separate
variable but become linked together with other similar
carts.
Array members can be accessed directly like normal
variable.
Arrays provide an important way for accessing different
variables in a loop. Loops will be discussed later in this
module.
Lesson 2: Introduction to C#
19
Declaring Arrays
Unlike declaring the data types discussed earlier,
declaring arrays take a slightly longer approach.
{datatype}[] var_name = new {datatype}[size];
Ex int[] num1 = new int[10];
Declares an array of 10 integers.
string[] msg1 = new string[1];
Declares an array of 1 string.
Lesson 2: Introduction to C#
20
var_name[index] = value;
num_array[0] = 10;
string_array[10] = hello;
Lesson 2: Introduction to C#
21
Accessing Arrays
Lesson 2: Introduction to C#
22
Lesson 2: Introduction to C#
23
Assignment Operator
The assignment operator is used to assign a value to a
variable. It is an operator that is applicable to all kinds of
data types.
It is arguably the most used operator.
Ex num1 = 100;
num2 = num1 + 1;
string msg = hello;
yes = true;
Lesson 2: Introduction to C#
24
25
Numerical Operators
Basic Numerical Operators include
addition (+)
subtraction (-)
multiplication (*)
division (/)
Lesson 2: Introduction to C#
26
Numerical Operators
Lesson 2: Introduction to C#
27
Numerical Operators
Examples 1
5 + 8 * (9 7) / 4
5+8*2/4
5 + 16 / 4
5+4
9
Example 2
(10 - (5 + 7) / 3 ) * 2
(10 12 / 3) * 2
(10 4) * 2
6*2
12
Lesson 2: Introduction to C#
28
Numerical Operators
Other Numerical Operators
Increment (++)
Adds 1 to the value of a variable
Ex. num1++, ++num1
Placing ++ before the variable adds the value before evaluating the
expression while placing it after would result in evaluation before adding
the value
Ex.
Lesson 2: Introduction to C#
29
Numerical Operators
Other Numerical Operators
Decrement (--)
Works exactly the same way as increment but subtracts 1
instead of adding 1.
Lesson 2: Introduction to C#
30
String Operator
string concatenation (+)
Appends concatenates strings.
Ex.
Lesson 2: Introduction to C#
31
Boolean Operators
Boolean Operators (cont)
and (&)
Returns true only if both operands are true
Ex
true & true = true
false & true = false
or (|)
Returns true if at least one operand is true
Ex.
true | false = true
false | false = false
not (!)
Negates an operand
Ex.
(!true) = false
(!false) = true
Lesson 2: Introduction to C#
32
Comparison Operators
Numerical Comparison Operators
Greater than (>)
Less than (<)
Greater than or equal (>=)
Less than or equal (<=)
Equal (==)
Not equal (!=)
33
Comparison Operators
String Comparison Operator
Equal (==)
Returns true if the two string operators hold exactly the same value.
Lesson 2: Introduction to C#
34
Type Conversion
Lesson 2: Introduction to C#
35
string y = "123";
int x = Convert.ToInt16(y);
Note: This would convert y into the number 123
string y = "123a";
long x = Convert.ToInt64(y);
Note: This would produce an exception because of it cannot convert
123a into a number.
Lesson 2: Introduction to C#
36
Lesson 2: Introduction to C#
37
If-then-else statements
More comprehensive branching statement capable of
representing just about every kind of branching need.
Case statements
A case statement is good option for checking ordinal values
(i.e. A-Z)
Lesson 2: Introduction to C#
38
Lesson 2: Introduction to C#
39
The Condition
The condition is a vital part in the If-Then-Else construct.
Should it evaluate to true it would tell the program to
execute the succeeding statements. Otherwise the
statements would be skipped.
The condition above was (num1 > 0).
Lesson 2: Introduction to C#
40
Lesson 2: Introduction to C#
41
Lesson 2: Introduction to C#
42
43
Format
switch(num1)
{
case 1
Console.WriteLine(one)
break;
case 2
Console.WriteLine(two)
break;
case 3
Console.WriteLine(three)
break;
}
switch(variable_name)
{
case constant1
Statements
break;
case constant2
Statements
break;
case constant3
Statements
break;
}
Lesson 2: Introduction to C#
44
Case Statements
In a case statement, you basically place the variable to be test
for in the switch statement.
What follows is the list of possible values preceded by the
case keywords and followed by the corresponding lines and a
break statement.
The statements following the case containing the matched
constant are executed.
Note that only constants can be placed after the case
keywords. Variables cannot be used.
Lesson 2: Introduction to C#
45
While Loop
int num = 10;
while(num>0)
{
Console.WriteLine(num--);
}
The loop will continue until
num becomes 0, at which
point it will not enter the
loop and jump to the end.
Program Output:
10
9
8
7
6
5
4
3
2
1
Lesson 2: Introduction to C#
46
Do-While Variant
int num = 0;
do {
Console.WriteLine(num--);
} while(num>0)
Program Output
0
The difference of the loop above to the last one is that the
statements are executed even before the condition is
tested.
You are guaranteed at least one run of the codes in the
loop.
Lesson 2: Introduction to C#
47
For Loop
The for loop construct is the stricter of the looping
constructs.
In loop definition we have the initialization part, the
condition part and the increment per run. The
statements follow the loop initialization.
The loop continues as long as the condition part
holds true.
Lesson 2: Introduction to C#
48
Lesson 2: Introduction to C#
49
int num1=0;
for(; num1<10;)
{
Console.WriteLine(num1);
num1 = num1 + 1;
}
Program Output
0
1
2
3
4
5
6
7
8
9
Lesson 2: Introduction to C#
50
Lesson 2: Introduction to C#
51
Console Output
We have shown plenty of examples that print information
on the command line. This is done through the use of the
Console.WriteLine() command.
This command accepts any data type and prints it into
the command line.
Ex Console.WriteLine(Love and Peace);
This would print Love and Peace in the console.
Lesson 2: Introduction to C#
52
Console Input
Console input is done through the use of the
Console.ReadLine() command.
It reads a string input from the user by waiting
for the Carriage Return to be pressed.
To use the input as other data types, the Convert
class is needed.
Lesson 2: Introduction to C#
53
Console Input
Example of storing a string:
string msg = Console.ReadLine();
Example of storing an integer:
string msg = Console.ReadLine();
int num1 = Convert.ToInt32(msg);
Lesson 2: Introduction to C#
54
Method Declaration
A method requires a method name, a return type
and a method definition.
Return types may be any data type, void or
objects. Void defines the method to return
nothing. Objects will be discussed further in the
next module. The value to be returned is marked
by the return keyword.
Lesson 2: Introduction to C#
55
Lesson 2: Introduction to C#
56
Static Methods
Methods are bound to objects but since we have yet
to discuss objects we will employ the use of static
methods.
Static methods can be called without instantiating
objects but we will change this approach in the next
module.
Static methods are declared as such but adding the
static keyword before stating the return type of the
method.
Lesson 2: Introduction to C#
57
58
Lesson 2: Introduction to C#
59
Lesson 2: Introduction to C#
60