finish lecture 4

- edit lecture 5
main
borb 2 weeks ago
parent 2f29b5f8ea
commit 05d1e4a420

File diff suppressed because one or more lines are too long

@ -1,246 +1,247 @@
# Loops
![](imgs/4%20Loops_0.png)
--- ---
marp: true
# Overview paginate: true
math: mathjax
While Loop theme: buutti
title: 4. Loops
For Loop ---
Nested Loops
Breaking Out of Loops
# Loops # Loops
Loops are useful when you need to repeat a block of code multiple times and to avoid code like this: <!-- headingDivider: 5 -->
<!-- class: invert -->
Console.WriteLine(count);
## Overview
++count;
* While loop
Console.WriteLine(count); * For loop
* Nested loops
++count; * Breaking out of loops
Console.WriteLine(count); ## Why loops?
// Yuck! Nobody wants to see this. * Loops are useful when you need to repeat a block of code multiple times and to avoid code like this:
```csharp
Loops can also be used for iterating through arrays and lists (more on those later) Console.WriteLine(count);
++count;
# while Loop Console.WriteLine(count);
++count;
_while_ loop keeps executing its code block as long as the condition in its statement is true: Console.WriteLine(count);
// Yuck! Nobody wants to see this.
int count = 0; ```
* Loops can also be used for iterating through arrays and lists (more on those later)
while(count < 4)
## `while` loop
{
* A `while` loop keeps executing its code block as long as the condition in its statement is `true`:
Console.WriteLine(count); ```csharp
int count = 0;
++count; while(count < 4)
{
} Console.WriteLine(count);
++count;
// Outputs '0', '1', '2' and '3' }
// 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: ## `do...while` loop
int count = 0; * `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***:
do ```csharp
int count = 0;
{ do
{
Console.WriteLine(count); Console.WriteLine(count);
++count;
++count; } while (count < 4);
// Outputs '0', '1', '2' and '3'
} while (count < 4); ```
// Outputs '0', '1', '2' and '3' ## `for` loop
# for Loop * `for` loops are used when the number of iterations is predefined
* `for` loop is initialized in three steps:
_for _ loops are used when the number of iterations are predefined ```csharp
for (<initial value>; <condition>; <increment>)
for loop executes in three inherent steps: {
/* Code to be executed */
for (/*initial value*/;/*condition*/;/*increment*/) }
```
{ * The ***initial value*** is set before the first iteration
* The ***condition*** is checked before each iteration
/* Code to be executed */ * The ***increment*** is executed after each iteration
} ## For loop: An example
The initial value is set before the first iteration. The condition is checked before each iteration, and the increment is executed after each iteration. See the following two examples:
# for Loop (continued) <div class='columns' markdown='1'>
<div markdown='1'>
Try to understand the following two examples:
```csharp
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
{ {
Console.WriteLine
Console.WriteLine ("Current: " + i);
("Current: " + i);
} }
/* Outputs /* Outputs
Current: 0 Current: 0
Current: 1 Current: 1
Current: 2 Current: 2
Current: 3 Current: 3
*/ */
```
for (int i = 8; i > 0; i -= 2) </div>
<div markdown='1'>
```csharp
for (int i = 8; i > 0; i -= 2)
{ {
Console.WriteLine
Console.WriteLine ("Current: " + i);
("Current: " + i);
} }
/* Outputs /* Outputs
Current: 8 Current: 8
Current: 6 Current: 6
Current: 4 Current: 4
Current: 2 Current: 2
*/ */
```
We start from zero, print the current value and add one until the value is no longer under four. </div>
</div>
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; <div class='columns' markdown='1'>
<div markdown='1'>
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.WriteLine
// Console.Write doesn't add a line break after printout ("Current: " + i);
Console.Write("(" + j.ToString() + "," + i.ToString() + ")");
} }
```
Console.WriteLine(""); </div>
<div markdown='1'>
} We start from eight `int i = 8`
![](imgs/4%20Loops_1.png)
# Breaking Out of Loops
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)
{ {
Console.WriteLine
++i; ("Current: " + i);
if(i > 3)
break;
} }
```
// i is now 4 </div>
</div>
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) <div class='columns' style='grid-template-columns: 3fr 1fr;' markdown='1'>
<div markdown='1'>
```csharp
int columns = 3;
int rows = 4;
for (int i = 0; i < columns; ++i)
{ {
for (int j = 0; j < rows; ++j)
++i; {
// Console.Write doesn't add a linebreak after printout
if (i % 2 == 0) Console.Write(
"(" + j.ToString() + "," + i.ToString() + ")"
continue; );
}
Console.WriteLine(i); Console.WriteLine("");
} }
```
// Prints every odd number from 1 to 10 </div>
<div markdown='1'>
# Exercise 1: The Main Loop ![](imgs/4%20Loops_1.png)
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".
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. </div>
</div>
## 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
<!--_class: "exercise invert" -->
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) ---
<!--_class: "exercise invert" -->
# 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
<!--_class: "exercise invert" -->
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) [Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/4.%20Loops)

