Professional Documents
Culture Documents
Lecture CSP08
Lecture CSP08
Lecture CSP08
Iteration in C
for statement
while statement
do-while statement
Flow Diagram of loop choice process
Ask yourself some of following questions to determine whether loops will be required
Basics of loops
updation
for Statement
Executes loop body as long as test evaluates to TRUE (non zero)
1. Initialization
2. Conditional Testing
3. Updation of loop control variable
i=1;
sum = 0;
for( ; ; )
sum + = i++;
How many times loop statement will be
executed?
for (i = 0,j = 7; i < j; i++, j--)
printf(“%d %d”,i,j);
Output???
for (i = 0,j = 7; i < j; i++, j--);
printf(“%d %d”,i,j);
Output???
for: Block(compound) statement
Choice1:
for (i = 0, sum = 0; i < 10; i++)
{
sum = sum + i;
printf(“%d”, sum);
}
Choice2:
i = 0; sum = 0;
for ( ; i<10;i++)
{ sum + = i;
printf(“%d”, sum);
}
* Example : find Volume and Surface Area of 5 Sphere */
#include <stdio.h>
int main()
{ float radius, sa,Volume, i;
printf(“program to compute volume of Ten sphere\n”);
for(i=0;i<5;i++)
{ printf("\n Please Enter the radius of a Sphere \n");
scanf("%f", &radius);
if(radius>=0)
{ sa = 4 * 3.14 * radius * radius;
Volume = (4.0 / 3) *3.14 * radius * radius * radius;
printf("\n The Surface area of a Sphere = %f", sa);
printf("\n The Volume of a Sphere = % f", Volume);
}
}
return 0;
}
* Example : print sum of N natural numbers
#include <stdio.h>
int main()
{ float num, i, sum;
sum=0;
printf(“Enter up to how many natural no you want to do summation\n”);
scanf("%d", &num);
for(i=0;i<=num; i++)
{ sum=sum+i;
}
printf("\n Sum of %d natural number is =%d”, num, sum);
return 0;
}
Examples:
Loop
/* print a multiplication table */
Statement
for (m1 = 1; m1 <= 10; m1++)
{
for (m2 = 1; m2 <= 10; m2++)
{
printf(“%d\t”, m1*m2);
}
printf(“\n”);
}
Braces aren’t necessary,
but they make the code easier to read.
Another Nested Loop
The test for the inner loop depends on the counter variable of
the outer loop.
while (test)
{ F
test
loop_body;
} T
loop_body
do
{ loop_body
loop_body;
} while (test);
test
T
F
digit = 0;
while(digit <= 9)
printf(“%d\n”,digit++);
digit = 0;
do
printf(“%d\n”,digit++);
while(digit <= 9);
init
loop_body
F
test F
test
T test
T T
loop_body F loop_body
updation
/*Volume and Surface Area of FIVE Spheres using WHILE LOOP*/
#include <stdio.h>
int main()
{ float radius, sa,Volume, i;
printf("program to compute volume of FIVE spheres\n");
i=0;
while(i<5)
{ printf("\n Please Enter the radius of a Sphere \n");
scanf("%f", &radius);
if(radius>=0)
{ sa = 4 * 3.14 * radius * radius;
Volume = (4.0 / 3) *3.14 * radius * radius * radius;
printf("\n The Surface area of a Sphere = %f", sa);
printf("\n The Volume of a Sphere = % f", Volume);
}
i++;
}
return 0;
}
/*Volume and Surface Area of FIVE Spheres using DO....WHILE LOOP*/
#include <stdio.h>
int main()
{ float radius, sa,Volume, i;
printf("program to compute volume of FIVE spheres\n");
i=0;
do
{ printf("\n Please Enter the radius of a Sphere \n");
scanf("%f", &radius);
if(radius>=0)
{ sa = 4 * 3.14 * radius * radius;
Volume = (4.0 / 3) *3.14 * radius * radius * radius;
printf("\n The Surface area of a Sphere = %f", sa);
printf("\n The Volume of a Sphere = % f", Volume);
}
i++;
} while(i<5);
return 0;
}
Most Suitable usage of DO....WHILE LOOP to computer 5 sphere volume
#include <stdio.h>
int main()
{ float radius, sa,Volume, i;
printf("program to compute volume of FIVE spheres\n");
i=0;
for(i=0;i<5;i++)
{
do
{ printf("\n Please Enter the radius of a Sphere \n");
scanf("%f", &radius);
} while(radius<=0);
sa = 4 * 3.14 * radius * radius;
Volume = (4.0 / 3) *3.14 * radius * radius * radius;
printf("\n The Surface area of a Sphere = %f", sa);
printf("\n The Volume of a Sphere = % f", Volume);
}
return 0;
}
Break and Continue
break;
• Used only in switch statement or iteration statement
• Passes control out of the “smallest” (loop or switch) statement
containing it to the statement immediately following
• Usually used to exit a loop before terminating condition occurs
(or to exit switch statement when case is done)
continue;
• Used only in iteration statement
• Terminates the execution of the loop body for this iteration
• Loop expression is evaluated to see whether another iteration should be
performed
• If for loop, also executes the re-initializer
Example: continue Example: break
int main() int main() {
{ int j; int j;
for (j=1;j<=3;j++) for (j=1;j<=3;j++)
{ if (i==j) { if (i==j) break;
continue; printf("(%d,%d)\n",i,j);
printf("(%d , %d) \n",i,j); }
} printf(“Outside Loop”);
printf(“\n Outside Looop”); return 0;
return 0; }
}
Example: break
k=2;
while(k<num)
{ if(num%k==0)
{
printf(“Message”);
break;
}
k++;
}
if(k==num) printf(“Message”);
output of program??
Examples
for (i=0;i<=20;i++)
{
if (i%2==0) continue;
printf("%d ",i);
}