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 |
Sunday, 27 January 2013
Home »
C language
» Loops in C
Loops in C
Related Posts:
Variables In C A variable is a name given to a storage area . Each variable in C has a type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of … Read More
Program to implement Stack in C /* Write a program to implement stack in C. Stack is a LIFO data structure * * LIFO - Last in First Out * * Perform PUSH(insert operation), POP(Delete operation) and Displa… Read More
Red Black Tree Red-black tree is a Binary search tree that has each node is colored either red or black. Properties of red black tree: 1. This is the same as for binary search trees: all the keys to left of a node are s… Read More
Algorithm and program for Binary Search. Binary search or half-interval search algorithm compares the element to be search(i.e key) with the middle element of the array. If the key is matched with the middle element ,it returns its positi… Read More
Types of Pointer in C Pointer is of five types:: 1.Wild pointer: A pointer which has not been initialized is known as wild pointer. int *ptr; printf("%d",*ptr); //Output: Garbage Value 2.Dangling pointer: If any pointer is pointin… Read More
0 comments:
Post a Comment