Now we learn the Loops in c language. In previous lesion we learn condition statements.
A looping is a sequence of statements are executed until some conditions for termination of the loop are satisfied. A program loop therefore consist of two segments, one known as the body of the loop and the other known as control statement. The control statement tests th certain conditions and then directs the repeated execution of the statement in the body of the loop.
The c language provides for three loop constructs for performing loop operations. They are
- The While statement
- The dostatement
- The forstatement
The While statement
The simplest of all the looping structures in C is the whilestatement. We have used whilein many of our earlier programs. The basic format of the whilestatement is
while(test condition){body of the loop}
While loop statement in c programming language repeatedly executes a target statement as long as a given condition is true.The test-condition is evaluated and if the condition is true, then the body of the loop is executed. After execution of the body,the test-condition is once again evaluated and if it is true,the body is executed once again.This process of repeated execution of the body body continues until the test-condition finally becomes false and the control is transferred out of the loop.
The body of the loop may have one or more statements. The braces are needed only if the body contains two or more statements. However,it is a good practice to use braces even if the body has only one statement.
Example:
#include<stdio.h>
int main()
{
int a=1;
while(a< 5)
{
printf("value of a :%d\n",a);
a++;
}
return 0;
}
OUTPUT
Value of a: 1
Value of a: 2
Value of a: 3
Value of a: 4
The body of the loop executed 4 times for a=1,2,3,4 . The value of the a is increment by one value upto loop ended.
DO Statement
Thewhileloop construct that we have discussed in the above section makes a test of condition before the loop is executed. Therfore, the body of the loop may not be executed at all if the condtion is not satsified at the very first attempt. On some occasions it might be necessary to execute the body the loop before the test is performed.Such situations can be handled with the help of the dostatement.Syntax is follows:
do
{
body of the loop
}
while(test-condition)
A do-while is similar to a while loop except that a do-while loop is guaranteed to execute at least on time.The conditional expression appears at the end of the loop.
On reaching the dostatement.the program proceeds to evaluate the body of the loop first.At the end of the loop,the test-condition in the while statement is evaluated.If the condition is true,the program continues to evaluate the body of the loop once again. This process continues as longh as the condition is true.when the condition becomes false,the loop will be terminated and the control goes to the statement that appears immediately after the while statement.
EXAMPLE:
#include<stdio.h>
int main()
{
int a=1;
do
{
printf("value of a :%d\n,a);
while(a=a+1);
while(a=a+1);
}
return 0;
}
OUTPUT
Value of a: 1
Value of a: 2
Value of a: 4
For Loop
The for loop provides a more concise loop control structure. The general for the for loop is
for(initilization;test-condition;increment)
{
body of the loop;
}
#include<stdio.h>
int main()
{
int a=1;
for(int i=0;i<10 br="" i="">
printf("%d",i);10>
<10 br="" i="">10>
}
return 0;
}
OUTPUT
1 2 3 4 5 6 7 8 9
The execution of the for statement follows:
- Intilization of the contorl variables is done first, using assignment statements such as i=0 .The variables or known as loop-control variable.
- The value of the control variable is tested using the test condition. The test condition is a relational expression.such as i<10 that determines when the loop will exit. if the condition is true the body of the loop is executed;other wise the loop is terminated and the execution continues with the statement that immediately follows the loop.
- when the body of the loop is executed the control is transferred back to the for statement after evaluting the last statement in the loop Now the control variable is incremented using the increment operator and the new value of the control variable is assign tested to weather it satisfies the loop condition. if the condition is satisfied the body of the loop is again executed.This process continues till the value of the control variable fails to satisfy test condition.

No comments:
Post a Comment