C Loops

loop are used to execute group of given instruction and statement under certain situations. loop are suitable when need to perform similar type of operation are executed.

C loops

Why use loops?

suppose we are print a given number 20 times. then solution is we can use 20 time print function. but this not a best way. because this process is need time and extra codes. so best approach we can use solve this problem by loops. this is only basic example. most of time we are need to execute 1000 of instruction then loop are suitable.

For loop

for loop flowchart

Condition are check before each instruction are executed. syntax of for loop.

for(initialization;condition_expression;update_expression) statements;

There are 3 part.

Iteam Overview
for keyword
Initialization Initialization are set initial value of loop control variables. in this section are possible to declare variables and assign initial values
Condition expression Expression to execute the loop
Update expression In this section we are modified the value of variable those are control the execution of loop. Most of case in use to (increment and decrements) work

Note that separate those three section by semicolon.

Example of for loop.

/*
Example of for loop
*/
#include <stdio.h>
int main(){
  //array elemnts
  int node[]={1,6,8,2,4,0,3};

  //count size of array
  int size=(sizeof(node))/sizeof(node[0]),index=0;
  
  /*
  initialization : index=0  
  condition : index is less than size
  increment : index are increment by one
  */  
  for(index=0;index<size;index++){
    printf("\n %d",node[index]);//print array elements
  }
  return 0;
}

Stack Areamainindex (int) =0node [0] 1 [1] 6 [2] 8 [3] 2 [4] 4 [5] 0 [6] 3size (int) =7

Output

 1
 6
 8
 2
 4
 0
 3

Nested for loop loop inside a loop it is called nested loop. for example.

for(initialization;condition;increment/decrement){
  //code
  for(initialization;condition;increment/decrement){
  //code
    for(initialization;condition;increment/decrement){
      //code
      for(initialization;condition;increment/decrement){
        //code
      }
    }
  }
}

No limit of nested loop. we are use loop inside loop according to our requirements. look at view this example.

/*
Example of nested for loop
*/
#include <stdio.h>
int main(){
  //array elemnts
  int node[][3]={{1,2,3},
              {4,5,6},
              {7,8,9}};

  int row=3,col=3,i=0,j=0;
  
  /*
  Outer loop
  initialization : i=0  
  condition : i is less than row
  increment : i are increment by one
  */  
  for(i;i<row;i++){
    /*
    Inner loop
    initialization : j=0  
    condition : j is less than col
    increment : j are increment by one
    */  
    for(j=0;j<col;j++){
      //print array elements
      printf("  %d",node[i][j]);
      
    }
    printf("\n");
  }
    
  return 0;
}
Stack Areamaincol (int) =3i (int) =0j (int) =0node [3,3] [0][0] 1[0][1] 2[0][2] 3[1][0] 4[1][1] 5[1][2] 6[2][0] 7[2][1] 8[2][2] 9row (int) =3

Output

  1  2  3
  4  5  6
  7  8  9

infinite for loop

There are possible to execute infinite for loop. look at this example.

Example 1

/*
Example of infinite for loop
*/
#include <stdio.h>
int main(){
  int i=1;
  //print infinite 1 because not given teminate condition 
  for(i;;i){
    printf("\n %d",i);
  }
  return 0;
}

Output : printed 1 infinitely.

Note that in this loop not given termination condition.

Example 2:

/*
Example of infinite for loop
*/
#include <stdio.h>
int main(){
  int i=1;  
  for(;;){
    printf("\n %d",i);////print infinite 1 
  }
  return 0;
}

Output : printed 1 infinitely.

Note that in this not given initialization, termination condition and increment. but loop are work in this situations. so conclusion is (initialization, terminate condition and increment statement) is optional parameter.

Infinite loop are crash your program or some time hang your system. so try to give valid termination condition of every loop.

while loop

while loop are check first given expression. if expression are valid then execute loop statement. this process are execute until satisfied valid expression. in while loop we are modified terminate condition inside while loop.

while loop flowchart

while loop syntax

  while(expression){
  //loop statements (body of loop)
  //update terminate expression in case
  }

  /* while :keyword
    expression: test condition
    loop statements: actual code
  */

Example of while loop

/* 
  Example of while loop
*/
/*Header file*/
#include <stdio.h>

int main(){
  //array elements
   char data[]="C Programmer";
   int index=0;
   //while loop
   while(data[index]!='\0'){
    printf("%c",data[index]);// print array elements
    index++;//increment index by one
   }
    return 0;
}
Example print array elements

Output

C Programmer

In this example we are not predefined how many number of element in given array. but we have know about terminating point of array so we are easily utilized while loop in this case.

Note that while loop are executed loop statements in unknown number of times. if test expression are false then they are terminate execution process.

do while loops

First execute given instructions then check after looping conditions. for example (do while loop). It execute at least one iteration

do-while loop flowchart

Syntax of do while

  do{
  //statements
  }while(expression);

Example:

/* 
  Example of do while loop
*/
/*Header file*/
#include <stdio.h>

int main(){
  
    int num=5;

    do{
      //start execution of loop
      printf("%d\n",num++); //print num
    }while(num<10);//check terminating condition

    return 0;
}
5
6
7
8
9

Loop Control Statements

loop continue statement

Control statement are change normal flow of loop execution. there are following type.

1) continue
2) break
3) goto

continue statements

continue statement are used to change execution follow of loop. if we are use continue statement inside a loop. then they are skipping execution of other instruction and control are jump to beginning of the loop. and executes the next iteration of given loop. note that continue statement are use to inside a loop only.they are used (for ,while and do while loop)

Syntax is

  continue;

Example to continue statement in for loop.

/* 
  Example of continue statements
*/
/*Header file*/
#include <stdio.h>

int main(){
    //ster
    int counter=1;

    for(counter;counter<=8;counter++){
      //condition 
      if(counter>3 && counter <7){
        continue; //continue statements
      }
      printf("Counter: %d\n",counter);
    }

  return 0; 
}

Output:

Counter: 1
Counter: 2
Counter: 3
Counter: 7
Counter: 8

How to work in continue

While loop

continue exmple

Do while loop

continue statement exmple

Break statements

berak are used to terminate execution of given loop. break are used in loops and multichoice statement like switch. special cases sometime we are need to terminate loop in early of given expression or next other instruction of loop. so break are useful to execute loop.

/* 
  Example of break statements
*/
/*Header file*/
#include <stdio.h>

int main(){
   //start execution of program
    int counter=1;
    
    while(counter<=7){
    
      if(counter==5){
        //break statements
        break;
      }

      printf("Counter :%d\n",counter);
      counter++;//increment counter by one

    }
  return 0; 
}

Output:

Counter :1
Counter :2
Counter :3
Counter :4

goto statements

Goto statement

goto statements is jump statements. there are changing the flow control of execution in anywhere to anywhere within the function there is transform the control with given label or tag name. the specialty of goto statement is there are utilize this within the loop and within the function.

/* 
  Example of goto statements
*/
/*Header file*/
#include <stdio.h>

int main(){
   //start execution of program
    int counter=1;
    
    level:
    printf("%d\n",counter);
    counter++;
    if(counter<=5){
      goto level;//goto statement
    }
  return 0; 
}

Output

1
2
3
4
5

drawback of goto statement is difficult to trace bug and logical error.

© 2019, regularcodes.com, All rights reserved