Skip to content

Commit

Permalink
Start of intro-to-programming module
Browse files Browse the repository at this point in the history
  • Loading branch information
coastalwhite committed Nov 30, 2023
1 parent d1f4407 commit 3e22087
Show file tree
Hide file tree
Showing 87 changed files with 890 additions and 0 deletions.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
21 changes: 21 additions & 0 deletions content/mods/Z-starting-programming/exercises/arrays.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
fn main() {
let list = [1, 1337, 42];

let mut sum = 0;
for number in list {
sum = sum + number;
}

// TODO: print the following:
// 1. print the numbers one by one
// 2. an addition line
// 3. the sum
//
// For example, for the list [5, 9, 2] your output would be:
//
// 5
// 9
// 2
// -------+
// 16
}
12 changes: 12 additions & 0 deletions content/mods/Z-starting-programming/exercises/conditionals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fn main() {
let age = 17;

if age > 17 {
println!("You are an adult 👩");
} else {
println!("You are a child 🧒");
}

// TODO: Add a clause for 13 - 17 year olds to be `teenagers`
// TIP: you can use the 🏫 emoji
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
println!("Hello World");

// TODO: add another print, where it prints your name
}
19 changes: 19 additions & 0 deletions content/mods/Z-starting-programming/exercises/functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
fn x_squared_plus_one(number: i32) -> i32 {
// TODO: Implement the expression for this
}

fn double(number: i32) -> i32 {
2 * number
}

fn main() {
let double_6 = double(6);
println!("2 * 6: {double_6}");

let six_squared_plus_one = x_squared_plus_one(6);
println!("6*6 + 1: {six_squared_plus_one}");

// TODO: Create the function named `x_cubed_plus_x_squared`
let six_cubed_plus_six_squared = x_cubed_plus_x_squared(6);
println!("6*6*6 + 6*6: {six_cubed_plus_six_squared}");
}
12 changes: 12 additions & 0 deletions content/mods/Z-starting-programming/exercises/variables.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fn main() {
let x = 10;
let double_x = 2 * x;
let x_plus_twenty = x + 22;

// `{x}` is replaced with the value of `x`
println!("x: {x}");

// TODO: create variables and print the following expressions:
// * y = (61 + 130) * 7
// * z = (x + y) * 5 + 2
}
16 changes: 16 additions & 0 deletions content/mods/Z-starting-programming/images/A0-abstract-program.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
57 changes: 57 additions & 0 deletions content/mods/Z-starting-programming/topics/booleans/slides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
layout: section
---

# Types --- Booleans

---

# Types --- Booleans (1)

* Boolean answer a *yes/no question* (`true` or `false`)
* Examples:
* Is $x$ even? $\to$ $x$ is even
* Is $x$ larger than 42? $\to$ $x$ is larger than 42
* Does text start with "Wow"? $\to$ text starts with "Wow"
* Is the left mouse button pressed? $\to$ Left mouse button is pressed

```rust
let is_x_larger_than_42 = x > 42;

let does_text_start_with_wow = text.starts_with("Wow");
```

---

# Types --- Booleans (2)

* Booleans are also the conditions for `if`

```rust
let age = 19;
let is_adult = age > 17;

if is_adult {
println!("You are an adult 👩");
} else {
println!("You are a child 🧒");
}
```

---

# Types --- Booleans (3)

* Booleans can be combined
* This or that?
* This and that?

```rust
if number > 10 && number < 20 {
println!("{number} is somewhere between 11 and 19");
}

if number < 10 || number > 20 {
println!("{number} is lower than 10 or higher than 20");
}
```
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
layout: section
---

# Types --- Composite Structures

---

# Types --- Composite Structures

* Create our own type by combining types

```rust
struct Point {
x: i32,
y: i32,
}

// Define a new point
let p = Point { x: 10, y: 20 };

// Print the properties
println!("Point's x: {}", point.x);
println!("Point's y: {}", point.y);
```
Empty file.
95 changes: 95 additions & 0 deletions content/mods/Z-starting-programming/topics/conditionals/slides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
layout: section
---

# Conditionals --- `if`

---

# Conditionals

* Output changes based on a condition
* *Branches* the execution flow
* Commonly done with `if` and optional `else`

```rust
if (
// A condition
) {
// If the condition is true do this.
} else {
// Else do this
}
```

---

# Conditionals --- Example (1)

```rust {all|1|3|4}
let age = 19;

if age > 17 {
println!("You are an adult 👩");
} else {
println!("You are a child 🧒");
}
```

#### Output:

```
You are an adult 👩
```

---

# Conditionals --- Example (2)

#### Code:

```rust {all|1|3|4}
let age = 12;

if age > 17 {
println!("You are an adult 👩");
} else {
println!("You are a child 🧒");
}
```

#### Output:

```
You are a child 🧒
```

---

# Conditionals --- Example (3)

```rust {1|3|4}
let role = "Student";

// `==` asks whether they are equal
if role == "Administrator" {
println!("All systems operational! 🔓");
} else {
println!("You don't have the authority!");
}
```

#### Output:

```
You are an adult 👩
```

---

# Conditionals --- Operations

- Equality: `==`
- Inequality: `!=`
- Comparison: `<`, `>`, `<=`, `>=`
- Invert: `!`
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Loading

0 comments on commit 3e22087

Please sign in to comment.