Architecture : Conditionals and Loops — The For and Do-While Loops

We’ll jump into the for and do-while loops in just a second, but I wanted to give you a list of some other resources I use to, quite frankly, keep up my motivation and excitement about learning to code (especially when I’m feeling like even having the thought that I could learn to code was a really, really, stupid idea).

There are a couple of podcasts that I listen to about coding in particular:

  • CodeNewbie — this is a podcast hosted by the founder of CodeNewbie, Saron Yitbarek. She talks to people about how they learned to code, and what they’re doing now with their newfound coding skills.
  • BaseCS — this podcast is also hosted by Saron, along with Vaidehi Joshi, who is the founder of BaseCS. They go through the basics of computer science, right from the beginning (the title of the first episode was Bits, Binary, and Bytes).

While listening to a recent episode of the CodeNewbie podcast, they mentioned their Codeland Conference. So I looked that up and saw some videos from it on YouTube, including one by someone else I follow (on YouTube) named Dan Shiffman, whose channel is called The Learning Train. I love how he teaches because, to use a Jewish word, he such a mensch. He teaches everything in such a friendly, conversational and (my particular favorite) self-deprecating way.

  • Facebook
  • Twitter
  • Google+
  • Pinterest

Another video from the Codeland Conference was the keynote by Scott Hanselman who is a Principal Program Manager at Microsoft, and hosts a podcast I listen to called HanselMinutes. I’m in the middle of Dan Shiffman’s talk, and will listen to Scott’s after that.

The video I’ll watch after that (also from the Codeland Conference) is by Quincy Larson, who is the founder of freeCodeCamp. It’s a community of people, all learning to code, who collaborate on projects for not-for-profits.

  • Facebook
  • Twitter
  • Google+
  • Pinterest

I wanted to mention those because this was another way I dealt with my (incorrect) thought that just using a single tool or resource would teach me everything I needed to know. Needless to say, Google has also been my number one tool for EVERYTHING!

The “for” Loop

Anyway, the next section from the SoloLearn app, within the Conditionals and Loops lesson, was called the for loop. As I mentioned at the end of the previous post, the for loop executes a set of statements a specific number of times. It’s written like this:

for (init; condition; increment;) {

The expressions you see there, between the parentheses, after the keyword for, are important to understand:

  • init — as mentioned, this loop will run a certain number of times, but interestingly, the incrementation to whatever that number is, doesn’t have to start at zero or one. The code put here is used to create a new variable, set it’s data type and then give it a value. For example, it might be something like this: int x = 10.
  • condition — this is the test that gets evaluated to see whether or not the condition is true (and causing the internal code to run) or false (and causing the internal code not to run). For example, it could look like this: x < 15.
  • increment — after the internal code is run (should the condition be evaluated as true), the value of the variable established with the init, will get incremented based on what is written here. For example, it could be: x++.

Overall, the code would look like this:

for (int x = 10; x < 15; x++) {

When this code is run, it’ll print 10, 11, 12, 13 and 14 to the screen. It starts with the number 10, because x is started (or initialized) at 10. Each time around, the code prints the next consecutive number because x++ increments x’s value by 1 each time it goes through the loop.

Finally, 14 is the last number printed to the screen (as opposed to 15) because the condition that is checked for is whether x is less than 15. Once this isn’t true anymore (i.e. 15 < 15, which it’s not), the loop stops.

Decrementing (as opposed to “Incrementing”)

Next, they went through an additional compound arithmetic operator that can be used to control loop iterations. In part 4b, we talked about how you can iterate the value of a variable by 1, or 2, or 3, etc., by using num++ (for iterating by 1) or num+= 2 (for iterating by 2). As it turns out, you can also decrement variable values, as in the following:

for (int x = 10; x > 0; x-=2) {

When run, this code would print 10, 8, 6, 4 and 2 to the screen, each on it’s own line. Here’s why:

  • First, a variable called x is created, has its data type set to be an integer, and has its value set to 10.
  • Next, the condition of whether or not the value of x is greater than 0, is checked (and since, right now, x is equal to 10, the code inside the for loop will run).
  • After that, the value of the x variable (at this point, 10) is reduced (or decremented) by 2 (now equaling 8). This is because x -= 2 translates into x = x–2, or x = 10–2, or x = 8.

The loop then starts again, using the new value of the variable x, which is 8. It tests the condition, prints it to the screen, and then decrements it to 6, and then 4, and then 2. When it decrements it again by 2, x is now equal to 0 and, since 0 is not greater than 0, the code stops.

MORE Efficiencies

Finally, the tutorial goes through some efficiencies, showing that the statements for both init and increment can actually be left out where appropriate (although they’re specific to say that the semicolons are still required). The creation of the variable, setting of it’s data type, and value, can be established somewhere else in the code. This I can understand, because, the value of the variable might be as a result of some other code before it reaches this code. Here’s what that would look like:

int x = 10;
for ( ; x > 0; x -= 3) {

However, I’m not sure what the benefit is of putting the increment within the code block, versus putting it within the parentheses after the for keyword. In other words, what’s the difference (or benefit) between the code block above, and the following code block:

int x = 10;
for ( ; x > 0 ; ) {
     x-= 3;
Hopefully someone can answer this for me?

While I did do some research, I was reminded of a shortcut that I had learned earlier in my training, which is worth mentioning here. When writing this code in a tool like Microsoft Visual Studio, after entering the for keyword, you can tap the tab key twice, and it will create the parentheses and placeholder expressions for you:

It uses a sample variable name of i, and highlights the first expression (the declaration of the variable itself), so you can change the value. After entering it, tap the tab key again to go to the next expression (which is the value to compare against for the condition).

The “do-while” Loop

Next, let’s talk about the do-while loop. The only difference between this kind of loop, and the while loop, is that the do-while is guaranteed to run at least once. In other words, with a while loop, it’s possible that the condition being evaluated may not be met, causing the code within the loop to never run!

That won’t happen with a do-while loop, which is written like this:

int a = 0;
do {
} while(a < 5);

Translated into English, this code means:

  • Create a variable called a, set it’s data type to be an integer, and then assign it a value of 0.
  • Next, do the following: print the value of the variable a to the screen, and then increment the value of a by 1 (which means the value of a is now 2).
  • Finally, evaluate whether or not a is less than 5 and, while this is true, run the code within the loop (and, conversely, when it’s not true, break out of the loop).

When this code is run, it prints 0 to the screen, incrementing each time, and then checking if the value of a is less than 5 and, as a result, printing 1, 2, 3 and 4 to the screen. Once the value is incremented to 5 and, since 5 is not less than 5, the loop is broken out of.

The End…

So, if anyone can answer my question above, I’d appreciate it! And, as usual, please feel free to correct, or add to, anything that I’ve written.

Also, here’s a link to the previous post, Learning to Code — Part 4b: Conditionals and Loops — The Switch Statement & While Loop.


Please enter your comment!
Please enter your name here