Sunday, 27 January 2013

Loops in C

 This is an attempt at explaining the loops in C .It is not complete, just the basics.
C Programming
C Programming

Loop is  a construct which execute a set of statements certain number of times.
 It is simply a way to execute the statement(s) as many as times you want. 
Suppose you have to print your name on screen? We all know how we can do this.

void main()
   printf(" YOUR NAME");
getch();
The above program simply print ypur name on the screen. Now What will you do when you ask to print your name 5 times? YES,you can write four more printf statement in above program But  when its 50 times or 100 time then? Who have dare to write printf statement that much time in his program,  

For  we have Loops 
 void main()
{ int i;
for(i=1;i<=100;i++)
   printf(" YOUR NAME");
getch();
This program is able to write your name 100 time.





Types of loop statement:
1.for statement:
the syntax is-
for ( initial; condition; increment )
  {statement(s);
  }

 When a compiler encountered with  for statement , the following events occur:
1. The expression initial is evaluated. initial is usually an assignment statement that sets a variable
      to a particular value.
2. The expression condition is evaluated. condition is typically a relational expression.
3. If condition evaluates to false, the for statement terminates, and execution  passes to the first statement following statement.
4. If condition evaluates to true , the  statement(s)  are executed.
5. The expression increment is evaluated, and execution returns to step .2

example:-
printint the multiple of 2

#include<sdtio.h>
#include<conio.h>
void main()
{  int i;
for(i=1;i<=10;i++)
{  printf("%d\n" ,2*i);
}
getch();
}

2.while statement:-
syntax
while (condition)
{ statements(s)
}
the following steps are evaluated
1. The expression condition is evaluated.
2. If condition is false, the while statement terminates, and execution
passes to the first statement following statement.
3. If condition is true the  statement(s) in  are executed.
4. Execution returns to step 1.

same program using while loop
#include<sdtio.h>
#include<conio.h>
void main()
{ int i;
while(i<=10)
   printf("%d\n" ,2*i);
       i=i+1;
}
getch();
}

3.do while Statement:-
syntax:
do
statement
while (condition);

1. The statements in statement are executed.
2. condition is evaluated. If it's true, execution returns to step 1. If it's false, the loop terminates
example:

#include<sdtio.h>
#include<conio.h>
void main()
{ int i=0;
do (
   printf("%d\n" ,2*i);
       i=i+1;
}while(i<10)
getch();

}
C - Overview
C - Basic Syntax
C - Data Types
C - Variables
C - Constants
C - Storage Classes
C - Operators
C - Decision Making
C - Loops
C - Functions
C - Scope Rules
C - Arrays
C - Pointers
C - Strings
C - Structures
C - Unions
C - Bit Fields
C - Typedef
C - Input & Output
C - File I/O
C - Preprocessors
C - Header Files
C - Type Casting
C - Error Handling
C - Recursion
C - Variable Arguments
C - Memory Management
C - Command Line Arguments

0 comments:

Post a Comment