Lecture CSP08

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Program Control Structure

 Repetition of steps in a program is called loop


 Loop body contains the statements to be repeated

 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

1. Initialization of a loop control variable.


2. Execution of the loop statements.
3. Test for a specified value of the loop repetition
condition variable.
4. Updating (changing) the loop control variable.

C provides three constructs for loop operations


 for statement
 while statement
 do –while statement
for Statement
incr/decr
Syntax:
init
for (init; test-condn; updation)
{
loop_body; F
test
}
T
Loop body can have a single loop_body
statement or a group of statements.

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

Either 1 or 2 or 3 or ALL of them can be optional

Note: Test is evaluated before executing loop body.


for : Single statement
sum = 0;
for (i = 1; i <= 10; i++)
sum + = i;
i = 1;
sum = 0;
for ( ; i <= 10; i + = 1)
sum + = i;
i = 1;
sum = 0
for ( ; i < 10 ; )
sum + = i++;
More Examples

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:

/* -- what is the output of this loop? -- */


for (i = 1, sum = 0; i <= 10; i++)
sum + = i*i;
printf("%d ", sum);

/* -- what is the output? -- */


letter = 'a';
for (c = 0; c < 26; c++)
printf("%c ", letter+c);

/* -- what does this loop do? -- */


sum = 0;
for (i = 0; i < 10 && sum < 50; i++)
{
sum = sum + i;
printf(“%d %d\n”, i, sum);
}
Nested Loops
The statement of for is again a for statement
Loop body can (of course) be another loop.

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.

for (outer = 1; outer <= input; outer++)


{ /* outer loop */
for (inner = 0; inner < outer; inner++)
{ /* inner loop */
sum + = inner;
}
}
Example: Generating Fibonacci Numbers
Definition:
The sequence of Fibonacci numbers is defined recursively
f0 = 0, f1 = 1, fi+1 = fi + fi-1
for i = 1, 2, 3, ……….
So the sequence is like
0, 1, 1, 2, 3, 5, 8, 13, 21, ……
Algorithm Description
Let f1 contains the value of the current
Fibonacci number and f0 contains the value of
the previous Fibonacci number.
1. Save the value of f1 in a temporary.
2. Add f0 and f1, and store the value in f1, the
new number.
3. Store the value of temporary in f0.
4. Print, and then repeat (1,2,3)till the
required number of Fibonacci numbers.
5. End
Program to print first 10 Fibonacci Numbers
#include<stdio.h>

int main() /* To generate Fibonacci Numbers/


{ int f0 = 0,f1 = 1, i, temp;
printf(“%d, %d, ”,f0,f1);
/* Loop */
for(i = 0; i<10; i++)
{ temp = f1;
f1 += f0;
f0 = temp;
printf(“%d, ”,f1);
}
return 0;
}

Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,


Exercise:
Rewrite the Fibonacci series program to print
the series till the Fibonacci number fnum. User
will enter the value of fnum.
Assume that fnum is a valid Fibonacci number
Example: fnum = 377
Sol:
Tips: Set the appropriate condition in for loop
to print the series till Fibonacci number
fnum.
While

while (test)
{ F
test
loop_body;
} T
loop_body

 Loop body can contain block of statements.


 Executes loop body as long as test evaluates to TRUE (non-zero).

Note: Test is evaluated before executing loop body.


Infinite Loops
The following loop will never terminate: Why???
x = 0;
while (x < 10)
printf(“%d ”, x);

Loop body does not change condition, so test never fails.

This is a common programming error that can be difficult to


find.
do-while

do
{ loop_body
loop_body;

} while (test);
test
T
F

Loop body can contain block of statements.


Executes loop body as long as test evaluates to TRUE (non-zero).

Note: Test is evaluated after executing loop body.


Examples:

digit = 0;
while(digit <= 9)
printf(“%d\n”,digit++);

digit = 0;
do
printf(“%d\n”,digit++);
while(digit <= 9);

What is the output in both the cases?


Initially if digit value is 10 then what will happen?
Summary

While do..while for

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”);

Q: In this program this loop can be used for???


Example: continue
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if(i==j)
continue;
printf(“%d%d\n”,i,j);
}
}

output of program??
Examples

int main() { int main() {


int i; int j; int i; int j;
for (i=1;i<=3;i++) for (i=1;i<=3;i++)
{ { for (j=1;j<=3;j++)
for (j=1;j<=3;j++) { if (i==j) break;
{ if (i==j) continue; printf("(%d , %d) \n",i,j);
printf("(%d , %d) \n",i,j); }
} }
} return 0;
return 0; }
}
Examples

What does the following loop do in program?

for (i=0;i<=20;i++)
{
if (i%2==0) continue;

printf("%d ",i);
}

What happens if break used instead of continue?


Programming Practice : Using for Loops
1. Write a c program to calculate Factorial of an
integer number.
Factorial of N is defined as
N*(N-1)*(N-2)*(N-3)….1

2. Write a c program to calculate ab, a and b


are inputs to the program.

3. Write a c program to print even/odd values


between 0 to 100.

You might also like