diff --git a/2-variables-and-types-slides.html b/2-variables-and-types-slides.html index cbf9f82..993ef4c 100644 --- a/2-variables-and-types-slides.html +++ b/2-variables-and-types-slides.html @@ -13,10 +13,10 @@ /* buutti.css */ /* @theme buutti */div#\:\$p>svg>foreignObject>section .columns{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns12{display:grid;grid-template-columns:1fr 2fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns21{display:grid;grid-template-columns:2fr 1fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns32{display:grid;grid-template-columns:3fr 2fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns23{display:grid;grid-template-columns:2fr 3fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .columns111{display:grid;grid-template-columns:1fr 1fr 1fr;gap:calc(var(--marpit-root-font-size, 1rem) * 1)}div#\:\$p>svg>foreignObject>section .centered{display:flex;flex-direction:column;justify-content:center;text-align:center}div#\:\$p>svg>foreignObject>section .tableborderless td,div#\:\$p>svg>foreignObject>section th{border:none!important;border-collapse:collapse}div#\:\$p>svg>foreignObject>section.extra{background-color:#5d275d;background-image:linear-gradient(to bottom,#401a40,#1d0c1d);color:white}div#\:\$p>svg>foreignObject>section.extra a{color:rgb(145,255,209)}div#\:\$p>svg>foreignObject>section.exercise{background-color:#29366f;background-image:linear-gradient(to bottom,#20636a,#173742);color:white}div#\:\$p>svg>foreignObject>section.exercise a{color:rgb(211,173,255)} -/* @theme xgqunf1arebgoo0h9avfqhbo3l9yyeh7yl4671m79nl */div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]{columns:initial!important;display:block!important;padding:0!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:before,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:before{display:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]{all:initial;display:flex;flex-direction:row;height:100%;overflow:hidden;width:100%}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container][data-marpit-advanced-background-direction=vertical]{flex-direction:column}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split]>div[data-marpit-advanced-background-container]{width:var(--marpit-advanced-background-split,50%)}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split=right]>div[data-marpit-advanced-background-container]{margin-left:calc(100% - var(--marpit-advanced-background-split, 50%))}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure{all:initial;background-position:center;background-repeat:no-repeat;background-size:cover;flex:auto;margin:0}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure>figcaption{position:absolute;border:0;clip:rect(0,0,0,0);height:1px;margin:-1px;overflow:hidden;padding:0;white-space:nowrap;width:1px}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content],div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo]{background:transparent!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo],div#\:\$p>svg[data-marpit-svg]>foreignObject[data-marpit-advanced-background=pseudo]{pointer-events:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background-split]{width:100%;height:100%}
+/* @theme gzapc67wmn4x4juhe1pf75x6urrixfh4arwl4efckrr */div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]{columns:initial!important;display:block!important;padding:0!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]:before,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:after,div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content]:before{display:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]{all:initial;display:flex;flex-direction:row;height:100%;overflow:hidden;width:100%}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container][data-marpit-advanced-background-direction=vertical]{flex-direction:column}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split]>div[data-marpit-advanced-background-container]{width:var(--marpit-advanced-background-split,50%)}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background][data-marpit-advanced-background-split=right]>div[data-marpit-advanced-background-container]{margin-left:calc(100% - var(--marpit-advanced-background-split, 50%))}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure{all:initial;background-position:center;background-repeat:no-repeat;background-size:cover;flex:auto;margin:0}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=background]>div[data-marpit-advanced-background-container]>figure>figcaption{position:absolute;border:0;clip:rect(0,0,0,0);height:1px;margin:-1px;overflow:hidden;padding:0;white-space:nowrap;width:1px}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=content],div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo]{background:transparent!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background=pseudo],div#\:\$p>svg[data-marpit-svg]>foreignObject[data-marpit-advanced-background=pseudo]{pointer-events:none!important}div#\:\$p>svg>foreignObject>section[data-marpit-advanced-background-split]{width:100%;height:100%}

Variables and Types

-
+

Overview

  • Variables
  • @@ -29,7 +29,7 @@
  • String Interpolation
-
+

Variables

  • A variable can be thought of as a name for a certain address in computer's memory @@ -41,7 +41,7 @@
  • On more practical terms: We can assign values to named variables.
-
+

Declaring variables

  • Every variable declaration in C# requires the type and the name of the variable, for example:
    int x;
    @@ -55,7 +55,7 @@
     
-
+

Printing to console with Console.WriteLine

@@ -89,7 +89,7 @@ Console.WriteLine(example);
-
+

Extra: Modifiers

    @@ -103,10 +103,10 @@ one = 2; // raises
  • Other modifiers include access modifiers introduced in Lecture 7.
-
+

Data types

-
+

What is a data type?

  • Data type tells to a computer what type of data is stored in a variable.
  • @@ -123,7 +123,7 @@ one = 2; // raises
-
+

Primitive data types

@@ -181,7 +181,7 @@ one = 2; // raises

More types listed in the C# reference!

-
+

Data type examples

double airPressure = 1.2;         // Use for most decimal numbers
 decimal accountBalance = 1.2m;    // Use for accuracy (e.g. financial applications)
