The easy way to learn python when you already know C programming language…

The different methods of writing a for loop in C

In method 1, method 2, method 3 the ways of declaring variable i and initiating the variable i to be used for the for loop is the same.The way of initiating means in laymen terms what value of i starts the for loop off. In my examples i starts off at i=1. The way of declaring means to say that a variable called i exists and it belongs to the data type int.

In method 4, i has been declared within the main loop outside of the for loop compared to method 1, method 2, method 3. Yet all method 1,method 2, method 3 and method 4 produces the same output.

Method 5 produces a different output as i equal to 10 instance is also taken into consideration.

Method 1 in c

#include <stdio.h>

Method 2 in c

#include <stdio.h>

Method 3 in c

#include <stdio.h>

In methods 1 to 3, I have represented different ways one can increment the integer i, that is for which instances of i you need to carry on executing the for statement. Yet the output remains the same for the for loops in other places of use along the program code lines can produce different results, which I will discuss on a later blog.

Method 4 in c

#include <stdio.h>

Method 5 in c

#include <stdio.h>

Output of Method 1, Method 2, Method 3, Method 4

1 2 3 4 5 6 7 8 9

Output of Method 5

1 2 3 4 5 6 7 8 9 10

The basic structure of a c for loop

for(initialization; condition; incremental/decrement){

The output of Method 5 has 10, while other methods don’t produce 10, the program continues to be in the for loop so as long as the condition mentioned is true.

Method 6 in c

Say we have a sentence and we want to iterate through the letters in the sentence and display the letters. Then in c, we store the sentence in an array, and iterate through all the different characters stored in different indexes of the array. Since I want each character to print on a separate line. I use escape sequence of a new line, “\n” .

The printf statement, y increments from 0 to 14 of the array , Hell and heaven.The 0th index of array stores letter H, the 14th index of array stores letter n. Using word[y], we use to iterate through the entire sentence.

printf("%c\n",word[y]);

The full code

#include <stdio.h>

The output

H
e
l
l

When we don’t use the newline escape sequence.Simply remove the “\n” .

Then printf statement is

printf("%c",word[y]);

The full code.

#include <stdio.h>

The output

Hell and heaven

Now let’s look at for loops in python.

Method 1 in python

Let’s say we have a sentence as “I love my garden”. The sentence is stored as a list. Then i use a for loop to iterate through the sentence. I stored the sentence in a list called word. To reference each character in the word list, I used letter, but there is no restrictions in python you can call that letter by any other name as row or whatever, so as long as both in for loop and within print statement, use the same name of reference.

word="I love my garden"

The output

I

Method 2 in python

The method 2 of using for loops in python. To print the word “laalalaa”, 4 times. I start with 1 and loops 4 times. In this range in python, one is inclusive, five is exclusive.

for i in range(1,5):
print("laalalaa")

Output

laalalaa
laalalaa
laalalaa
laalalaa

Yet if you want to display the word “laalalaa”, five times, you can start the loop with zero,(0). Method 2 part b:

for i in range(0,5):
print("laalalaa")

Output

laalalaa
laalalaa
laalalaa
laalalaa
laalalaa

Method 3 in python

Even this method works,Method 4:

for i in range(5):
print("laalalaa")

Output

laalalaa
laalalaa
laalalaa
laalalaa
laalalaa

As in Method 3 its even sufficient to state the end point. Then the start point by default considered is zero. The word “laalalaa” is displayed five times. Unlike in c, in python there is no need to give instructions to increment the loop, but if required can state the start point, end point and the numerical gap between increments as in method 4. As in method 2, and method 2 part b, its sufficient to state the inclusive start point and exclusive end point.

Method 4 in python

for i in range(0,50,5):
print(i)

Output

0
5
10
15
20
25
30
35
40
45

In method 4 the for loop start at 0, 0 is inclusive, then increments by 5, and stops at 50, 50 is exclusive. range(start point, end point, increment)

Thanks for reading. Have fun coding !!!

Student at University of Moratuwa

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store