Chapter 4 - Loops
What will you learn in this chapter
In this chapter you will learn about:
Loops are a way to make the code repeat itself a certain amount of times, of which the amount of repetitions may vary depending on what is going on in the code.
Arrays - short introduction
This subsection is supposed to teach you the very basics of arrays, because next you will learn about loops, and loops are mostly useful for array manipulation. You won't learn in details what the array is and how to do operations on it, but you will learn how to create one and access its elements.
An array is an ordered group of elements, of which there is a starting element and an element at the end.
Code-wise it's a template object, as you need to specify of which data type its values will be. To create an array you can use the {element, element2, ...} assignment:
Since array and dictionary are reference types, you initialize them as an object handles (the @ symbol). For now, just remember that you add that symbol after the type name, it will get explained later on.
To access elements in an array you use the index operator [], where [i] will access the i'th element of the array (counting from 0):
More on the arrays will be talked about in later parts of this guide.
While, do while
The while (expression) loop will execute code until the expression evaluates to false:
The while loop will not execute the code (even once) if the condition evaluates to false from the very start.
A do while loop however, will first execute the code inside the loop and only then check if the condition is true to execute it once more:
It is possible to create a never ending loop, the easiest way is to do while (true). Such loops are most of times an error in programming. However, these loops can be an intentional decision when you are sure the loop will at some time stop executing, either with the break statement (more on that in a bit) or other ways.
TASK 1:
Write a program that will print out "Hello again!" to the console 10 times.
For
A for loop is an advanced version of the while loop. It takes 3 arguments in its statement - for (statement1; statement2; statement3) {...}:
Statement 1 - gets executed before the loop runs (but in the loop's variable scope), often used to intialiaze a local indexing variable, such as
int i = 0;.Statement 2 - is a condition for the execution of the loop, checked before the loop executes the code inside.
Statement 3 - is executed after a successful code execution inside the loop (every time).
An example of a for loop can be a loop that cycles through every character in a string:
Statements 1 and 3 in for loops can be skipped, as an example for (;condition;) is a valid form of a for loop, and so is for (int a = 0;condition;) etc.
TASK 2:
Given an array of integers, write a program that will add all of these integers and print out the result.
Because of the Variable Scope , you will need to define a variable to store the sum outside of the loop.
Foreach
A foreach loop can be used to perform code for each item in a container objects. Container objects are objects that store values of other data types, such as the array<T> object or the dictionary object. Its structure foreach (vars : container_object) consists of two parts: where vars contains declarations of the variable names, such as int val, and the container object is the, well, container object. Some objects unpack more than one variable, such as the dictionary objects that unpacks the key and the appropriate value.
TASK 3:
Write a program that will print out every element of a given array.
Special keywords
These are the special keywords that can be used inside loops.
Break statement
The break statement is a way to exit a loop execution early. Calling it will cause the program to abort loop execution and continue executing code after the loop.
Continue statement
The continue statement will cause the loop to stop and go to the next element.
Nested loops and special keywords
Keywords like break and continue work on the bottom-most loop in nested code, meaning that if you have code like this:
TASK 4:
Create a while (true) loop that adds all integers until 20.