From 05d1e4a420ab518666a8a19a4ab4a84cb152bb23 Mon Sep 17 00:00:00 2001 From: borb Date: Tue, 1 Jul 2025 15:17:43 +0300 Subject: [PATCH] finish lecture 4 - edit lecture 5 --- 4-loops-slides.html | 232 +++++++++++++++++++++ 4-loops.md | 367 +++++++++++++++++---------------- 5-arrays-and-lists-slides.html | 32 +-- 5-arrays-and-lists.md | 3 +- imgs/4 Loops_3.png | Bin 20699 -> 0 bytes 5 files changed, 433 insertions(+), 201 deletions(-) create mode 100644 4-loops-slides.html delete mode 100644 imgs/4 Loops_3.png diff --git a/4-loops-slides.html b/4-loops-slides.html new file mode 100644 index 0000000..0c8d435 --- /dev/null +++ b/4-loops-slides.html @@ -0,0 +1,232 @@ +4. Loops
+

Loops

+
+
+

Overview

+
    +
  • While loop
  • +
  • For loop
  • +
  • Nested loops
  • +
  • Breaking out of loops
  • +
+
+
+

Why loops?

+
    +
  • Loops are useful when you need to repeat a block of code multiple times and to avoid code like this:
    Console.WriteLine(count);
    +++count;
    +Console.WriteLine(count);
    +++count;
    +Console.WriteLine(count);
    +// Yuck! Nobody wants to see this.
    +
    +
  • +
  • Loops can also be used for iterating through arrays and lists (more on those later)
  • +
+
+
+

while loop

+
    +
  • A while loop keeps executing its code block as long as the condition in its statement is true:
    int count = 0;
    +while(count < 4)
    +{
    +  Console.WriteLine(count);
    +  ++count;
    +}
    +// Outputs '0', '1', '2' and '3'
    +
    +
  • +
+
+
+

do...while loop

+
    +
  • do...while works the same way as while, but there's a key difference:
  • +
  • The code is executed at least once and the condition is checked at the end:
    int count = 0;
    +do
    +{
    +  Console.WriteLine(count);
    +  ++count;
    +} while (count < 4);
    +// Outputs '0', '1', '2' and '3'
    +
    +
  • +
+
+
+

for loop

+
    +
  • for loops are used when the number of iterations is predefined
  • +
  • for loop is initialized in three steps:
    for (<initial value>; <condition>; <increment>)
    +{
    +  /* Code to be executed */
    +}
    +
    +
  • +
  • The initial value is set before the first iteration
  • +
  • The condition is checked before each iteration
  • +
  • The increment is executed after each iteration
  • +
+
+
+

For loop: An example

+

See the following two examples:

+
+
+
for (int i = 0; i < 4; ++i)
+{
+  Console.WriteLine
+    ("Current: " + i);
+}
+
+/* Outputs
+Current: 0
+Current: 1
+Current: 2
+Current: 3
+*/
+
+
+
+
for (int i = 8; i > 0; i -= 2)
+{
+  Console.WriteLine
+    ("Current: " + i);
+}
+
+/* Outputs
+Current: 8
+Current: 6
+Current: 4
+Current: 2
+*/
+
+
+
+
+
+
+
+

We start from zero int i = 0

+

print the current value

+

add one ++i

+

until the value is no longer under four
+i < 4.

+
for (int i = 0; i < 4; ++i)
+{
+  Console.WriteLine
+    ("Current: " + i);
+}
+
+
+
+

We start from eight int i = 8

+

print the current value

+

remove two i -= 2

+

until the value is no longer over zero
+i >

+
for (int i = 8; i > 0; i -= 2)
+{
+  Console.WriteLine
+    ("Current: " + i);
+}
+
+
+
+
+
+

Nested Loops

+

Nothing prevents you from using a loop inside another loop:

+
+
+
int columns = 3;
+int rows = 4;
+for (int i = 0; i < columns; ++i)
+{
+  for (int j = 0; j < rows; ++j)
+  {
+    // Console.Write doesn't add a linebreak after printout
+    Console.Write(
+      "(" + j.ToString() + "," + i.ToString() + ")"
+    );
+  }
+  Console.WriteLine("");
+}
+
+
+
+

+
+
+
+
+

Breaking out of loops with break

+
    +
  • To stop the execution of a loop before its end condition is met, use the break keyword:
    int i = 0;
    +while(true)
    +{
    +  ++i;
    +  if(i > 3)
    +  break;
    +}
    +// i is now 4
    +
    +
  • +
+
+
+

Skipping ahead with continue

+
    +
  • To skip the current iteration, use the continue keyword
    int i = 0;
    +while(i < 10)
    +{
    +  ++i;
    +  if (i % 2 == 0)
    +  continue;
    +  Console.WriteLine(i);
    +}
    +// Prints every odd number from 1 to 10
    +
    +
  • +
