Nested For Loops | Development | Online Course With Appy Pie Academy

Nested for loops

Lesson Details:
June 29, 2020


I: Introduction

A: Computer Programs

B: Programs are written in programming language. Different programming languages have different syntax and functionality.

C: Programming Languages are categorized based on their use. There are four categories

1. Low-Level Languages

a) Assembly Language

b) Machine Language

2. High-Level Languages

a) Scripting Languages

b) Interpreted languages

3. Special-Purpose Languages

a) Domain Specific Languages

4. General-Purpose Languages

a) Object Oriented Programming Languages

b) Functional Programming Languages

II: Body

A: Nested for loops. A nested loop is a loop within a loop. The inner loop will run several times while the outer loop will run once for each iteration of the inner loop. Using nested loops we can reduce the amount of code that we need to write to perform a task. Let us take an example, suppose we need to add up all the numbers from 1 to 100 we can do it in two ways. First we can iterate through the numbers using a for loop and add one by one using a second for loop inside it. This will be done as follows:

for i in range(1, 101): for j in range(1, 101): total = total + i * j print("Total = ", total)

In this case we used two loops to add up all the numbers from 1 to 100. However, we can also reduce the amount of code by using a nested for loop that will do the same thing as above, but in a more compact form. We can do it like this:

loader
Course content