@ -13,10 +13,10 @@
/* buutti.css */ /* 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 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%}</style></head><body><div class="bespoke-marp-osc"><button data-bespoke-marp-osc="prev" tabindex="-1" title="Previous slide">Previous slide</button><span data-bespoke-marp-osc="page"></span><button data-bespoke-marp-osc="next" tabindex="-1" title="Next slide">Next slide</button><button data-bespoke-marp-osc="fullscreen" tabindex="-1" title="Toggle fullscreen (f)">Toggle fullscreen</button><button data-bespoke-marp-osc="presenter" tabindex="-1" title="Open presenter view (p)">Open presenter view</button></div><div id=":$p"><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="1" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> /* @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%}</style></head><body><div class="bespoke-marp-osc"><button data-bespoke-marp-osc="prev" tabindex="-1" title="Previous slide">Previous slide</button><span data-bespoke-marp-osc="page"></span><button data-bespoke-marp-osc="next" tabindex="-1" title="Next slide">Next slide</button><button data-bespoke-marp-osc="fullscreen" tabindex="-1" title="Toggle fullscreen (f)">Toggle fullscreen</button><button data-bespoke-marp-osc="presenter" tabindex="-1" title="Open presenter view (p)">Open presenter view</button></div><div id=":$p"><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="1" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="1" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h1 id="arrays-and-lists">Arrays and Lists</h1> <h1 id="arrays-and-lists">Arrays and Lists</h1>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="2" data-marpit-fragments="5" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="2" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="2" data-marpit-fragments="5" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="2" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h2 id="overview">Overview</h2> <h2 id="overview">Overview</h2>
<ul> <ul>
<li data-marpit-fragment="1">Arrays</li> <li data-marpit-fragment="1">Arrays</li>
@ -26,7 +26,7 @@
<li data-marpit-fragment="5">Foreach</li> <li data-marpit-fragment="5">Foreach</li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="3" data-marpit-fragments="5" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="3" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="3" data-marpit-fragments="5" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="3" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h2 id="arrays">Arrays</h2> <h2 id="arrays">Arrays</h2>
<ul> <ul>
<li data-marpit-fragment="1">Arrays are a collection of variables of the same type</li> <li data-marpit-fragment="1">Arrays are a collection of variables of the same type</li>
@ -38,7 +38,7 @@
<li data-marpit-fragment="5">Declaring an array does not yet allocate space from memory</li> <li data-marpit-fragment="5">Declaring an array does not yet allocate space from memory</li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="4" data-marpit-fragments="3" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="4" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="4" data-marpit-fragments="3" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="4" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h3 id="initializing-arrays">Initializing arrays</h3> <h3 id="initializing-arrays">Initializing arrays</h3>
<ul> <ul>
<li data-marpit-fragment="1"><em><strong>Initialize</strong></em> an array with a length of 3 using the following syntax:<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-built_in">int</span>[] userIds = <span class="hljs-keyword">new</span> <span class="hljs-built_in">int</span>[<span class="hljs-number">3</span>]; <li data-marpit-fragment="1"><em><strong>Initialize</strong></em> an array with a length of 3 using the following syntax:<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-built_in">int</span>[] userIds = <span class="hljs-keyword">new</span> <span class="hljs-built_in">int</span>[<span class="hljs-number">3</span>];
@ -50,7 +50,7 @@
<li data-marpit-fragment="3">Indexing starts from 0, so the above line assigns a value of <code>104</code> to the <em><strong>first</strong></em> element of the array</li> <li data-marpit-fragment="3">Indexing starts from 0, so the above line assigns a value of <code>104</code> to the <em><strong>first</strong></em> element of the array</li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="5" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="5" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="5" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="5" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<ul> <ul>
<li data-marpit-fragment="1">You can also create an array and instantly populate it with values with one statement:<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-built_in">string</span>[] names = <span class="hljs-keyword">new</span> <span class="hljs-built_in">string</span>[<span class="hljs-number">3</span>] { <span class="hljs-string">&quot;Johannes&quot;</span>, <span class="hljs-string">&quot;Rene&quot;</span>, <span class="hljs-string">&quot;Ville&quot;</span> }; <li data-marpit-fragment="1">You can also create an array and instantly populate it with values with one statement:<pre is="marp-pre" data-auto-scaling="downscale-only"><code class="language-csharp"><span class="hljs-built_in">string</span>[] names = <span class="hljs-keyword">new</span> <span class="hljs-built_in">string</span>[<span class="hljs-number">3</span>] { <span class="hljs-string">&quot;Johannes&quot;</span>, <span class="hljs-string">&quot;Rene&quot;</span>, <span class="hljs-string">&quot;Ville&quot;</span> };
</code></pre> </code></pre>
@ -60,7 +60,7 @@
</li> </li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="6" data-marpit-fragments="3" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="6" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="6" data-marpit-fragments="3" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="6" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h2 id="multidimensional-arrays">Multidimensional Arrays</h2> <h2 id="multidimensional-arrays">Multidimensional Arrays</h2>
<ul> <ul>
<li data-marpit-fragment="1">C# supports multidimensional arrays</li> <li data-marpit-fragment="1">C# supports multidimensional arrays</li>
@ -77,7 +77,7 @@ Console.WriteLine(letters[<span class="hljs-number">1</span>, <span class="hljs-
</li> </li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="7" data-marpit-fragments="4" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="7" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="7" data-marpit-fragments="4" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="7" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h2 id="lists">Lists</h2> <h2 id="lists">Lists</h2>
<ul> <ul>
<li data-marpit-fragment="1">The .NET Framework Library <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic?view=net-9.0"><code>System.Collections.Generic</code></a> offers another structure to contain multiple variables: the <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-9.0">List</a> class</li> <li data-marpit-fragment="1">The .NET Framework Library <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic?view=net-9.0"><code>System.Collections.Generic</code></a> offers another structure to contain multiple variables: the <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-9.0">List</a> class</li>
@ -92,7 +92,7 @@ Console.WriteLine(letters[<span class="hljs-number">1</span>, <span class="hljs-
</li> </li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="8" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="8" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="8" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="8" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h3 id="why-lists">Why lists?</h3> <h3 id="why-lists">Why lists?</h3>
<ul> <ul>
<li data-marpit-fragment="1">The key benefit of using Lists lies in its built-in functionalities<br /> <li data-marpit-fragment="1">The key benefit of using Lists lies in its built-in functionalities<br />
@ -100,7 +100,7 @@ Console.WriteLine(letters[<span class="hljs-number">1</span>, <span class="hljs-
<li data-marpit-fragment="2">You can see a list (heh) of available methods in Visual Studio by following the list variable with a dot</li> <li data-marpit-fragment="2">You can see a list (heh) of available methods in Visual Studio by following the list variable with a dot</li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="9" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="9" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="9" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="9" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h2 id="arrays-vs-lists">Arrays vs Lists</h2> <h2 id="arrays-vs-lists">Arrays vs Lists</h2>
<ul> <ul>
<li data-marpit-fragment="1">The memory allocation of an array is <em><strong>static</strong></em> a.k.a. fixed, but list memory is <em><strong>dynamic</strong></em></li> <li data-marpit-fragment="1">The memory allocation of an array is <em><strong>static</strong></em> a.k.a. fixed, but list memory is <em><strong>dynamic</strong></em></li>
@ -115,7 +115,7 @@ intArray.Remove(<span class="hljs-number">0</span>);
</li> </li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="10" data-marpit-fragments="6" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="10" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="10" data-marpit-fragments="6" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="10" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h3 id="when-to-use-arrays-or-lists">When to use arrays or lists?</h3> <h3 id="when-to-use-arrays-or-lists">When to use arrays or lists?</h3>
<ul> <ul>
<li data-marpit-fragment="1">Use arrays when you... <li data-marpit-fragment="1">Use arrays when you...
@ -132,7 +132,7 @@ intArray.Remove(<span class="hljs-number">0</span>);
</li> </li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="11" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="11" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="11" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="11" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h2 id="example-iterating-an-array-with-a-for-loop">Example: Iterating an array with a for loop</h2> <h2 id="example-iterating-an-array-with-a-for-loop">Example: Iterating an array with a for loop</h2>
<div class='columns21' markdown='1'> <div class='columns21' markdown='1'>
<div markdown='1'> <div markdown='1'>
@ -154,7 +154,7 @@ intArray.Remove(<span class="hljs-number">0</span>);
</div> </div>
</div> </div>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="12" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="12" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="12" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="12" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h2 id="iterating-a-list">Iterating a List</h2> <h2 id="iterating-a-list">Iterating a List</h2>
<ul> <ul>
<li data-marpit-fragment="1">You can iterate the elements of a list with a for loop the same way as an array</li> <li data-marpit-fragment="1">You can iterate the elements of a list with a for loop the same way as an array</li>
@ -169,7 +169,7 @@ intArray.Remove(<span class="hljs-number">0</span>);
</li> </li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="13" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="13" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="13" data-marpit-fragments="2" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="13" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h2 id="foreach-statement"><code>foreach</code> Statement</h2> <h2 id="foreach-statement"><code>foreach</code> Statement</h2>
<ul> <ul>
<li data-marpit-fragment="1">There is one more statement to use to iterate arrays and lists: the <code>foreach</code> statement</li> <li data-marpit-fragment="1">There is one more statement to use to iterate arrays and lists: the <code>foreach</code> statement</li>
@ -195,7 +195,7 @@ intArray.Remove(<span class="hljs-number">0</span>);
</div> </div>
</div> </div>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="14" data-marpit-fragments="3" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="14" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="14" data-marpit-fragments="3" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="14" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h3 id="note-about-foreach">Note about <code>foreach</code></h3> <h3 id="note-about-foreach">Note about <code>foreach</code></h3>
<ul> <ul>
<li data-marpit-fragment="1">However, <code>foreach</code> creates a copy of each element in the object so the element cannot be mutated directly:<br /> <li data-marpit-fragment="1">However, <code>foreach</code> creates a copy of each element in the object so the element cannot be mutated directly:<br />
@ -204,7 +204,7 @@ intArray.Remove(<span class="hljs-number">0</span>);
<li data-marpit-fragment="3">If you need to change every element, consider creating a new list instead</li> <li data-marpit-fragment="3">If you need to change every element, consider creating a new list instead</li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="15" data-marpit-fragments="5" data-paginate="true" data-class="exercise invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="exercise invert" data-marpit-pagination="15" style="--paginate:true;--class:exercise invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="15" data-marpit-fragments="5" data-paginate="true" data-class="exercise invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="exercise invert" data-marpit-pagination="15" style="--paginate:true;--class:exercise invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h2 id="exercise-1-expanding-the-console-application">Exercise 1: Expanding the Console Application</h2> <h2 id="exercise-1-expanding-the-console-application">Exercise 1: Expanding the Console Application</h2>
<ol> <ol>
@ -217,7 +217,7 @@ intArray.Remove(<span class="hljs-number">0</span>);
<li data-marpit-fragment="5"><em><strong>Note</strong></em>: you can use the <code>int.Parse()</code> method to parse the user input string to an integer</li> <li data-marpit-fragment="5"><em><strong>Note</strong></em>: you can use the <code>int.Parse()</code> method to parse the user input string to an integer</li>
</ul> </ul>
</section> </section>
</foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="16" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv" lang="en-US" class="invert" data-marpit-pagination="16" style="--paginate:true;--class:invert;--heading-divider:5;--theme:0l9lmw6o8hzbn7azbwom7xifcdckrnrzxwa8batouzv;" data-marpit-pagination-total="16"> </foreignObject></svg><svg data-marpit-svg="" viewBox="0 0 1280 720"><foreignObject width="1280" height="720"><section id="16" data-paginate="true" data-class="invert" data-heading-divider="5" data-theme="bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm" lang="en-US" class="invert" data-marpit-pagination="16" style="--paginate:true;--class:invert;--heading-divider:5;--theme:bjzisialpqm2sihzm4yjrz548zloo23j3k4fev0f0ykm;" data-marpit-pagination-total="16">
<h2 id="assignments">Assignments</h2> <h2 id="assignments">Assignments</h2>
<p><a href="https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/5.%20Arrays%20&amp;%20Lists">Assignments about this topic can be found here</a></p> <p><a href="https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/5.%20Arrays%20&amp;%20Lists">Assignments about this topic can be found here</a></p>
</section> </section>

@ -204,5 +204,4 @@ foreach (string name in names)
## Assignments ## Assignments
[Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/5.%20Arrays%20&%20Lists) [Assignments about this topic can be found here](https://gitea.buutti.com/education/academy-assignments/src/branch/master/C%23%20Basics/5.%20Arrays%20&%20Lists)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Loading…
Cancel
Save