Print 1–100 without a loop
Learn how to loop without a loop
Question:
Print all the natural numbers up to the given number
n
, without using a loop.
Overview:
Printing 1
to a given number sounds fairly simple. You loop from 1
to the number using a for loop or any other type of loop and print the number on every iteration.
Printing without a loop is a problem. Even if we could store every number during their iteration in a data structure, we would still have to loop through the data structure to access every element.
Therefore, we are going to make use of the call stack and use recursion as a way to loop through each number.
Call Stack:
A stack is a linear data structure, where you can insert or delete data from only one end. The Last item In the stack is the First item to go Out.
The call stack is a data structure that keeps track of where we are in the program.
The JavaScript runtime has a single stack, so only one function can be run at a time.
Functions that are added to the stack are only deleted when it is complete.
Recursive Function:
A recursive function is a function that calls itself until a base case is fulfilled.
When a recursive function calls itself, it creates another version of the function to be executed before itself. When that happens, the first version has to wait to be processed until the latter version is completed.
In the call stack, the first function gets added first. When it calls itself, the latter version gets added on top of the first.
When a function completes, it is popped from the top of the stack so the runtime can execute the next function in the stack.
Recursive functions should be designed to keep calling itself until a particular condition is satisfied. That means, it should keep creating different versions of itself until it has found the one that satisfies the base case.
In our program, we are going to start with the given number n
. Then the function will keep calling itself with n-1
until it finds 1
. That means we will keep adding functions to the stack frame from n
to 1
until we reach 1
.
function print(n) {
print(n - 1)
}
Base Case:
The base case is a condition that will end the loop when it is satisfied.
If there is no base case, a recursive function like the one above will continue to call itself until the call stack cannot handle more functions and throws an error. This is also comparable to an infinite loop.
In our program, we need to print 1
through n
, therefore our base case can be satisfied when n < 1
.
When the base case is satisfied, we need to stop the function. We can stop a function by adding a simple return
statement.
function print(n){
if(n < 1) return
print(n - 1)
}
When print(1)
is executed, it fulfills the base case and completes the function. Then we are back to line 3 on print(1)
, where the program is going to move to line 4.
Solution:
Our main goal is to print all these numbers, so we are going to use the console.log()
statement to print the current number n
.
function print(n){
if(n < 1) return
print(n - 1)
console.log(n)
}
After the console.log()
statement, the function completes and the program is going to access the next function in the call stack with n = 2
. The program will then continue to print the number in the console until we reach the number we started with.
In other words, the call stack keeps popping the function on top, until it reaches the very first function.
All we need to do now is call the function with any number.
print(100)
In a nutshell:
A call stack is a data structure that keeps track of where we are in the program.
A stack is a linear data structure that first executes the last function added to the list.
We can use a recursive function to make use of the call stack instead of adding a loop or declaring a data structure in our program.