© Manzur
Ashraf
Objective:
Learn
the Repetition Structures.
Scope:
The
student should know the following:
1.
For loop
2.
While loop
3.
Do while loop
4.
Nested loops
Discussion:
The
following problem will be discussed:
Write
a program that displays a menu for conversion from Celsius to Fahrenheit and
vice versa.
Syntax of
different loops :
for while do-while
for(n=1; n<=10;
++n) n=1; n=1;
{
while(n<=10) do
------
{
{
------
------
------
------
------ ------
}
n=n+1;
n=n+1;
}
} while(n<=10)
------------------------------------------------------------------------------------------------------------------
Useful Solved Examples using different loops :
Example (i):
for loop :
#include<stdio.h>
void main()
{
int num,square,limit;
printf("Please
Input value of limit : ");
scanf("%d",&limit);
for (num=0; num < limit; ++num)
{
square = num*num;
printf("%5d
%5d\n", num, square);
}
}
Example(ii).
:
/**********************************************************
Program Showing a Sentinel-Controlled for loop.
To
Compute the sum of a list of exam scores.
***********************************************************/
#include <stdio.h>
#define SENTINEL -99
int
main(void)
{
int
sum = 0, /* sum of scores input so
far */
score; /* current
score */
printf("Enter first score (or %d to quit)> ",
SENTINEL);
for(scanf("%d", &score); score != SENTINEL; scanf("%d", &score))
{
sum += score;
printf("Enter
next score (%d to quit)> ", SENTINEL);
}
printf("\nSum of exam scores is
%d\n", sum);
return (0);
}
Example(iii) :
while
loop :
/*Conditional
loop Using while Statement */
#include<stdio.h>
#define
RIGHT_SIZE 10
void main()
{
int
diameter;
printf("Please
Input Balloon's diameter > ");
scanf("%d",&diameter);
while(diameter < RIGHT_SIZE)
{
printf("Keep
blowing ! \n");
printf("Please
Input new Balloon's diameter >
");
scanf("%d",
&diameter);
}
printf("Stop
blowing ! \n");
}
Example(iv).
:
do-while
loop :
/*
do-while loop */
#include<stdio.h>
void main()
{
int num;
do
{
printf("Please
Input Positive numbers between ( < 1
or >= 10 ) : ");
scanf("%d",&num);
}while (num < 1 || num >=10);
printf("Dear
, Sorry ! Your number is out of specified range in program");
}
Example(v)
:
Use of Increment and Decrement operators :
/*
Explanation on Increment , Decrement operators */
#include<stdio.h>
void main()
{
int n, m, p, i = 3, j
= 9;
n
= ++i * --j;
printf("From
above First given expression calculated n=%d i=%d
j=%d\n", n, i, j);
m
= i + j--;
printf("From
above second given expression calculated n=%d i=%d
j=%d\n",m ,i ,j);
p
= i + j;
printf("From
above Third given expression calculated n=%d i=%d
j=%d\n", p, i, j);
}
Exercises:
Solve
the following problems:
1.
Write a program that reads a set of grades
(between 0 to 100) and finds the
following:
a. Sum
b. Average
(Note: Solve
using do-while based on solved Example(iv) ).
2.
Write
a program to generate Squire , Cube , Square
Root of positive numbers from 1 to
any Limit Entered by user. (Note :
Solve using for loop base on solved example(i)
).
Evaluation:
Your
grade will depend on your active participation and seriousness during the lab.