1. Lecture 5Lecture 5
Version 1.0Version 1.0
Essentials of RepetitionEssentials of Repetition
The while loop structureThe while loop structure
Version 1.0Version 1.0
2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh
RepetitionRepetition
Most C programs involve in repetition orMost C programs involve in repetition or
loopinglooping
AA looploop is ais a group of instructionsgroup of instructions the computerthe computer
executes while someexecutes while some loop continuationloop continuation
conditioncondition remainsremains truetrue
3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh
Types of RepetitionTypes of Repetition
1. Counter-controlled repetition1. Counter-controlled repetition
2. Sentinel-controlled repetition2. Sentinel-controlled repetition
4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh
Counter Controlled RepetitionCounter Controlled Repetition
We know in advance exactly how many timesWe know in advance exactly how many times
the loop will be executedthe loop will be executed
AA control variablecontrol variable is used to count the numberis used to count the number
of repetitionsof repetitions
TheThe control variable iscontrol variable is
incremented/decrementedincremented/decremented each time theeach time the
group of instructions is performedgroup of instructions is performed
5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh
Counter Controlled RepetitionCounter Controlled Repetition
When theWhen the value of the control variablevalue of the control variable
indicates thatindicates that correct number of repetitionscorrect number of repetitions
has been performedhas been performed, the loop, the loop terminatesterminates
The computer continues executingThe computer continues executing thethe
statement after the repetition structurestatement after the repetition structure
6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh
Sentinel Controlled LoopSentinel Controlled Loop
It is not known in advance how many times theIt is not known in advance how many times the
loop will be executedloop will be executed
It is particularly useful when-It is particularly useful when-
a. The precise number of repetition is nota. The precise number of repetition is not
known in advanceknown in advance
b. The loop includes statements that obtainb. The loop includes statements that obtain
data each time the loop is performed.data each time the loop is performed.
7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh
The while LoopThe while Loop
The general form of while loop is-The general form of while loop is-
8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh
ExampleExample
The following program prints number 1 to 10 using aThe following program prints number 1 to 10 using a
while loopwhile loop
9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh
Case Study 1Case Study 1
The lecturer of CSE department will have toThe lecturer of CSE department will have to
count total number of passes and fails for hiscount total number of passes and fails for his
class after taking the final exam.class after taking the final exam.
The program will tell how many students passedThe program will tell how many students passed
and how many failed.and how many failed.
Moreover, if his class has number of passes overMoreover, if his class has number of passes over
80%, then we will call his class BEST.80%, then we will call his class BEST.
Otherwise, we will ask him to try better nextOtherwise, we will ask him to try better next
time.time.
10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh
Case Study 1Case Study 1
1. Input:1. Input:
The input of the program will be pass/fail onlyThe input of the program will be pass/fail only
for each student.for each student.
You will have to collect the number of studentsYou will have to collect the number of students
from the user.from the user.
This number of students will be your loopThis number of students will be your loop
control variable.control variable.
11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh
Case Study 1Case Study 1
2. Inside Loop:2. Inside Loop:
Inside the loop, you will have to keep track ofInside the loop, you will have to keep track of
number of fail and number of pass based on thenumber of fail and number of pass based on the
input.input.
12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh
Case Study 1Case Study 1
3. Output:3. Output:
You will have to show number of passedYou will have to show number of passed
students and number of failed students.students and number of failed students.
Also, you will have to see if the number ofAlso, you will have to see if the number of
passed students is 80% or higher.passed students is 80% or higher.
13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh
Let’s Start Planning the ProgramLet’s Start Planning the Program
1. Variables1. Variables
For holding number of students (will be used inFor holding number of students (will be used in
loop condition), number of pass, number of fail,loop condition), number of pass, number of fail,
loop counter, and the user input (pass/fail)loop counter, and the user input (pass/fail)
14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh
Let’s Start Planning the ProgramLet’s Start Planning the Program
2. A loop2. A loop
Inside of the loop, you will have to have anInside of the loop, you will have to have an
if/else structure (if passed then increase pass,if/else structure (if passed then increase pass,
else increase fail).else increase fail).
15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh
Let’s Start Planning the ProgramLet’s Start Planning the Program
3. At the end of the loop3. At the end of the loop
You will have to have another if/else structureYou will have to have another if/else structure
(if pass is above or equal to 80% it is the BEST(if pass is above or equal to 80% it is the BEST
class, else the teacher will be asked for betterclass, else the teacher will be asked for better
luck next time).luck next time).
17. 17Rushdi Shams, Dept of CSE, KUET, Bangladesh
Case Study 2Case Study 2
The user is teacher once again. The user will giveThe user is teacher once again. The user will give
the program- number of students. Then he willthe program- number of students. Then he will
provide their marks. In the end, the program willprovide their marks. In the end, the program will
respond with the average marks. The programrespond with the average marks. The program
can really help a teacher to understand the meritcan really help a teacher to understand the merit
of his class.of his class.