Open In App

Difference between for loop in C and Python

Last Updated : 26 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

For loops, in general, are used for sequential traversal. It falls under the category of definite iteration. Definite iterations mean the number of repetitions is specified explicitly in advance.  However, there is a difference in working of c and python for loop, though both are used for iterations the working is different.

For Loop in C

For loop in C starts from the initialized value, and then it is checked for condition. If the test expression turns out to be true the code under for is executed, and it is done without any abnormality the update expression equation increments or decrements the variable accordingly. 

Syntax:

for (initialization expr; test expr; update expr)

{    

    // body of the loop

    // statements we want to execute

}

Example 1:

for(int i=1;i<=10;i++)

{

  printf(“%d “,i);

}

OUTPUT

1 2 3 4 5 6 7 8 9 10

It stops executing the body of for loop whenever i becomes greater than 10.

Example 2:

for(int i=1;i<=10;i++)

{

i=i+1

printf(“%d “,i);

}

OUTPUT:

2 4 6 10

//because each time i is getting incremented by 1 in for body and whenever i>10 it stops the execution.

Program

C




//Program to show working of for loop in C
 
#include <stdio.h>
 
int main()
{
 
    // code
    for (int i = 1; i <= 10; i++)
    {
      // it will have effect on termination
      // condition
      i=i+1;
      printf("%d\n", i);
    }
    return 0;
}


Output:

2

4

6

8

10

For Loop in Python

For loops in python works on the range given to it. The variable name given along with its declaration is changes its values starting from the initial value then being increment or decremented accordingly. The loop works for one less than the ending value.

Syntax

for val in range(x,y,z)

Where, 

x= initial value (Default initial value is 0)

y=final value

z=increment/ decrement (Default increment value is 1)

Example 1:

for i in range(1,11):

  print(i)

OUTPUT:

1 2 3 4 5 6 7 8 9 10

//as we can see it is iterating for 10 times ie(11-1)=10 times

Example 2:

for i in range(1,11):

   i=1+1;

   print(i)

OUTPUT:

2 3 4 5 6 7 8 9 10 11

//though we are incrementing i inside the for body still it is iterating  for 10 times.

Program

Python3




# program to show for loop in python
 
for i in range(1, 11):
   
    # it will have no effect in termination
    # condition
    i = i+1
     
    # it will always iterate for 10 times as
    # given in range
    print(i)


Output

2

3

4

5

6

7

8

9

10

11

Difference between c and python for loop

C Python
The for loop in C executes a statement or a block of statements repeatedly until a specified expression evaluates to false The foreach loop in Python repeats a group of embedded statements for each element in an array or an object collection. You do not need to specify the loop bounds minimum or maximum.
Evaluation of expression decides execution No such evaluation required
Increment or decrement operation has to be specified explicitly If not given it is assumed to be +1
It is a repetition control structure. For loop in python is used to iterate over either a list, a tuple, a dictionary, a set, or a string
It allows us to efficiently write a loop that needs to execute a specific number of times. For loop is initialized using for keyword.
For loop in C is a entry-cotrolled loop The for loop does not require an indexing variable to set beforehand.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads