Skip to content
Related Articles

Related Articles

Improve Article
Difference between for loop in C and Python
  • Last Updated : 31 May, 2021

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

y=final value

z=increment/ decrement

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

CPython
The for loop in C executes a statement or a block of statements repeatedly until a specified expression evaluates to falseThe 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 executionNo such evaluation required
Increment or decrement operation has to be specified explicitlyIf not given it is assumed to be +1

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :