Professional Documents
Culture Documents
(Technical Part)
At 6th Semester of Graduate Programme
2015
Technical Session 3
(03
(03.02.2015
(03.02.2015)
02 2015)
2015)
(Loop)
2015
Session Objective
To use the while repetition statement to
execute
t statements
t t
t in
i a program repeatedly.
t dl
Counter-controlled repetition and sentinelcontrolled
t ll d repetition.
titi
Additional repetition control structures
-for
f
-dowhile
Technical Aptitude Training JISCE
2015
Th while
The
hil Statement
S
Repetition structure
Programmer specifies an action to be repeated while
some condition remains true
Pseudocode:
While there are more items on my shopping list
Purchase next item and cross it off my list
while
Example:
int product = 2;
while ( product <= 1000 )
product
d
= 2 * product;
d
Technical Aptitude Training JISCE
2015
2015
Flow Chart
2015
Counter-controlled repetition
Loop repeated until counter reaches a certain value
Definite repetition: number of repetitions is known
Example: A class of ten students took a quiz. The
grades (integers in the range 0 to 100) for this quiz
are available to you. Determine the class average
on the quiz.
q
2015
2
3
Outline
4
5
6
7
8
9
10
int grade;
i
d
int total;
/* grade
/
d value
l
*/
/
/* sum of grades input by user */
11
fig03_06.c
(1 of 2 )
Counter to control while loop
12
13
14
/* initialization phase */
total = 0;
/* initialize total */
15
Initialize counter to 1
16
17
18
/* processing phase */
while ( counter <= 10 ) {
/* loop 10 times */
/
/
19
20
21
22
23
} /*
/ end
d while
hil */
/
2015
/* termination phase */
26
O li
Outline
27
28
29
30
31
fig03 06.c
fig03_06.c
(2 of 2 )
grade: 98
grade: 76
grade: 71
grade: 87
grade: 83
grade: 90
grade: 57
grade: 79
grade: 82
grade: 94
average is 81
2015
Repetition Essentials
Loop
Group of instructions computer executes repeatedly while
some condition remains true
Counter-controlled repetition
p
Definite repetition: know how many times loop will execute
Control variable used to count repetitions
Sentinel-controlled repetition
Indefinite repetition
U d when
Used
h number
b off repetitions
titi
nott known
k
Sentinel value indicates "end of data"
2015
10
2015
11
Example:
int counter = 1;
// initialization
while ( counter <= 10 ) { // repetition condition
printf( "%d\n", counter );
++counter;
;
// increment
}
The statement
int counter = 1;
Names counter
Defines it to be an integer
Reserves space for it in memory
Sets it to an initial value of 1
2015
12
f Statements
for
S
2015
13
2015
14
for
E ample:
Example:
for( int counter = 1; counter <= 10; counter++ )
printf( "%d\n", counter );
2015
15
2015
16
2015
17
2015
18
d
do-while
hil repetition
i i statement
Format:
do {
statement;
} while ( condition );
2015
19
d
do-while
hil flow
fl chart
h
2015
20
PracticeCode
Calculate sum & average of N numbers using Array
Calculate
C l l
mean, variance,
i
standard
d d deviation
d i i off N numbers
b
Calculate Sum of digits of a given number
Prime numbers below 200
Find Length of String
Find maximum value
2015
21
Assignment:Day3
1. List all the prime numbers between two given numbers.
2. Write a program, that accepts a integer from the user and print the
integer
g with reverse digits.
g For eg:
g rev ((1234)) = 4321.
3. Find the sum of the digits of a given number. Find the digit of
convergence
4. Given a number,, determine its absolute value.
5. Given three numbers, determine whether they can form the sides of a
triangle.
6. Given a number, determine whether it is a valid year and if so, whether
it is a leap year.
7. Given two numbers, determine whether they are valid years, and if so,
list all leap years between the two years (both included).
included)
Technical Aptitude Training JISCE
2015
22