Wednesday 7 February 2018

Looping In C

Loops are used to execute a set of statements repeatedly until a particular condition is satisfied
The basic purpose of loop is to execute set of statements until condition becomes false and same code repeated again and again.

Advantage
It is used to reduce length of Code
It takes less memory space.
Types of Loops
There are three types of loop available in C programming language.
·         while loop
·         for loop
·         do-while
  While loop
In while loop  First check the condition if condition is true then the control goes inside the loop body and  print the output , It will repeated again and again until condition becomes false. If condition becomes false control goes outside the body and loop is terminated
Syntax:
assignment;
while(condition){    
Statements;   
………….
Increment/decrement (++,--)

Example:
#include <stdio.h> 
#include<conio.h>
void main ()
{        
 int i=1; 
clrscr();   
         while(i<=5)  
       {     
            printf("%d\n",i);
            i++; 
          }
getch();
    } 
Output:
1
2
3
4
5
Note: If while loop condition never false then loop becomes infinite loop

do-while
First print output then checks the condition. Since condition is checked later, the body statements will execute at least once
Syntax:
assignment;
do
{    
Statements;   
………….
Increment/decrement(++,--)
      }
    while(condition); 

Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
i=1;
do
{
  printf("%d\n",i);
i++;
}
while(i<=5);
getch();
}
Output:
1
2
3
4
5

For loop
for loop is used to iterate a program several times and execute the code repeatedly.
We can initialize variable, check condition and increment/decrement value.
Syntax:
      for(initialization; condition; increment/decrement)
    {    
      //Statement
      }    

Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=5;i++)
{
  printf("%d\n",i);
}
getch();
}
Output:
1
2
3
4
5

Break
break is used to break loop or switch statement.
Syntax:
statement;      
break;  
Example:
#include <stdio.h> 
#include <conio.h> 
  void main()
      for (int i = 1; i <= 5; i++)   
          {   
              if (i == 4)   
              {   
                  break;   
              }   
        printf("%d\n",i);
          }   
getch();
Output:
1
2
3


Continue
continue statement is used to continue loop.
Syntax:
Statement;
continue;   
Example:
#include <stdio.h> 
#include <conio.h> 
void main() 
     for(int i=1;i<=5;i++){     
            if(i==3){   
               
                continue;     
            }     
             printf("%d\n",i);
        }       
getch();
Output:
1
2
4
5

Comments
Comments are not executed by compiler.
Comments are used to highlight the statements.
You can explain code, variable, method, object, property or class but not execute the code.
There are two types of comments in C.
  • Single Line comment (//)
  • Multi Line comment (/ ..... /).

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...