+
+
+

Exercise 1: The main loop

+ +

At the heart of every program that doesn't quit right away, there is something called the main loop

+
    +
  1. Create a console application that keeps asking the user for input until the user inputs quit.
  2. +
  3. Expand the program so that if the user inputs help, a help screen appears that explains what both quit and help commands do (See next image).
  4. +
+
+
+

+
+
+

Exercise 2: Prime numbers

+ +

A Prime number is a natural number greater than 1 that is not a product of two natural numbers (so it cannot be divided into a whole number).

+

Create a console application which prints all the prime numbers between 0 and 40.

+
+
+

Assignments

+

Assignments about this topic can be found here

+
+
\ No newline at end of file diff --git a/4-loops.md b/4-loops.md index e1b7198..bb3ea7c 100644 --- a/4-loops.md +++ b/4-loops.md @@ -1,246 +1,247 @@ -# Loops - -![](imgs/4%20Loops_0.png) - --- - -# Overview - -While Loop - -For Loop - -Nested Loops - -Breaking Out of Loops +marp: true +paginate: true +math: mathjax +theme: buutti +title: 4. Loops +--- # Loops -Loops are useful when you need to repeat a block of code multiple times and to avoid code like this: - -Console.WriteLine(count); - -++count; - -Console.WriteLine(count); - -++count; - -Console.WriteLine(count); - -// Yuck! Nobody wants to see this. - -Loops can also be used for iterating through arrays and lists (more on those later) - -# while Loop - -_while_ loop keeps executing its code block as long as the condition in its statement is true: - -int count = 0; - -while(count < 4) - -{ - -Console.WriteLine(count); - -++count; - -} - -// Outputs '0', '1', '2' and '3' - -# do..while Loop - -_do..while_ works the same way as while, but it executes the code at least once and checks the condition at the end: - -int count = 0; - -do - -{ - -Console.WriteLine(count); - -++count; - -} while (count < 4); - -// Outputs '0', '1', '2' and '3' - -# for Loop - -_for _ loops are used when the number of iterations are predefined - -for loop executes in three inherent steps: - -for (/*initial value*/;/*condition*/;/*increment*/) - -{ - -/* Code to be executed */ - -} - -The initial value is set before the first iteration. The condition is checked before each iteration, and the increment is executed after each iteration. - -# for Loop (continued) - -Try to understand the following two examples: - + + + +## Overview + +* While loop +* For loop +* Nested loops +* Breaking out of loops + +## Why loops? + +* Loops are useful when you need to repeat a block of code multiple times and to avoid code like this: + ```csharp + Console.WriteLine(count); + ++count; + Console.WriteLine(count); + ++count; + Console.WriteLine(count); + // Yuck! Nobody wants to see this. + ``` +* Loops can also be used for iterating through arrays and lists (more on those later) + +## `while` loop + +* A `while` loop keeps executing its code block as long as the condition in its statement is `true`: + ```csharp + int count = 0; + while(count < 4) + { + Console.WriteLine(count); + ++count; + } + // Outputs '0', '1', '2' and '3' + ``` + +## `do...while` loop + +* `do...while` works the same way as `while`, but there's a key difference: +* The code is executed ***at least once*** and the condition is checked ***at the end***: + ```csharp + int count = 0; + do + { + Console.WriteLine(count); + ++count; + } while (count < 4); + // Outputs '0', '1', '2' and '3' + ``` + +## `for` loop + +* `for` loops are used when the number of iterations is predefined +* `for` loop is initialized in three steps: + ```csharp + for (; ; ) + { + /* Code to be executed */ + } + ``` +* The ***initial value*** is set before the first iteration +* The ***condition*** is checked before each iteration +* The ***increment*** is executed after each iteration + +## For loop: An example + +See the following two examples: + +
+
+ +```csharp for (int i = 0; i < 4; ++i) - { - -Console.WriteLine - -("Current: " + i); - + Console.WriteLine + ("Current: " + i); } /* Outputs - Current: 0 - Current: 1 - Current: 2 - Current: 3 - */ +``` -for (int i = 8; i > 0; i -= 2) +
+
+```csharp +for (int i = 8; i > 0; i -= 2) { - -Console.WriteLine - -("Current: " + i); - + Console.WriteLine + ("Current: " + i); } /* Outputs - Current: 8 - Current: 6 - Current: 4 - Current: 2 - */ +``` -We start from zero, print the current value and add one until the value is no longer under four. - -We start from eight, print the current value and remove two until the value is no longer larger than zero. - -for (int i = 8; i > 0; i -= 2) - -{ +
+
-Console.WriteLine - -("Current: " + i); - -} - -for (int i = 0; i < 4; ++i) - -{ - -Console.WriteLine - -("Current: " + i); - -} - -# Nested Loops +--- -Nothing prevents you from using a loop inside another loop: -int columns = 3; +
+
-int rows = 4; +We start from zero `int i = 0` -for (int i = 0; i < columns; ++i) +print the current value -{ +add one `++i` -for (int j = 0; j < rows; ++j) +until the value is no longer under four +`i < 4`. +```csharp +for (int i = 0; i < 4; ++i) { - -// Console.Write doesn't add a line break after printout - -Console.Write("(" + j.ToString() + "," + i.ToString() + ")"); - + Console.WriteLine + ("Current: " + i); } +``` -Console.WriteLine(""); +
+
-} - -![](imgs/4%20Loops_1.png) - -# Breaking Out of Loops +We start from eight `int i = 8` -To stop the execution of a loop completely, use the _break _ -keyword: +print the current value -int i = 0; +remove two `i -= 2` -while(true) +until the value is no longer over zero +`i >` +```csharp +for (int i = 8; i > 0; i -= 2) { - -++i; - -if(i > 3) - -break; - + Console.WriteLine + ("Current: " + i); } +``` -// i is now 4 +
+
-To skip the current iteration, use the _continue _ -keyword +## Nested Loops -int i = 0; +Nothing prevents you from using a loop inside another loop: -while(i < 10) +
+
+```csharp +int columns = 3; +int rows = 4; +for (int i = 0; i < columns; ++i) { - -++i; - -if (i % 2 == 0) - -continue; - -Console.WriteLine(i); - + for (int j = 0; j < rows; ++j) + { + // Console.Write doesn't add a linebreak after printout + Console.Write( + "(" + j.ToString() + "," + i.ToString() + ")" + ); + } + Console.WriteLine(""); } +``` -// Prints every odd number from 1 to 10 +
+
-# Exercise 1: The Main Loop - -At the heart of every dynamic program, there is something called the _event loop _ a.k.a the _main loop. _ Create a console application which keeps asking the user for an input, until the user inputs "quit". +![](imgs/4%20Loops_1.png) -Expand the program so that if the user inputs "help", a dialog shows up which shows the command and explanation for the command for both quit and help commands. +
+
+ +## Breaking out of loops with `break` + +* To stop the execution of a loop before its end condition is met, use the `break` keyword: + ```csharp + int i = 0; + while(true) + { + ++i; + if(i > 3) + break; + } + // i is now 4 + ``` + +## Skipping ahead with `continue` + +* To skip the current iteration, use the `continue` keyword + ```csharp + int i = 0; + while(i < 10) + { + ++i; + if (i % 2 == 0) + continue; + Console.WriteLine(i); + } + // Prints every odd number from 1 to 10 + ``` + +## Exercise 1: The main loop + + +At the heart of every program that doesn't quit right away, there is something called the **_main loop_** + +1) Create a console application that keeps asking the user for input until the user inputs `quit`. +2) Expand the program so that if the user inputs `help`, a help screen appears that explains what both `quit` and `help` commands do (See next image). -![](imgs/4%20Loops_2.png) +--- + -# Exercise 2: Prime Numbers +![w:1200px](imgs/4%20Loops_2.png) -Prime number is a number greater than 1 that is not a product of two small numbers, i.e. cannot be divided into a whole number. (Wikipedia: [https://en.wikipedia.org/wiki/Prime\_number](https://en.wikipedia.org/wiki/Prime_number) ) +## Exercise 2: Prime numbers + -Create a console application which prints all the prime numbers between 0 .. 40 +A [Prime number](https://en.wikipedia.org/wiki/Prime_number) is a natural number greater than 1 that is not a product of two natural numbers (so it cannot be divided into a whole number). -![](imgs/4%20Loops_3.png) +Create a console application which prints all the prime numbers between 0 and 40. -# Assignments +## Assignments [Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/4.%20Loops) diff --git a/5-arrays-and-lists-slides.html b/5-arrays-and-lists-slides.html index 31066f0..af4fa77 100644 --- a/5-arrays-and-lists-slides.html +++ b/5-arrays-and-lists-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 0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv */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 bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm */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%}

Arrays and Lists

-
+

Overview

  • Arrays
  • @@ -26,7 +26,7 @@
  • Foreach
-
+

Arrays

  • Arrays are a collection of variables of the same type
  • @@ -38,7 +38,7 @@
  • Declaring an array does not yet allocate space from memory
-
+

Initializing arrays

  • Initialize an array with a length of 3 using the following syntax:
    int[] userIds = new int[3];
    @@ -50,7 +50,7 @@
     
  • Indexing starts from 0, so the above line assigns a value of 104 to the first element of the array
-
+
  • You can also create an array and instantly populate it with values with one statement:
    string[] names = new string[3] { "Johannes", "Rene", "Ville" };
     
    @@ -60,7 +60,7 @@
-
+

Multidimensional Arrays