mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-22 14:03:10 -05:00
exercise for new for-loops
This commit is contained in:
parent
582f3b1b09
commit
101151f3f1
4 changed files with 75 additions and 3 deletions
|
@ -42,7 +42,7 @@ Verify the installation and build number of `zig` like so:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ zig version
|
$ zig version
|
||||||
0.11.0-dev.1711+xxxxxxxxx
|
0.11.0-dev.1844+xxxxxxxxx
|
||||||
```
|
```
|
||||||
|
|
||||||
Clone this repository with Git:
|
Clone this repository with Git:
|
||||||
|
@ -82,7 +82,7 @@ about input:
|
||||||
|
|
||||||
### Version Changes
|
### Version Changes
|
||||||
|
|
||||||
Version-0.11.0-dev.1711+xxxxxxxxx
|
Version-0.11.0-dev.1844+xxxxxxxxx
|
||||||
* *2023-02-21* zig 0.11.0-dev.1711 - changes in `for loops` - new: Multi-Object For-Loops + Struct-of-Arrays
|
* *2023-02-21* zig 0.11.0-dev.1711 - changes in `for loops` - new: Multi-Object For-Loops + Struct-of-Arrays
|
||||||
* *2023-02-12* zig 0.11.0-dev.1638 - changes in `std.Build` cache_root now returns a directory struct
|
* *2023-02-12* zig 0.11.0-dev.1638 - changes in `std.Build` cache_root now returns a directory struct
|
||||||
* *2023-02-04* zig 0.11.0-dev.1568 - changes in `std.Build` (combine `std.build` and `std.build.Builder` into `std.Build`)
|
* *2023-02-04* zig 0.11.0-dev.1568 - changes in `std.Build` (combine `std.build` and `std.build.Builder` into `std.Build`)
|
||||||
|
|
|
@ -8,7 +8,7 @@ const print = std.debug.print;
|
||||||
// When changing this version, be sure to also update README.md in two places:
|
// When changing this version, be sure to also update README.md in two places:
|
||||||
// 1) Getting Started
|
// 1) Getting Started
|
||||||
// 2) Version Changes
|
// 2) Version Changes
|
||||||
const needed_version = std.SemanticVersion.parse("0.11.0-dev.1711") catch unreachable;
|
const needed_version = std.SemanticVersion.parse("0.11.0-dev.1844") catch unreachable;
|
||||||
|
|
||||||
const Exercise = struct {
|
const Exercise = struct {
|
||||||
/// main_file must have the format key_name.zig.
|
/// main_file must have the format key_name.zig.
|
||||||
|
@ -479,6 +479,10 @@ const exercises = [_]Exercise{
|
||||||
.output = "The normalized angle of 765.2 degrees is 45.2 degrees.",
|
.output = "The normalized angle of 765.2 degrees is 45.2 degrees.",
|
||||||
.C = true,
|
.C = true,
|
||||||
},
|
},
|
||||||
|
.{
|
||||||
|
.main_file = "095_for_loops.zig",
|
||||||
|
.output = "1 2 4 7 8 11 13 14 16 17 19",
|
||||||
|
},
|
||||||
.{
|
.{
|
||||||
.main_file = "999_the_end.zig",
|
.main_file = "999_the_end.zig",
|
||||||
.output = "\nThis is the end for now!\nWe hope you had fun and were able to learn a lot, so visit us again when the next exercises are available.",
|
.output = "\nThis is the end for now!\nWe hope you had fun and were able to learn a lot, so visit us again when the next exercises are available.",
|
||||||
|
|
64
exercises/095_for_loops.zig
Normal file
64
exercises/095_for_loops.zig
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
//
|
||||||
|
// The Zig language is in rapid development and continuously improves
|
||||||
|
// the language constructs steadily.
|
||||||
|
//
|
||||||
|
// Since version 0.11, the "for-loops" widely used in other languages
|
||||||
|
// such as C, e.g. "for (int i = 0; i < 10..." can now also be formed
|
||||||
|
// similarly in Zig, which previously required a "while" construct.
|
||||||
|
// Similar in this case actually means better, just as Zig generally
|
||||||
|
// tries to make everything simple and "better".
|
||||||
|
//
|
||||||
|
// These new "for-loops" look like the following in Zig:
|
||||||
|
//
|
||||||
|
// for (0..10) |idx| {
|
||||||
|
// // In this case 'idx' takes all values from 0 to 9.
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// This is really simple and can replace the previous, somewhat bulky:
|
||||||
|
//
|
||||||
|
// var idx: usize = 0;
|
||||||
|
// while (idx < 10) : (idx += 1) {
|
||||||
|
// // Again, idx takes all values from 0 to 9.
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// This would also simplify exercise 13, for example.
|
||||||
|
// The best way to try this out is to use this exercise, which in the
|
||||||
|
// original looks like this:
|
||||||
|
//
|
||||||
|
// ...
|
||||||
|
// var n: u32 = 1;
|
||||||
|
//
|
||||||
|
// // I want to print every number between 1 and 20 that is NOT
|
||||||
|
// // divisible by 3 or 5.
|
||||||
|
// while (n <= 20) : (n += 1) {
|
||||||
|
// // The '%' symbol is the "modulo" operator and it
|
||||||
|
// // returns the remainder after division.
|
||||||
|
// if (n % 3 == 0) continue;
|
||||||
|
// if (n % 5 == 0) continue;
|
||||||
|
// std.debug.print("{} ", .{n});
|
||||||
|
// }
|
||||||
|
// ...
|
||||||
|
//
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
// And now with the new "for-loop".
|
||||||
|
pub fn main() void {
|
||||||
|
|
||||||
|
// I want to print every number between 1 and 20 that is NOT
|
||||||
|
// divisible by 3 or 5.
|
||||||
|
for (???) |n| {
|
||||||
|
|
||||||
|
// The '%' symbol is the "modulo" operator and it
|
||||||
|
// returns the remainder after division.
|
||||||
|
if (n % 3 == 0) continue;
|
||||||
|
if (n % 5 == 0) continue;
|
||||||
|
std.debug.print("{} ", .{n});
|
||||||
|
}
|
||||||
|
|
||||||
|
std.debug.print("\n", .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is actually a little easier. The interesting thing here is that the other
|
||||||
|
// previous 'while' exercises (11,12, 14) cannot be simplified by this
|
||||||
|
// new "for-loop". Therefore it is good to be able to use both variations
|
||||||
|
// accordingly.
|
4
patches/patches/095_for_loops.patch
Normal file
4
patches/patches/095_for_loops.patch
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
49c49
|
||||||
|
< for (???) |n| {
|
||||||
|
---
|
||||||
|
> for (1..21) |n| {
|
Loading…
Reference in a new issue