Let's solve Counting to Infinity.
The problem statement asks us to output the numbers from 1 to n
for some integer n
. As with the preceding problem, we will require some kind of loop to achieve our goal, but one could argue that this is easier than the preceding problem. I'll look at two equivalent solutions to this problem, each of which uses a different kind of loop.
while
loop
The while
loop is the most basic kind of loop: it readily translates to plain English, and it closely reflects how the computer actually interprets our code. As I discussed previously, a while
loop requires a condition to check for, and a set of statements to execute while the condition is true.
It seems clear enough that our program should keep track of the number it's up to. I'll use a variable called upto
for this, though as always you should name your own variable whatever is most meaningful to you (one-letter names like i
are popular for variables used to count from one number to another).
What value should we give to upto
to begin with? There is no one right answer, but starting off at 1 seems sensible. That is, after all, where we're counting from.
What needs to happen inside the loop? To answer this, we must ask ourselves what actions we need to repeat multiple times. After some consideration, we might decide we need to:
upto
(because we need to print the numbers as we count)
upto
(because we need a different value each time)
What is the condition we need to check? Well, if left unchecked, upto
will simply continue growing until it is too big for the computer to store. To make sure we only print the first n
integers, our condition should be true when upto
is one of the numbers {1
, 2
, 3
, ..., n-1
, n
}, and false when upto
grows any bigger.
A natural condition satisfying the above is (upto <= N)
. Notice that we don't have to worry about upto
being too small, because we set it to 1 and only ever let it grow.
Putting this all together, we get:
upto = 1
while upto <= n:
outputFile.write(str(upto) + "\n")
upto += 1
upto = 1;
while (upto <= N) {
fprintf(outputFile,"%d\n",upto);
upto++;
}
The first line of code initialises upto
to 1.
Then, while upto <= n
the computer prints out the value of upto
and increments it.
This should print out all the integers from 1 to n
.
(An aside: when the program leaves the loop, the value of upto
is in fact n+1
.
This is because it is incremented after the very last printf
/write
, only after which the condition is checked and found to be false.)
Before we go on, I should stress something: the above code is correct. It makes perfect sense, and will score you 100% for Counting to Infinity. Any coding problem that requires a loop can be solved using a while
loop, and there are many cases in which this is the best approach.
for
loop
Many programming languages offer you a variety of other loops you can use in your code.
I will begin by discussing the for
loop as it appears in languages like C++. Scroll down for a description of for
loops in Python. If you are using a different language you should consult a reference to see what kinds of loop it supports. Most languages will at least provide you with a convenient way to count through a list of numbers.
for
loops in C++
This particular kind of loop is useful when a program is going through a sequence of data. In this problem, we are running through the numbers from 1 to n
. In each iteration of the while
loop (each time it is run) our program does two things: it does something with the current number - specifically, it prints it out - and then it increments the counter variable in preparation for the next iteration.
Now, printing out the number is something very specific to the problem. There aren't that many contexts in which we'd want to print out the numbers 1 to n
.
However, the rest of the code to do with the loop (the initialisation, the condition, and the update) is quite general, and could easily be used for other purposes. A program that prints out the first n
squares or a program that modifies a list of numbers will need to use the same kind of code to iterate through some range.
We can use for
loops to separate these elements of our code. At the start of the loop we put all the code that deals with the counting. Inside the loop we put all the code that actually uses the counter. Here's a direct translation of the previous code:
In general, the format is "for (initialisation; condition; update)
". The computer executes the initialisation code, then, while the condition is true, executes the statements inside the loop followed by the update.
Whether this is any more readable than the while
loop version is a matter of opinion. You may find that after a while spent reading and writing for
loops, you are able to quickly interpret the start of the loop as "use upto
to count between 1 and N". The added benefit is that there is no confusing the 'counting' code and the 'processing' code, as they are clearly separated.
for (upto = 1; upto <= N; upto++) {
fprintf(outputFile,"%d",upto);
}
for
loops in Python
for
loops in C++ are about repeatedly applying an update until some termination condition is reached.
Python as a language feels that this is what a while
should be used for, and so for
loops in Python
perform a related, but distinct role.
In Python, for
loops take the form: for upto in list
.
Python will then execute the statements inside your loop once for each item in list
, using upto
to store
that item during each iteration (don't worry if you haven't gotten to learn about lists yet. If you are following an external Python tutorial, the topic will come up eventually).
For example, this code:
for x in [1, 4, 1, 5, 9]:
print(x)
}
Predictably prints:
1 4 1 5 9
To write a for loop that iterates from 1 to n
, you would create a list of numbers [1, 2, 3, ..., n-1, n]
,
which you can do as follows:
# range(x, y) creates a list of numbers that starts from x and finishes
# at y, not including b
#
# So range(1, n+1) generates [1, 2, 3, ..., n-1, n]
for upto in range(1, n+1):
outputFile.write(str(upto) + "\n")
In conclusion, both solutions (using a for
loop or using a while
loop) are valid solutions.
There are some situations where a for
loop makes more sense and some situations where a while
loop makes more sense.
Spoilers. Only look if you're really stuck!
# Read the input
inputFile = open("countin.txt", "r")
n = int(inputFile.readline().strip())
# Write the output
outputFile = open("countout.txt", "w")
for i in range(1, n+1):
outputFile.write(str(i) + "\n")
# Clean up!
inputFile.close()
outputFile.close()
#include <cstdio>
int main() {
/*Declare variables*/
FILE* inputFile;
FILE* outputFile;
int n, i;
/*Read the input*/
inputFile = fopen("countin.txt", "r");
fscanf(inputFile, "%d", &n);
/*Write the output*/
outputFile = fopen("countout.txt", "w");
for (i = 1; i <= n; i++) {
fprintf(outputFile, "%d\n", i);
}
/*Clean up!*/
fclose(inputFile);
fclose(outputFile);
}