Skip to main content

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

1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2pub static STEP_FOR_LOOPS: ConceptEntry = ConceptEntry {
3    name: "7. for loops",
4    summary: "for loops",
5    category: ConceptCategory::ControlFlow,
6    prerequisites: &[],
7    descriptions: &[
8        DescriptionEntry {
9            kind: DescriptionKind::Explanation,
10            title: None,
11            description: "while loops are great when you do not know how many times you will loop. when you do know, a for loop is cleaner. the range form goes from a start number up to but not including the end",
12            examples: &["for i in 0..5 {\n    println(i)\n}\n// 0\n// 1\n// 2\n// 3\n// 4"],
13            expected_output: &[],
14        },
15        DescriptionEntry {
16            kind: DescriptionKind::Explanation,
17            title: None,
18            description: "the C-style for loop gives you full control: an initializer, a condition, and an increment, all in one line",
19            examples: &[
20                "for [int i = 1, i <= 5, i += 1] {\n    println(i)\n}\n// 1\n// 2\n// 3\n// 4\n// 5",
21            ],
22            expected_output: &[],
23        },
24        DescriptionEntry {
25            kind: DescriptionKind::Explanation,
26            title: None,
27            description: "you can also loop over the elements of an array directly without needing an index at all",
28            examples: &[
29                "dec arr[string] days = [\"sat\", \"sun\", \"mon\"]\n\nfor day in days {\n    println(day)\n}",
30            ],
31            expected_output: &[],
32        },
33        DescriptionEntry {
34            kind: DescriptionKind::Explanation,
35            title: None,
36            description: "exercise: before the game starts, print a row of dashes as a divider using a for loop. then after the game ends print a summary showing each attempt number\n\nexpected output:\n  ----------\n  welcome to the guessing game!\n  ----------",
37            examples: &[
38                "get print from std::io\n\n// print 10 dashes without a newline each\nfor i in 0..10 {\n    print(\"-\")\n}\nprintln(\"\") // move to next line\n\nprintln(\"welcome to the guessing game!\")\n\nfor i in 0..10 {\n    print(\"-\")\n}\nprintln(\"\")",
39            ],
40            expected_output: &[],
41        },
42    ],
43    pitfalls: &[],
44    related: &[],
45    related_stdlib: &[],
46    since: None,
47};