6.1 For-Loops



In programming, you will often need to do the same task a number of times. For example, if you want to print “Hello World!” to the screen 100 times, you could use printf(“Hello World!”); and paste that same statement 100 times. It does the job, but it is very inefficient. The more lines you take up in your program, the more memory is consumed in your computer. So, there has to be an easier and more efficient way to complete this task. In fact, there is a structure called a for loop that allows you to complete a task a certain number of times. Let’s look at the syntax of a for loop:
for (int i = 0; i < 100; i++) //for loop header { printf(“Hello World!\n”); }
In the above example, the keyword for indicates to the computer that you are using a for loop. After this keyword, you should have a set of parentheses in which you should set a starting point for a variable, give a condition for the loop to check pertaining to the variable, and increment or decrement the variable. You can only use a for loop if you know exactly how many times you need to do something. In most cases, the variable declared in the for loop header is a variable of type int named i. That should be set to a certain number, based on the intended use of the for loop. Usually, the first value is set to 0 to start off with. In the example above, that would be the int i = 0; statement. Then, there is a condition that the computer checks each time it iterates through the loop. In the example above, that would be the i < 100; statement. After this, you need to change the value of your variable following each iteration. You can choose to either increase or decrease it based on what you are doing in the for loop. In our example above, that would be that i++ statement. Take note of the fact that there are semicolons after both the initialization of the iteration variable and the conditional statement pertaining to that variable. After your parentheses, you should have a set of curly braces and inside these curly braces is where your code for the for loop should go. In the example above, this is where we placed the print statement.
That was a lot of information and it might have been a little confusing. Let’s go through what the computer will understand when it reads our code. First, it creates a variable named i of type int and sets it equal to 0. Next, the computer checks to see if i is less than 100. Since it is, the computer executes everything that is in the curly braces, which means it will print “Hello World!” After this, the computer will increase the value of i by 1 because of the increment statement in the for loop header. That was one complete iteration of the for loop. After this, the computer will go back to the for loop and check to see if the value of i is less than 100. Since the value of i is 1 and 1 is less than 100, the computer will execute whatever is in the curly braces. Then, it will increase the value of i by 1, which means i now equals 2. This will continue until i is not less than 100. So, when the value of i is incremented to 100, the computer will check to see if i is less than 100. Since 100 is not less than 100, the code within the for loop will not execute. This is how the computer knows not to keep iterating through the loop.
If the conditional in the for loop header is not set properly, the computer might run the for loop forever, therefore causing the computer to crash. For example, let’s say your for loop header looks like this:
for (int i = 0; i < 1; i-
This for loop would run forever because the value of i keeps decreasing with every iteration and the computer is checking to see if the value of i is less than 1. Since i keeps decreasing, the value of i is ALWAYS going to be less than 1. Thus, the for loop would run infinitely with no end. As a programmer, you do not want to have an infinite loop.

Next Section

6.2 While-Loops
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.