Iteration In C: Different ways To Use For Loop In C With Examples

Iteration/Looping Statement In C

Looping statement is the process to repeat the piece of code multiple times until the condition is true.  It is also known as "Iterative statement".

Looping concept in c
Iterative Statement In C. ByTechAchievers.com   
As we can see in the diagram that first loop condition initializes, then first condition checks if condition is true or status is Y then piece of code will execute. If condition status is N then loop will stop.With every repetition condition will be updated with new values that it can reach to the end of loop.In this chapter we will learn three kinds of looping statements.

For Loop

While Loop

Do While Loop

For Loop  

Single For Loop –

For loop is one of the looping statements. It initializes counter, checks condition and increment or decrements the counter, for is the keyword, counter specification separated by and enclosed within the bracket.

Syntax :-

for ( counter ; counter condition ; counter increment/decrements ) {

  1………….

  2………….

}

Note: If there is only one line of code to be executed then we can avoid curly braces.

for ( counter ; counter condition ; counter increment/decrements )

  1………….

#Program1

Write a simple program to print 1 to 10 numbers in C.

# include <stdio.h>


void main() {


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


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


}


#Explanation


for     -  is the keyword


int i    -  i is the integer counter, counting starting from 1.


i<=10   -  this is the counting condition that is count until i <=10.


i= i+1  -  after loop execution i value will be incremented by 1.



Flow of the execution is ... 


i =1 


-> condition checking, "yes" it is less then 10 


-> code will execute (print value of i with new line) 


-> i value will increment.


Again,


i =2 


-> condition checking, "yes" it is less then 10 


-> code will execute (print value of i with new line) 


-> i value will be incremented by 1.


i =3 same process will continue until i=10

Multiple For Loop –

A program can have multiple for loops on requirement basis. Each one works separately. 
No one make impact on each other.

Syntax :-


for ( counter ; counter condition ; counter increment/decrements ) {

  1………….

  2………….

}

for ( counter ; counter condition ; counter increment/decrements ) {

  1………….

  2………….

} 

#Program2

Write a program to print table of any number.Write another program to ask user 
name and count number of vowels.

#include <stdio.h>


void main() {


int number,count=0;

char name[10];


printf("Please enter any number");

scanf("%d",&number);


for (int i =number; i<=number*10 ; i=i+number) {

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

}

printf ("Please enter your name");

scanf("%s",name);

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

if (   (name[i]=='a'||name[i]=='A')

     || (name[i]=='e'||name[i]=='E')

     || (name[i]=='i'||name[i]=='I' 

     || (name[i]=='o'||name[i]=='O') 

     || (name[i]=='u'||name[i]=='U') ) {

    count = count +1;

  }

}

printf("\n Count of vowels %d",count);

}

Nested For Loop –

When one for loop works inside the another for loop is called nested loop.Inner loop runs for each value of outer loop. 

Syntax :-


for ( counter ; counter condition ; counter increment/decrements ) {


               // outer loop

   
 for ( counter ; counter condition ; counter increment/decrements ) {


              // Inner Loop  

 }

}


#Program3

Write a program to print student name and marks in each subject using nested for loop.

#include <stdio.h>


void main() {


char name[10];

int marks,total;


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


printf ("\nPlease enter your name");

scanf("%s",name);


total = 0;


for (int j= 1; j<=3 ; j=j+1){


printf ("Please marks for subject %d -", j);

scanf("%d",&marks);


total = total +marks;


} // end of inner loop


printf("\ntotal - %d of %s",total,name);


} // end of outer loop


}


#Explanation


In the program outer loop (i) counter running for 5 times and inner 
loop (j) running for 3 times. 


i = 1,


-> if i<=5 , "yes" condition true 

-> enter student name and initialize his/her total of marks = 0.

-> for i =1 , j loop will run 1 to 3 times.

-> enter student's marks 3 times and add to total.

-> once j loop ends, print student's total marks and name.


i = 2

-> if i<=5 , "yes" condition true

-> enter student name and initialize his/her total of marks = 0.


-> for i =2 , j loop will run 1 to 3 times.


-> enter student's marks 3 times and add to total.

-> once j loop ends, print student's total marks and name.


Same process will be continue until i is i<=5.


In next chapter we will learn more practical examples on nested for loop including pre/post increment and decrements concepts. 


Thanks for reading, please do share and comment your suggestions.

No comments

Powered by Blogger.