@@ -193,7 +193,7 @@ one = 2;  // raises
 
  • char is only used for single characters, multi-character strings will be introduced in a bit.
  • -
    +

    Extra: Casting data types

    Data types can be cast to another either...

    @@ -221,17 +221,17 @@ Console.WriteLine((decimal)a + b);
  • C# Guide: Casting and type conversions
  • -
    +

    Assignments (variables)

    Assignments about this topic can be found here

    -
    +

    Assignments (data types)

    Assignments about this topic can be found here

    -
    +

    Arithmetic operations?

    • Arithmetic operations are common mathematical operations: @@ -246,7 +246,7 @@ Console.WriteLine((decimal)a + b);
    • The operations are represented by arithmetic operators
    -
    +

    Arithmetic Operators

    @@ -303,7 +303,7 @@ Console.WriteLine((decimal)a + b);
    -
    +

    Exercise 1: Trying Out Variables

      @@ -312,7 +312,7 @@ Console.WriteLine((decimal)a + b);
    1. Print the sum, difference, fraction and product of those values to the console.
    -
    +

    The assignment operator

    We have used the assignment operator = for assigning values for variables:

    int x;
    @@ -326,7 +326,7 @@ x = 25;
     
     
     
    -
    +

    Assignment operators

    @@ -380,7 +380,7 @@ x = 25;
    -
    +

    Assignment operators: An example

    int uppercaseLetters = 2;
     uppercaseLetters += 4;          // is now 6
    @@ -392,7 +392,7 @@ Console.WriteLine(uppercaseLetters);
     Console.WriteLine(specialCharacters);
     
    -
    +

    Increment and decrement operations

    • You can increment or decrement a variable value by 1 with dedicated short-hands @@ -429,7 +429,7 @@ a--; // a is now 0
    • ++ and -- are called the increment and decrement operators
    -
    +

    Extra: Increment/decrement operation precedence

      @@ -460,61 +460,92 @@ so its value is 3

    -
    +

    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 name = "Johannes Kantola";

    -

    You can concatenate multiple strings with the '+' operator:

    -

    string firstName = "Johannes";

    -

    string lastName = "Kantola";

    -

    string fullName = firstName + " " + lastName;

    -

    Console.WriteLine(fullName); // Outputs "Johannes Kantola"

    +
      +
    • string is a special type that contains an array of characters.
      string name = "Sini Aalto";
      +
      +
    • +
    • You can concatenate (i.e., combine) multiple strings with the + operator:
      string firstName = "Sini";
      +string lastName = "Aalto";
      +
      +string fullName = firstName + " " + lastName;
      +
      +Console.WriteLine(fullName);    // Outputs "Sini Aalto"
      +
      +
    • +
    -
    -

    Character Constants

    -

    Character constants are preceded by a backslash '\' and can be used for formatting strings

    -

    '\n' represents a newline in the following example:

    -

    string firstName = "Johannes";

    -

    string lastName = "Kantola";

    -

    string fullName = firstName + "\n" + lastName;

    -

    Console.WriteLine(fullName);

    -

    /* This outputs

    -

    Johannes

    -

    Kantola

    -

    */

    -

    All character constants: https://www.tutorialspoint.com/csharp/csharp_constants.htm

    +
    +

    Character Constants

    +
      +
    • Character constants are preceded by a backslash \ and can be used for formatting strings
    • +
    • \n represents a newline
      string firstName = "Johannes";
      +string lastName = "Kantola";
      +string fullName = firstName + "\n" + lastName;
      +Console.WriteLine(fullName);
      +
      +/* This outputs
      +Johannes
      +
      +Kantola
      +*/
      +
      +
    • +
    • All character constants are listed here
    • +
    -
    -

    String Interpolation

    -

    Concatenating multiple variables into one string with the '+' operator quickly becomes tedious

    -

    It's much easier to use __string interpolation __ by prefixing your target string with '$' and inserting the variables inside curly brackets '{ }':

    -

    string animal = "Dog";

    -

    string sound = "Woof";

    -

    Console.WriteLine($"{animal} says {sound}!");

    -

    // Outputs "Dog says Woof!"

    +
    +

    String Interpolation

    +
      +
    • Concatenating multiple variables into one string with the + operator quickly becomes tedious
    • +
    • It's much easier to use string interpolation by prefixing your target string with $ and inserting the variables inside curly brackets { }:
      string animal = "Dog";
      +string sound = "Woof";
      +
      +Console.WriteLine($"{animal} says {sound}!");
      +// Outputs "Dog says Woof!"
      +
      +
    • +
    -
    -

    String Formatting

    -

    You can add format strings to change the way variables are interpolated into a string

    -

    Add the format string after your variable, separated by a colon (:)

    -

    You can find an overview of format strings and a handy list of both standard and custom strings here

    -

    double pi = 3.141592653;

    -

    Console.WriteLine($"Pi to three digits: {pi:G3}");

    -

    // Outputs "Pi to three digits: 3.14"

    +
    +

    String Formatting

    +
      +
    • You can add format strings to change the way variables are interpolated into a string
    • +
    • Add the format string after your variable, separated by a colon (:)
    • +
    • You can find an overview of format strings and a handy list of both standard and custom strings here
      double pi = 3.141592653;
      +Console.WriteLine($"Pi to three digits: {pi:G3}");
      +// Outputs "Pi to three digits: 3.14"
      +
      +
    • +
    -
    -

    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:

    -

    string userInput = Console.ReadLine();Console.WriteLine(userInput);

    -

    +
    +

    User input with 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:
      string userInput = Console.ReadLine();
      +Console.WriteLine(userInput);
      +
      +
    • +
    +
    +
    +

    +
    +
    -
    +

    Exercise 2: Weekday survey

    -

    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.

    +
      +
    1. Create a console application that asks the user which weekday it is and assigns the answer to a string variable.
    2. +
    3. Print Have a nice <weekday> to the console where <weekday> is replaced with the string the user wrote.
    4. +
    -
    +

    Assignments (arithmetic operations)

    Assignments about this topic can be found here

    diff --git a/2-variables-and-types.md b/2-variables-and-types.md index 913729c..613b53d 100644 --- a/2-variables-and-types.md +++ b/2-variables-and-types.md @@ -330,87 +330,86 @@ so its value is 3 ## 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 name = "Johannes Kantola"; - -You can concatenate multiple strings with the '+' operator: - -string firstName = "Johannes"; - -string lastName = "Kantola"; - -string fullName = firstName + " " + lastName; - -Console.WriteLine(fullName); // Outputs "Johannes Kantola" - -## Character Constants - -Character constants are preceded by a backslash '\\' and can be used for formatting strings - -'\\n' represents a newline in the following example: - -string firstName = "Johannes"; - -string lastName = "Kantola"; - -string fullName = firstName + "\\n" + lastName; - -Console.WriteLine(fullName); - -/* This outputs - -Johannes - -Kantola - -*/ - -All character constants: [https://www.tutorialspoint.com/csharp/csharp\_constants.htm](https://www.tutorialspoint.com/csharp/csharp_constants.htm) - -## String Interpolation - -Concatenating multiple variables into one string with the '+' operator quickly becomes tedious - -It's much easier to use __string interpolation __ by prefixing your target string with '$' and inserting the variables inside curly brackets '{ }': - -string animal = "Dog"; +* `string` is a special type that contains an array of characters. + ```csharp + string name = "Sini Aalto"; + ``` +* You can ***concatenate*** (i.e., combine) multiple strings with the `+` operator: + ```csharp + string firstName = "Sini"; + string lastName = "Aalto"; + + string fullName = firstName + " " + lastName; + + Console.WriteLine(fullName); // Outputs "Sini Aalto" + ``` -string sound = "Woof"; +### Character Constants -Console.WriteLine($"{animal} says {sound}!"); +* Character constants are preceded by a backslash `\` and can be used for formatting strings +* `\n` represents a ***newline*** + ```csharp + string firstName = "Johannes"; + string lastName = "Kantola"; + string fullName = firstName + "\n" + lastName; + Console.WriteLine(fullName); -// Outputs "Dog says Woof!" + /* This outputs + Johannes -## String Formatting + Kantola + */ + ``` +* All character constants are listed [here](https://www.tutorialspoint.com/csharp/csharp_constants.htm) -You can add __format strings__ to change the way variables are interpolated into a string +### String Interpolation -Add the format string after your variable, separated by a colon (:) +* Concatenating multiple variables into one string with the `+` operator quickly becomes tedious +* It's much easier to use *__string interpolation__* by prefixing your target string with `$` and inserting the variables inside curly brackets `{ }`: + ```csharp + string animal = "Dog"; + string sound = "Woof"; -You can find an overview of format strings and a handy list of both standard and custom strings [here](https://learn.microsoft.com/en-us/dotnet/standard/base-types/formatting-types) + Console.WriteLine($"{animal} says {sound}!"); + // Outputs "Dog says Woof!" + ``` -double pi = 3.141592653; +### String Formatting -Console.WriteLine($"Pi to three digits: {pi:G3}"); +* You can add *__format strings__* to change the way variables are interpolated into a string +* Add the format string after your variable, separated by a colon (:) +* You can find an overview of format strings and a handy list of both standard and custom strings [here](https://learn.microsoft.com/en-us/dotnet/standard/base-types/formatting-types) + ```csharp + double pi = 3.141592653; + Console.WriteLine($"Pi to three digits: {pi:G3}"); + // Outputs "Pi to three digits: 3.14" + ``` -// Outputs "Pi to three digits: 3.14" +## User input with `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: + ```csharp + string userInput = Console.ReadLine(); + Console.WriteLine(userInput); + ``` -string userInput = Console.ReadLine();Console.WriteLine(userInput); +
    +
    -![](imgs/2%20Variables%20and%20Types_1.png) +![w:200](imgs/2%20Variables%20and%20Types_1.png) +
    +
    ## Exercise 2: Weekday survey -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. +1) Create a console application that asks the user which weekday it is and assigns the answer to a string variable. +2) Print `Have a nice ` to the console where `` is replaced with the string the user wrote. ## Assignments (arithmetic operations)