[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/1.%20C%23%20Basics%20Assignments)
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/1.%20C%23%20Basics%20Assignments)
More types listed in the [C# reference](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/built-in-types)!
# Data Types in C# - Example
### Data type examples
double airPressure = 1.2; // Use generally for everything
```csharp
double airPressure = 1.2; // Use for most decimal numbers
decimal accountBalance = 1.2m; // Use for accuracy (e.g. financial applications)
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
float bulletSpeed = 1.2f; // Use only when you know the precision will be enough (in other words, don't use)
bool loggedIn = false;
bool loggedIn = false;
char previousInput = 'b';
char previousInput = 'b';
```
# Assignments (variables)
* `char` is only used for single characters, multi-character ***strings*** will be introduced in a bit.
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/2.1.%20Variables%20&%20Types)
## Extra: Casting data types
<!-- _class: "extra invert" -->
# Assignments (data types)
Data types can be ***cast*** to another either...
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/2.2.%20Data%20Types)
* Casting is useful when, for example, when we want to sum a `double` and a `decimal` together:
```csharp
double a = 1.0;
decimal b = 2.1m;
Console.WriteLine(a + (double)b);
Console.WriteLine((decimal)a + b);
```
* [C# Guide: Casting and type conversions](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions)
division
## Assignments (variables)
<!--_class: "exercise invert" -->
modulus (remainder, in Finnish jakojäännös)
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/2.1.%20Variables%20&%20Types)
exponentiation
## Assignments (data types)
<!--_class: "exercise invert" -->
Earlier we used an _arithmetic operator _ to subtract b from a:
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/2.2.%20Data%20Types)
Console.WriteLine(a - b);
## Arithmetic operations?
# Arithmetic Operators
* ***Arithmetic operations*** are common mathematical operations:
* Addition
* Subtraction
* Multiplication
* Division
* Modulus (remainder, in Finnish *jakojäännös*)
* The operations are represented by **_arithmetic operators_**
| Operator | Name | Example | Description |
## Arithmetic Operators
| :-: | :-: | :-: | :-: |
| + | 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 of a variable by 1 |
| -- | Decrement | a–- | Decreases the value of a variable by 1 |
| `++` | Increment | `a++` | Increases the value by 1 |
| `--` | Decrement | `a–-` | Decreases the value by 1 |
Tässä on kaikki aritmeettiset operaattorit
## Exercise 1: Trying Out Variables
<!--_class: "exercise invert" -->
# Exercise 1: Trying Out Variables
1) Create a new console application and declare two variables of type `double`.
2) Assign different values for those variables.
3) Print the sum, difference, fraction and product of those values to the console.
Create a new console application as we did in the last exercise and declare two variables of type __double__ _._
## The assignment operator
Assign different values for those variables.
We have used the assignment operator `=` for assigning values for variables:
```csharp
int x;
x = 25;
```
* ***Note*** the difference between `=` and `==` introduced in [Lecture 3](3-conditionals#comparison-operators)!
* `=` is used for assigning values for variables, `==` is used for ***comparing*** values
Print the __sum__ , __difference__ , __fraction __ and __product __ of those values to the console.
### Assignment operators
# Increment & Decrement
<divclass='columns'markdown='1'>
<divmarkdown='1'>
Increment and decrement operations are operations that can be used to increment or decrement a variable value by 1.
| Operator | 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` |
Most programming languages implement these operations
</div>
<divmarkdown='1'>
Addition example: int a = 3; a = a + 1; _// a is now 4_ Can be written with increment: int a = 3; a++; _// a is now 4_
* As shown here, there are some assignment operators that work as ***shorthands*** for longer assignments
* Particularly useful when the variable name is longer, so you don't have to write it twice when its value is changed
Decrement is like increment but with just subtraction
</div>
</div>
Substraction example: int a = 3; a = a - 1; _// a is now 2_ Can be written with decrement: int a = 3; a--; _// a is now 2_
Increment and decrement operators can be written in two ways. So sometimes you may also see this: int a = 3; --a; _// a is now 2__ _ ++a; _// a is now 3_
### Assignment operators: An example
This will do the same thing as a++ and a--. There is a small difference between these syntaxes though.
```csharp
int uppercaseLetters = 2;
uppercaseLetters += 4; // is now 6
int a = 3;const _ _ b = _ _ a++; _// b will be 3_ Console.WriteLine(a); _// this will print out 4_ Here the assignment to b happens before incrementing a, thus b will be assigned value 3.
int specialCharacters = 2;
specialCharacters *= 2; // is now 4
int a = 3;const _ _ b = _ _ ++a; _// b will be 4_ Console.WriteLine(a); _// this will print out 4_ Here the assignment to b happens after incrementing a, thus b will be assigned value 4.
Console.WriteLine(uppercaseLetters);
Console.WriteLine(specialCharacters);
```
---
### Increment and decrement operations
Täällä voi helposti demota: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment
* You can increment or decrement a variable value by 1 with dedicated short-hands
* Most programming languages implement these!
# Assignment Operators
<divclass='columns'markdown='1'>
<divmarkdown='1'>
We have used the assignment operator ' __='__ for assigning values for variables:
* Addition example:
```csharp
int a = 3;
int x;
a = a + 1; // a is now 4
a += 1; // a is now 5
a++; // a is now 6
```
x = 25;
</div>
<divmarkdown='1'>
__Notice __ the difference between '=' and the conditional '=='!
* Subtraction example:
```csharp
int a = 3;
'=' is used for assigning values for variables, '== is used for comparing values
a = a - 1; // a is now 2
a -= 1; // a is now 1
a--; // a is now 0
```
| Operator | 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 |
---
</div>
</div>
Tässä on joitakin asetusoperaattoreita, lisäksi loogiset (ei käydä tässä)
* `++` and `--` are called the ***increment and decrement operators***
* Note that incrementing can be written as ***prefix*** (`++i`) or a ***postfix*** (`i++`)
* In this example, `a++` and `++a` do exactly the same:
```csharp
int a = 3;
a++; // a is now 4
++a; // a is now 5
```
* Their exact difference is [complicated](https://stackoverflow.com/questions/3346450/what-is-the-difference-between-i-and-i-in-c), and in some cases, using either prefix or postfix form can produce different results:
int specialCharacters = 2;
<divclass='columns'markdown='1'>
<divmarkdown='1'>
specialCharacters *= 2; // is now 4
```csharp
int a = 3;
int b = ++a;
Console.WriteLine(b); // 4
```
Assignment of `b` happens after `++`,
so its value is 4
Console.WriteLine(uppercaseLetters);
</div>
<divmarkdown='1'>
Console.WriteLine(specialCharacters);
```csharp
int a = 3;
int b = a++;
Console.WriteLine(b); // 3
```
Assignment of `b` happens before `++`,
so its value is 3
</div>
</div>
This outputs 6 and 4
# Strings
## Strings
String is a special type, which contains an array of characters. You can declare and assign strings like any other type of variables:
String is a special type, which contains an array of characters. You can declare and assign strings like any other type of variables:
You can add __format strings__ to change the way variables are interpolated into a string
You can add __format strings__ to change the way variables are interpolated into a string
@ -304,7 +396,7 @@ Console.WriteLine($"Pi to three digits: {pi:G3}");
// Outputs "Pi to three digits: 3.14"
// Outputs "Pi to three digits: 3.14"
# Console.ReadLine()
## Console.ReadLine()
For the next exercise, you'll need the Console.ReadLine() method. The method pauses the program, waits for an input stream from the console that pops up, and returns the value of the input:
For the next exercise, you'll need the Console.ReadLine() method. The method pauses the program, waits for an input stream from the console that pops up, and returns the value of the input:
Create a console application which asks the user which weekday it is and assigns the answer to a string variable.
Create a console application which asks the user which weekday it is and assigns the answer to a string variable.
Print "Have a nice weekday!" to the console where weekday is replaced with the string the user wrote.
Print "Have a nice weekday!" to the console where weekday is replaced with the string the user wrote.
# Assignments (arithmetic operations)
## Assignments (arithmetic operations)
<!--_class: "exercise invert" -->
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/2.3.%20Arithmetic%20Operations)
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/2.3.%20Arithmetic%20Operations)