Saturday, 8 August 2015

Loop's or Iterative Statements in C language

Loops


A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages:
C programming language provides the following types of loop:
for
while
do while

Syntax of for loop:

(1)
for(initialization; conditions;update_statements)
        statements

(2)
for(initialization; conditions;update_statements)
{        
block of statement
}

E.g.

main()
{
int a;
for(a=0;a<10;a++)
printf(" hello  %d",a);
}

output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9

E.g.

main()
{
int a;
for(a=0;a<10;a++)
{
printf(" hello  %d");
printf("%d",a);
}
}

output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9

Syntax of while loop:
while(condition)
{
statement's
}

E.g.
main()
{
int a=0;
While(a<10)
{
printf(" hello  %d");
printf("%d",a);
a++;
}
}

output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9

E.g.
main()
{
int a=0;
While(a<10)
{
printf(" hello  %d",a);
a++;
}
}

output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9
Syntax of do while loop:

do
{
statement's
}
while(Condition);

E.g.
main()
{
int a=0;
do
{
printf(" hello  %d");
printf("%d",a);
a++;
}
While(a<10);
}

output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9

E.g.
main()
{
int a=0;
do
{
printf(" hello  %d",a);

a++;
}
While(a<10);
}

output:
hello 0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9

No comments:

Post a Comment