Hey @tylvaughan! Welcome to the code crew forums!
You can format your code by wrapping it with three backticks:
```
code here
```
And with that, lemme format the code for you and give you some explanation on how it works, and how I would visualize/understand it.
var drawPixel: String = "x"
var height: Int = 5
var tempRow: String = ""
for columnPixel in 1...height {
tempRow = ""
for _ in 1...columnPixel {
tempRow += drawPixel
}
print(tempRow)
}
This code will print an output:
x
xx
xxx
xxxx
xxxxx
Understanding Loops
When I was learning loops, what worked for me is tracing the code and writing the output into a piece of paper. I did this so I would clearly understand what happens each step of the loop.
If you find nested loops difficult to trace, you can take a step back and watch what happens in a single, non-nested loop.
Single non-nested loop
Now, let’s rewrite the code without the nested loop:
height = 3
for columnPixel in 1...height {
tempRow = "x"
print(tempRow)
}
Let’s break this code down so we can have a better understanding on how the loop works.
for columnPixel in 1...height
means you start the loop with columnPixel = 1
and the loop would continue until columnPixel = height
. If we assume that height = 3
, then the loop would work 3 times
.
The three dots ...
is a range operator, so 1...height
can be also read as 1 to 3
, if height = 3
.
columnPixel = 1 (prints an x into the console)
columnPixel = 2 (prints an x into the console)
columnPixel = 3 (prints an x into the console)
The code above would show an output:
x
x
x
In the code with the nested loop, the outer loop for columnPixel in 1...height
is the loop that is responsible for printing rows.
The inner loop for _ in 1...columnPixel
is the loop that is responsible for printing columns.
Nested Loops
Going back to the nested loop above,
height = 3
for columnPixel in 1...height {
tempRow = ""
for _ in 1...columnPixel {
tempRow += drawPixel
}
print(tempRow)
}
We can trace this code every step of the way.
Let’s start with the outer loop: for columnPixel in 1...height
. If height = 3
, then this loop will run 3 times.
Initially, columnPixel = 1
, then it will increment (increase in value) until columnPixel = 3
.
columnPixel = 1
columnPixel = 2
columnPixel = 3
For each iteration of the outer loop with columnPixel
, we’ll trace the code inside the loop and check what happens each step of the way:
Let’s start with columnPixel = 1
columnPixel = 1
tempRow = ""
for _ in 1...columnPixel {
tempRow += drawPixel
}
print(tempRow)
The inner loop can be rewritten as: for _ in 1...columnPixel
, since columnPixel = 1
, we get: for _ in 1...1
, which is a loop that starts with 1, and ends with 1. The underscore _
means you won’t be assigning a variable name to the loop counter, and you’ll just go ahead with the loop count. Since the loop starts with 1 and ends with 1, the loop will only run a single time. If this sounds confusing, it’s alright, you can send a reply so we can simplify it further.
The code inside the loop will run a single time so that means: tempRow += drawPixel
will run a single time.
+=
is a shorthand syntax for tempRow = tempRow + drawPixel
, and since both are String
types, you get tempRow = "" + "x"
, thereby tempRow = "x"
The succeeding print statement should show a single x
in the console.
Output so far:
x
columnPixel = 2
tempRow = ""
for _ in 1...columnPixel {
tempRow += drawPixel
}
print(tempRow)
The inner loop can be rewritten as: for _ in 1...2
since columnPixel = 2
at this state of the loop.
This would mean that the inner loop would execute 2 times
and would thereby execute tempRow += drawPixel
twice.
In the first run, tempRow = ""
is currently an empty string, and adding the drawPixel = "x"
to it would simply concatenate a single "x"
.
On the second run, tempRow = "x"
, and adding another drawPixel = "x"
would make it tempRow = "xx"
. That’s what tempRow += drawPixel
means, it simply appends the drawPixel
string into it.
So the output at this point of the loop would be:
x
xx
columnPixel = 3
tempRow = ""
for _ in 1...columnPixel {
tempRow += drawPixel
}
print(tempRow)
We are now at the final outer loop (since height = 3
).
We can rewrite the inner loop as: for _ in 1...3
, since columnPixel = 3
. This would mean that the inner loop would execute 3 times
, and would append drawPixel = "x"
into tempRow
3 times, making it:
tempRow = "xxx"
.
The output to the console would be as follows:
x
xx
xxx
I hope that I have somehow made it clear? If not you can go ahead and ask which part made you lost, and I’ll try to explain it the best I can.