Skip to content

Commit

Permalink
-- lecture 8 - flow control
Browse files Browse the repository at this point in the history
  • Loading branch information
djnzx committed Oct 3, 2024
1 parent 5b570ad commit cef2e7c
Show file tree
Hide file tree
Showing 16 changed files with 391 additions and 11 deletions.
17 changes: 17 additions & 0 deletions questionare/08.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Лекція 13. Контрольне опитування

### Чи ....

- так
- ні
- не знаю
- залежить від ситуації
- що таке ...

### ....

- так
- ні
- не знаю
- залежить від ситуації
- що таке ...
Binary file removed slides-keynote/Rust7-patternmatching.key
Binary file not shown.
Binary file added slides-keynote/Rust8-flow-control.key
Binary file not shown.
Binary file added slides-pdf/Лекція 13.pdf
Binary file not shown.
68 changes: 68 additions & 0 deletions src/lectures/lec08/a1if.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/// if/else
/// match
/// for/while/loop
/// continue/break
fn basic_flow() {
fn f(a: u32) -> f32 {
todo!()
}
fn g(a: f32) -> bool {
todo!()
}
fn h(a: bool) -> String {
todo!()
}

fn app(a: u32) -> String {
let b = f(a);
let c = g(b);
let d = h(c);
d
}

let n: i32 = 5;

if n < 0 {
println!("{} is negative", n);
} else {
println!("{} is non-negative", n);
}
}

#[test]
fn if_basic_syntax() {
let n: i32 = 5;

if n < 0 {
println!("{} is negative", n);
} else {
println!("{} is non-negative", n);
}
}

#[test]
fn if_stacked_syntax() {
let n: i32 = 5;

if n < 0 {
println!("{} is negative", n);
} else if n > 0 {
println!("{} is positive", n);
} else {
println!("{} is zero", n);
}
}

#[test]
fn if_returns_syntax() {
let n: i32 = 5;

let s = if n < 0 {
"negative"
} else if n > 0 {
"positive"
} else {
"zero"
};
}
39 changes: 39 additions & 0 deletions src/lectures/lec08/a2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
### not, !

| a | !a |
|-------|:-----:|
| true | false |
| false | true |

### or, a || b

| a | b | a\|\|b |
|-------|-------|:------:|
| false | false | false |
| false | true | true |
| true | false | true |
| true | true | true |

### and, a && b

| a | b | a&&b |
|-------|-------|:-----:|
| false | false | false |
| false | true | false |
| true | false | false |
| true | true | true |

### xor, a ^ b

| a | b | a^b |
|-------|-------|:-----:|
| false | false | false |
| false | true | true |
| true | false | true |
| true | true | false |

- (a || b) && c == a && c || b && c
- a || !a = true
- a && !a = false
- a || true = true
- a && true = a
11 changes: 11 additions & 0 deletions src/lectures/lec08/a2boolean_algebra.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn not(x: bool) -> bool {
!x
}

fn and(x: bool, y: bool) -> bool {
x && y
}

fn or(x: bool, y: bool) -> bool {
x || y
}
25 changes: 25 additions & 0 deletions src/lectures/lec08/a3for.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// if/else
/// match
/// for/while/loop
/// continue/break
// 4h24min
#[test]
fn for_basic_not_including_syntax() {
for n in 0..5 {
println!("{}", n);
}
}

#[test]
fn for_basic_including_syntax() {
for n in 0..=5 {
println!("{}", n);
}
}

fn for_infinite() {
for n in 0.. {
println!("{}", n);
}
}
16 changes: 16 additions & 0 deletions src/lectures/lec08/a4while.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[test]
fn while_basic_syntax() {
let mut n = 1;
while n < 20 {
if n % 15 == 0 {
println!("{}: fizzbuzz", n);
} else if n % 3 == 0 {
println!("{}: fizz", n);
} else if n % 5 == 0 {
println!("{}: buzz", n);
} else {
println!("{}", n);
}
n += 1;
}
}
86 changes: 86 additions & 0 deletions src/lectures/lec08/a5loop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/// infinite
#[ignore]
#[test]
fn loop_syntax() {
let mut n = 0u32;
loop {
n += 1;
println!("{}", n);
}
}

