View
5
Download
0
Category
Preview:
Citation preview
LOOP STATEMENTS IN C
• A loop allows one to execute a statement or block of statements repeatedly. There are mainly two types of iterations or loops – unbounded iteration or unbounded loop and bounded iteration or bounded loop.
• A loop can either be a pre-test loop or be a post-test loop as illustrated in the diagram.
Iteration and Repetitive Execution
while statement is a pretest loop. The basic syntax of the while statement is shown below:
“while” loop construct
#include <stdio.h>
int main()
{
int c;
c=0; // Initialization
while(c<5) // Test Expression
{
printf(“ \nHello”);
c++; // Updating
}
return 0;
}
This loop contains all the parts of a while loop. When executed in a program, this loop will output
Hello Hello Hello Hello Hello
An Example
“for” Loop Construct
• The general form of the for statement is as follows:
for(initialization; TestExpr; updating)
{
stmT;
}
for construct
flow chart
Example
#include <stdio.h> int main() { int c; for(c=0;c<5;c++) // initialization,Test Expression, updating { printf(“ \nHello”); } return 0; }
While and For loop syntax
• for(initialization; TestExpr; updating) { stmT; for loop } • Initialization; while(TestExpr) { stmT; while loop Updating; }
Initialization;
do
{
stmT;
updating;
}while(TestExpr);
“do-while” loop Construct
#include <stdio.h>
int main()
{
int c;
c=0; // Initialization
do
{
printf(“ \nHello”);
c++; // Updating
}while(c<5);
return 0;
}
This loop contains all the parts of a while loop. When executed in a program, this loop will output
Hello Hello Hello Hello Hello
An Example
Test
Condition
True
False
Body of The Loop
Entry
Statement
False
True
Entry
Exit Control
Body of The Loop
Test
Condition
PRE-TEST AND POST-TEST LOOPS
ENTRY EXIT
CONTROLLED LOOP CONTROLLED LOOP
Point to Note
With a do-while statement, the body of the loop is executed first and the test expression is checked after the loop body is executed. Thus, the do-while statement always executes the loop body at least once.
A nested loop refers to a loop that is contained within another loop.
If the following output has to be obtained on the screen
1
2 2
3 3 3
4 4 4 4
then the corresponding program will be
#include <stdio.h>
int main()
{
int row, col;
for(row=1;row<=4; row++)
{
for(col=1;col<=row; col++)
printf(“%d \t”, row);
printf(“\n”);
}
return 0;
}
NESTED LOOPS
SPECIAL CONTROL STATEMENTS/JUMP STATEMENTS
“goto” statements
“break” statements
“continue” statements
Examples using while loop
• WAP to calculate the sum of first 10 numbers
• WAP to print 5 times table
• WAP to accept a number from the user and calculate the sum of its digits
• WAP to accept a number from the user and check whether it is an Armstrong number or not.
• WAP to accept a number from the user and display its binary equivalent.
Examples using for loop
• WAP to calculate the sum of first 100 numbers
• WAP to accept ‘n’ numbers from the user and calculate its sum and average.
• WAP to accept a number from the user and calculate its factorial.
• WAP to calculate the first ‘n’ terms of the Fibonacci series where ‘n’ is given by the user.
• WAP to calculate the value of Xn
Examples using for loop
• WAP to calculate the sum of the following series upto n terms
1/2+ 3/4 + 5/6 + 7/8 + …….. +n
• WAP to calculate the sum of the following series upto n terms
x + x2/2 + x3/3 + x4/4 + …… + n
• WAP to calculate the sum of the following series upto n terms
x + x2/2! + x3/3! + x4/4! + …… + n
The control is unconditionally transferred to the statement associated with the label specified in the goto statement. The form of a goto statement is
goto label_name;
The following program is used to find the factorial of a number.
#include <stdio.h> int main() { int n, c; long int f=1; in: printf(“\n Enter the number:”); scanf(“%d”,&n); if(n<0) goto in: for(c=1; c<=n; c++) f=f*c; printf(“\n FACTORIAL IS %ld”, f); return 0; }
GOTO STATEMENT
Break Statement
When the break statement is encountered inside a loop , the loop is immediately exited and program continues with the statement immediately following the loop
19 Department Of Information
Technology
While(…………)
{. . . . . .
If (condition)
break;
. . . . . .
}
. . . . . .
do
{. . . . . .
If (condition)
break;
. . . . . .
} While(…………);
. . . . . .
For(…………)
{
. . . . . .
If (error)
break;
. . . . . .
}
. . . . . .
for(. . . . . . .)
{. . . . . . .
for(…………)
{. . . . . .
If (condition)
break;
. . . . . .
}
. . . . . . }
Exit
From
loop
Exit
From
loop
Exit
From
loop
Exit
From
Inner
loop
Continue Statement
Causes the loop to be continued with the next iteration after skipping any statements in between
21 Department Of Information
Technology
while (Test Condition)
{. . . . . .
if (. . . . . .)
continue;
. . . . . .
}
do
{. . . . . .
if (. . . . . .)
continue;
. . . . . .
} while (Test Condition);
for ( initialization; test condition ; increment)
{
. . . . . .
if(. . . . . . . )
continue;
. . . . . . .
. . . . . . .
}
“break” and “continue” statements
break continue
1. It helps to make an early exit from the block where it appears.
1. It helps in avoiding the remaining statements in a current iteration of the loop and continuing with the next Iteration
2. It can be used in loop constructs statements and switch construct.
2. It can be used only in loop constructs.
Recommended