FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
717 views
in RTU/BTU B.Tech(CSE-I & II Sem) Computer Programming Lab by Goeduhub's Expert (7.6k points)

Programs to understand for loops for iterative statements.

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

Programs to understand for loops for iterative statements.

Loops : Loops executes a block of statements number of times until the condition becomes false.

For Loops : A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax : 

for (initialization; condition ; increment / decrement)
{
       //Statements to execute
}

  • Initialization : In this expression we have to initialize the loop counter to some value. for example: int i=1;
  • Condition: In this expression we have to test the condition. If the condition evaluates to true then we will execute the body of loop and go to update expression otherwise we will exit from the for loop. For example: i <= 10;
  • Increment / Decrement: After executing loop body this expression increments/decrements the loop variable by some value. for example: i++ or i--;

 

Example-1 : 

#include<stdio.h>

#include<conio.h>

void main()

{

    int i;

   for (i=1; i<=5; i++)

   {

       printf("%d\n", i);

   }

}

Output : 

1

2

3

4

5

Example-2 (Nested For Loop) : 

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j;

    for (i=0; i<2; i++)

   {

for (j=0; j<4; j++)

{

   printf("%d, %d\n",i,j);

}

   }

}

Output : 

0, 0

0, 1

0, 2

0, 3

1, 0

1, 1

1, 2

1, 3

Example-3 (Infinite for loop) :

for(;;)  

    {  

        printf("welcome to GoEduHub Technologies");  

    }  

Example-4 Multiple Initialisation inside for loop : 

#include<stdio.h>

#include<conio.h>

void main()

{

    int i,j,k;  

    for(i=0,j=0,k=0;i<4,k<8,j<10;i++)  

    {  

        printf("%d %d %d\n",i,j,k);  

        j+=2;  

        k+=3;  

    } 

}

Output : 

0 0 0

1 2 3

2 4 6

3 6 9

4 8 12


For more RTU/BTU II Sem Computer Programming Lab  Experiments CLICK HERE

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...