Saturday 23 August 2014

Iterative Statements (Looping Statements)

Iterative Statements (Looping Statements)
Whenever we have to execute some statements (or expression) for a certain number of time (or for a particular condition until it gets false) we use Loops (Iterative statements).
There are three types of Loop:
1)      While Loop
2)      Do-while Loop
3)      For Loop
While Loop: Provides a mechanism of repeating one or more statements while a particular condition is true.
                Syntax:
                Statement a;
                while(condition)
                {
Statement block;
}
Statement b;

Initially the control of the program is at “statement a”à then condition is checked à if true (1) à “statement block” will get executed à if false (0) control will jump out of the while loop and “statement  b“ will get executed.

Example: program to find sum of first 10 numbers:
                #include<stdio.h>
#include<conio.h>
int main()
{
int i=0, sum =0;
while(i<=10)
{             
sum=sum+I;
i=i+1;    // here condition is getting updated.
//When value of  i  will be equal to 11,
// condition gets false. And prinf() statement will get executed.
}
printf(“\n sum of first 10 numbers = %d”,sum);
getch();
return 0;
}

Note: we did not put semicolon at the end of while.

Do-while Loop: is similar to the while Loop, but the only difference is that the condition is tested after the Loop block. That means the Loop block will get executed at least once even if the condition is false.
                Syntax:
                Statement a;
                do
                {
                Statement block;
                }
                while(condition);
                Statement b;

Example:
                #include<conio.h>
                #include<stdio.h>
                int main()
                {
                int i=0;
                do
                {
                                printf(“\n  %d”, i);
                                i=i+1;
                }while(i<=10);
getch();
                return 0;
                }
                Now this code will print numbers from 0 to 11, not from 0 to 10.
Note: in Do-while Loop, we always terminate while() with semicolon but not in the while Loop.



For Loop: for loop also provides us the mechanism of repeating a task until a particular condition gets false.
Here the programmer exactly know that how many time the task has to repeat.

How the for loop is different from while and do while:
            Here the initialization of variable, checking condition, and updating condition is written in a single line.

Syntax:
                for(initialization; condition; update)
                {
                                Statement block;
                }
                Statement y;
Example :
                Program to print first n numbers using for loop.
#include<conio.h>
#include<stdio.h>
int main()
{
int a,n;
printf(“\n Enter the value of n “);
scanf(“ %d “,&n);
for(a=0;a<=n;a++)
{
                printf(“\n %d “, a);
}
getch();
return 0;
}
Mechanism :
11)      Firstly initialization of a=0;
22)      Check condition a<=10;
33)      If true à get in the statement block. And print the value.
Then update the condition a=a+1; i.e. a=1;
Again check condition a<=10;
True: again get into the statement block and print.
And so on.
44)      If falseà get out of the for loop(or statement block)

Note: there must be no semicolon after the for statement.

More about for loop:
11)      You can initialize and update more than one variable,.( must be separated by the comma(,) )
int i,j;
For(i=0,j=1; i<5; i++,j++)
{
Statement;
}
22)      Multiple conditions can be use by logical operators (&& or ||)
33)      If you have already initialize the variable then you can skip initialization in the for statement.
int i=0;
for( ; i<10;i++)
44)      Similarly you can update condition inside the block
int i=1;
for( ; i<10 ; )
{
Printf(“\n %d”, i);
i=i+3;
}
55)      If you do not provide any condition or a condition which is always true. Then it becomes an infinite loop.
for( ; ; )
{
printf(“programming infinitum “);
}

Or

for(i=1; i>0; i++)    // infinite loop



like us on facebook and get updated Programming Infinitum

No comments:

Post a Comment