#[test]
fn loop_syntax_with_break() {
let mut n = 0u32;
loop {
n += 1;
println!("{}", n);
if n == 10 {
break;
}
}
}

#[ignore]
#[test]
fn loop_syntax_with_break_and_continue() {
let mut n = 0u32;
loop {
// ...
// ...
n += 1;
println!("{}", n);
if n % 2 == 0 {
continue;
}
// ...
// ...
if n == 100 {
break;
}
// ...
// ...
}
}

#[test]
fn loop_syntax_with_labels() {
let mut n = 0u32;

'a: loop {
n += 1;

println!("b");

if n == 100 {
break;
};
continue 'a;
}
}

#[test]
fn loop_old_usage() {
let mut total = 1;

loop {
total *= 2;
if total > 1000 {
break;
}
}
assert_eq!(1024, total);
}

#[test]
fn loop_returns() {
let mut total = 1;

let outcome = loop {
total *= 2;
if total > 1000 {
break total;
}
};
assert_eq!(1024, outcome);
}
31 changes: 31 additions & 0 deletions src/lectures/lec08/a6break.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[test]
fn break_for() {
for n in 0.. {
println!("{}", n);
if n == 999 {
break;
}
}
}

#[test]
fn break_while() {
let mut n: i32 = 0;
while true {
n += 1;
if n == 999 {
break;
}
}
}

#[test]
fn break_loop() {
let mut n: i32 = 0;
loop {
n += 1;
if n == 999 {
break;
}
}
}
12 changes: 12 additions & 0 deletions src/lectures/lec08/a7continue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[test]
fn continue_syntax() {
let mut n: i32 = 0;
for i in 0..100 {
n = n + 1; // 100 times
if i % 2 == 0 {
continue;
}
n = n + 1; // 50 times
}
assert_eq!(150, n);
}
26 changes: 26 additions & 0 deletions src/lectures/lec08/a8nested.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[test]
fn nested_loops() {
let mut x = 0;
let mut y = 0;
loop {
loop {
x += 1;
y += 1;
print!("{} ", x);
if x == 5 {
break;
}
}
loop {
x -= 1;
y += 1;
print!("{} ", x);
if x == 0 {
break;
}
}
if y >= 20 {
break;
}
}
}
38 changes: 38 additions & 0 deletions src/lectures/lec08/a9control.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#[test]
fn pattern_match() {
enum Solution {
NoRoots,
OneRoot(f32),
TwoRoots(f32, f32),
}

fn solve_quadratic(a: f32, b: f32, c: f32) -> Solution {
use Solution::*;

let d = b.powi(2) - 4.0 * a * c;

match d {
d if d > 0. => {
let dq = f32::sqrt(d);
let a2 = a * 2.;
TwoRoots((-b - dq) / a2, (-b + dq) / a2)
}
0. => OneRoot(-b / (2. * a)),
_ => NoRoots,
}
}

fn solve_and_report(a: f32, b: f32, c: f32) {
use Solution::*;

match solve_quadratic(a, b, c) {
NoRoots => println!("quadratic equation has no roots"),
OneRoot(x) => println!("quadratic equation has one root: {x}"),
TwoRoots(x1, x2) => println!("quadratic equation has two roots: {x1}, {x2}"),
}
}

solve_and_report(1., 2., 2.); // no roots
solve_and_report(1., 2., 1.); // one root
solve_and_report(1., 1., 0.); // two roots
}
10 changes: 9 additions & 1 deletion src/lectures/lec08/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@

mod a1if;
mod a2boolean_algebra;
mod a3for;
mod a4while;
mod a5loop;
mod a6break;
mod a7continue;
mod a8nested;
mod a9control;
Loading

0 comments on commit cef2e7c

Please sign in to comment.