Data update
This commit is contained in:
parent
ed705008a8
commit
0df55f9f24
2196 changed files with 32999 additions and 3075 deletions
40
Task/Loops-Foreach/Bait/loops-foreach.bait
Normal file
40
Task/Loops-Foreach/Bait/loops-foreach.bait
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
fun for_in_array() {
|
||||
arr := ['1st', '2nd', '3rd']
|
||||
|
||||
// Iterate over array indices and elements
|
||||
for i, val in arr {
|
||||
println('${i}: ${val}')
|
||||
}
|
||||
|
||||
// Using only one variable will iterate over the elements
|
||||
for val in arr {
|
||||
println(val)
|
||||
}
|
||||
|
||||
// To only iterate over the indices, use `_` as the second variable name.
|
||||
// `_` is a special variable that will ignore any assigned value
|
||||
for i, _ in arr {
|
||||
println(i)
|
||||
}
|
||||
}
|
||||
|
||||
fun for_in_map() {
|
||||
nato_abc := map{
|
||||
'a': 'Alpha'
|
||||
'b': 'Bravo'
|
||||
'c': 'Charlie'
|
||||
'd': 'Delta'
|
||||
}
|
||||
|
||||
// Iterate over map keys and values.
|
||||
// Note that, unlike arrays, only the two-variable variant is allowed
|
||||
for key, val in nato_abc {
|
||||
println('${key}: ${val}')
|
||||
}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
for_in_array()
|
||||
println('')
|
||||
for_in_map()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue