Monday, March 3, 2008

loops and jumping statement

Go To Statement

C supports the goto statement to branch unconditionary from one point to another in the program. It requires a label in order to identify the place where the branch is to be made label is a variable name & followed by a colon:
go to label: label:
------- statement;
------- --------
label: --------
statement; go to label;

forward jump Backward jump

Loop control statement
In looping a sequence of statements are executed until a particular condition is satisfied. A program consists of 2 segments;
1 Body of loop.
2 Control Statement.
2 A control statement tests contains condition & then directs the repeated execution of statements. Three methods for looping:
(1) While (2) Do while (3) For
1 While:- The while is an entry controlled loop statement. The test condition is evaluated if the condition is true than the body of loop is executed. The process of repeated execution of the body continues until the test condition becomes false & control is transferred out of loop.
while(test condition)
{
body of the loop
}
The loop counter can of integer type or float.
/* Sum of series 1 to n*/
#include
#include
void main()
{
int x, n=1,sum=0;
clrscr();
printf("Enter Number");
scanf("%d",&x);
while (n<=x)
{
sum=sum+n;
n++;
}
printf("sum=%d",sum);
getch();
}
1 Initialization (out side the body of loop/before)
2 Test condition
3 Increment/decrement (inside the body of loop)

2 Do while:- In while statement condition is tested before executing any statement but in do-while the condition is tested after executing the do block.
do
{
body of loop
}
while(test condition);
Since the test condition is evaluated at the bottom of the loop. so the body of the loop is executed at least once.
/* Do while example*/
#include
#incluce
void main()
{
int x, n=a, sum=0;
clrscr();
printf("Enter number");
scanf("%d",x);
do
{
sum=sum+x;
n=n+1;
}while (n<=x);
printf("Sum=%d",sum);
getch();
}
/* while statement */ /* do while statement*/
#include #include
#include #include
main() main()
{ {
int i, n=1; int i,n=1;
printf("Enter number"); printf("Enter Number");
scanf("%d",&i); scanf("%d",&i);
while(n{ {
printf("value %d",n); printf("value %d",n);
n=n+1; n=n+1;
} } while (ngetch(); getch();
} }

3 For statement:- The for loop allowed us to specify 3 things about a loop in a single line.
1 Setting a loop counter to initial value.
2 Testing a loop counter using test condition.
3 Increasing value of loop counter.

for(initialization;test-condition;increment/decrement)
{
body of loop;
}
#include
#include
void main()
{
int i,n,sum=0;
printf("Enter value of n");
scanf("%d",&n);
for(i=0;i {
sum=sum+i;
}
printf("sum=%d",sum);
getch();
}
Additional features of for loop.
1 increment of loop counter.
main()
{
int i;
for(i=0;i<4;i++)
{
printf("value of i=%d",i);
i++;
}
2 initialization of loop counter
main()
int i=0;
for(;i<4;i++)
{
printf("%d",i);
}
}
3 main()
{
int i=0;
for(;i<4;)
{
printf("%d",i);
i++;
}
}
if the body of loop contain only a semicolon then it is called a null statement.
for(i=0;i<1000;i++)
;-null statement
time dely
4 Multiple initialization & increment:
for(i=1;j=2;j<=10;i++;j++)
multiple initialization increment expressions can be there in a for statement but only one expression is allowed in test statement.
General structure of for,do while & while statements.
for do-while while
for(n=1;n<=10;n++) n=1; n=1;
{ do while(n<=10)
------- { {
------ ----- --------
} n=n+1; n=n+1; } } while(n<=10)