Skip to main content

rl_lang/docs/entries/tutorial/t1/
p6.rs

1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2pub static STEP_LOOPS: ConceptEntry = ConceptEntry {
3    name: "6. repeating things",
4    summary: "repeating things",
5    category: ConceptCategory::ControlFlow,
6    prerequisites: &[],
7    descriptions: &[
8        DescriptionEntry {
9            kind: DescriptionKind::Explanation,
10            title: None,
11            description: "a while loop runs its block over and over as long as its condition stays true. as soon as the condition becomes false the loop stops",
12            examples: &[
13                "dec int count = 3\n\nwhile (count > 0) {\n    println(count)\n    count -= 1\n}\n// 3\n// 2\n// 1",
14            ],
15            expected_output: &[],
16        },
17        DescriptionEntry {
18            kind: DescriptionKind::Explanation,
19            title: None,
20            description: "break exits the loop immediately no matter what the condition says. use it when something happens inside the loop that means you are done",
21            examples: &[
22                "dec int i = 0\n\nwhile (true) {\n    println(i)\n    i += 1\n    if (i == 3) { break } // stops after printing 0, 1, 2\n}",
23            ],
24            expected_output: &[],
25        },
26        DescriptionEntry {
27            kind: DescriptionKind::Explanation,
28            title: None,
29            description: "continue skips the rest of the current iteration and jumps back to the condition check",
30            examples: &[
31                "dec int i = 0\n\nwhile (i < 5) {\n    i += 1\n    if (i == 3) { continue } // skip 3\n    println(i)\n}\n// 1\n// 2\n// 4\n// 5",
32            ],
33            expected_output: &[],
34        },
35        DescriptionEntry {
36            kind: DescriptionKind::Explanation,
37            title: None,
38            description: "exercise: wrap your guess check in a loop so the player keeps guessing until they get it right or run out of attempts. decrease attempts_left each round and break when they win or lose\n\nexpected output:\n  attempts left: 10\n  enter your guess: 30\n  too low!\n  attempts left: 9\n  enter your guess: 42\n  correct! you got it!",
39            examples: &[
40                "get read_int from std::io\nget concat   from std::str\n\nCONST int MAX_ATTEMPTS = 10\ndec int secret         = 42\ndec int attempts_left  = MAX_ATTEMPTS\n\nwhile (attempts_left > 0) {\n    println(concat(\"attempts left: \", attempts_left))\n    dec int guess = read_int(\"enter your guess: \")\n    attempts_left -= 1\n\n    if (guess < secret) {\n        println(\"too low!\")\n    } else if (guess > secret) {\n        println(\"too high!\")\n    } else {\n        println(\"correct! you got it!\")\n        break\n    }\n}\n\nif (attempts_left == 0) {\n    println(concat(\"out of attempts! the number was \", secret))\n}",
41            ],
42            expected_output: &[],
43        },
44    ],
45    pitfalls: &[],
46    related: &[],
47    related_stdlib: &[],
48    since: None,
49};