double airPressure = 1.2; // Use for most decimal numbers
decimal accountBalance = 1.2m; // Use for accuracy (e.g. financial applications)
float bulletSpeed = 1.2f; // Use only when you know its precision will be enough
bool loggedIn = false;
char previousInput = 'b';
char
is only used for single characters, multi-character strings will be introduced in a bit.Data types can be cast to another either...
...implicitly:
double valueAddedTax = 25.5;
decimal valueAddedTaxDecimal = valueAddedTax;
...explicitly:
double valueAddedTax = 25.5;
decimal valueAddedTaxDecimal = (decimal)valueAddedTax;
double
and a decimal
together:double a = 1.0;
decimal b = 2.1m;
Console.WriteLine(a + (double)b);
Console.WriteLine((decimal)a + b);
Operator | Name | Example | Description |
---|---|---|---|
+ |
Addition | a + b |
Adds together two values |
- |
Subtraction | a - b |
Subtracts one value from another |
* |
Multiplication | a * b |
Multiplies two values |
/ |
Division | a / b |
Divides one value by another |
% |
Modulus | a % b |
Returns the division remainder |
++ |
Increment | a++ |
Increases the value by 1 |
-- |
Decrement | a–- |
Decreases the value by 1 |
double
.We have used the assignment operator =
for assigning values for variables:
int x;
x = 25;
=
and ==
introduced in Lecture 3!
=
is used for assigning values for variables, ==
is used for comparing valuesOperator | Example | Same As |
---|---|---|
= |
x = 5 |
x = 5 |
+= |
x += 5 |
x = x + 5 |
-= |
x -= 5 |
x = x - 5 |
*= |
x *= 5 |
x = x * 5 |
/= |
x /= 5 |
x = x / 5 |
%= |
x %= 5 |
x = x % 5 |
int uppercaseLetters = 2;
uppercaseLetters += 4; // is now 6
int specialCharacters = 2;
specialCharacters *= 2; // is now 4
Console.WriteLine(uppercaseLetters);
Console.WriteLine(specialCharacters);
int a = 3;
a = a + 1; // a is now 4
a += 1; // a is now 5
a++; // a is now 6
int a = 3;
a = a - 1; // a is now 2
a -= 1; // a is now 1
a--; // a is now 0
++
and --
are called the increment and decrement operators++i
) or a postfix (i++
)a++
and ++a
do exactly the same:int a = 3;
a++; // a is now 4
++a; // a is now 5
int a = 3;
int b = ++a;
Console.WriteLine(b); // 4
Assignment of b
happens after ++
,
so its value is 4
int a = 3;
int b = a++;
Console.WriteLine(b); // 3
Assignment of b
happens before ++
,
so its value is 3
string
is a special type that contains an array of characters.string name = "Sini Aalto";
+
operator:string firstName = "Sini";
string lastName = "Aalto";
string fullName = firstName + " " + lastName;
Console.WriteLine(fullName); // Outputs "Sini Aalto"
\
and can be used for formatting strings\n
represents a newlinestring firstName = "Johannes";
string lastName = "Kantola";
string fullName = firstName + "\n" + lastName;
Console.WriteLine(fullName);
/* This outputs
Johannes
Kantola
*/
+
operator quickly becomes tedious$
and inserting the variables inside curly brackets { }
:string animal = "Dog";
string sound = "Woof";
Console.WriteLine($"{animal} says {sound}!");
// Outputs "Dog says Woof!"
double pi = 3.141592653;
Console.WriteLine($"Pi to three digits: {pi:G3}");
// Outputs "Pi to three digits: 3.14"
Console.ReadLine()
Console.ReadLine()
methodstring userInput = Console.ReadLine();
Console.WriteLine(userInput);
Have a nice <weekday>
to the console where <weekday>
is replaced with the string the user wrote.