Sei sulla pagina 1di 20

PRESENTATION ON :-

Repetition Statement (for loop)

RUPESH GUJRATI
PARUL KALA
MCA IVTH SEM
REPETITION STATEMENTS
• Repetition statements allow us to execute a
statement or a block of statements multiple times
• Often they are referred to as loops
• Like conditional statements, they are controlled by
boolean expressions
• C has three kinds of repetition statements:
While loop
Do while loop
For loop
• The programmer should choose the right kind of
loop statement for the situation
For loop
For loop is a programming language conditional
iterative statement which is used to check for
certain conditions and then repeatedly
execute a block of code as long as those
conditions are met.
The for loop is distinguished from other looping
statements through an explicitly loop counter
or loop variable which allows the body of the
loop to know the exact sequencing of each
iteration.
For loop
• The for loop
for (expr1; expr2; expr3)
statement
• is exactly equivalent to the following
expr1;
while (expr2) {
statement
expr3;
}
For loop
• Expr1:-initialization
• Expr2:-condition
• Expr3:-updation
EXAMPLE
int i;
int limit = 10;
for (i = 0; i < limit; i++) {
...;
/* do something with ith entity
*/
...;
}
Nested for loop
int i, j;
int limit = 10, limit2 = 10;
for (i = 0; i < limit; i++) {
...; /*prepare subgroup i*/
for (j = 0; j < limit2; j++) {
...;
/* do something with item j of
subgroup i*/
...;
}
...;
}
QUESTIONS ?
1.void main()
{
int x = 123;
int i={printf(“c” “++”)};
for(x = 0;x <= i; x++){
printf(“%x “,x);
}
}
2.void main(){
int k = 0;
for(k < 3; k++)
printf(“hello”);
}
3.void main(){
double k = 0;
for(k = 0.0;k < 3.0;k++)
printf(“hello”);
}
a.run time error
b.hello is printed thrice
c.hello is printed once
d.logical error
4.void main(){
int i;
for(i = 0;i < 5; i++){
int i = 10;
printf(“ %d”,i);
i++;
}
}
5.void main(){
int k;
{
int k;
for(k = 0;k < 10;k++);
}
}
6.void main(){
int i = 0,j = 0;
for(j = 0;j < 5;j++)
i=i++;
printf(“%d %d”,i,j);
7.void main(){
for(;;){
static int x = 1;
int y = 1;
x++;
y++;
printf(“%d %d ”,x,y);
if(x > y)
break;
}
}
8.void main(){
int i;
for(i = 0;-i<3;i--)
printf(“%d”,i);
}
9.void main{
int i = 3;
for(;i;printf(“%d”,i--));
}
10.void main(){
int i,j;
for(i=2,j=5;i<5,j<10;i++,j++)
printf(“i=%d j=%d”,i,j);
11.void main(){
for(;;)
printf(“hello”);
}
12.void main(){
int i = 0;
for(;i;)
printf(“\n loop”);
printf(“\n end”);
}
13.void main(){
for(;0;)
printf(“\n loop”);
printf(“\n end”);
}
14.{int i;
for(;scanf(“%d”,&i);printf(“%d”,i)
break;
}

Potrebbero piacerti anche