You are on page 1of 15

CSC240

INTRODUCTION TO
PROGRAMMING – I
Mr. Imran yar

Lecturer,
Department of Computer Science,
Jahan University
Kabul, Afghanistan.
Previous Lecture Outline
• Control Structures
• Understanding selection structure
• Working with IF and IF-ELSE
Lecture Outline
• Working with repetitive structures
• FOR, WHILE and DO-WHILE loops
Working with Repetitive Structures
• A control structure in which the statements are executed for the
number of times is called iterative or repetitive structure.
• The repetition continues while a condition is true. When the condition
becomes false, the loop ends and control passes to the statements
following the loop.
Working with Repetitive Structures
• There are three kinds of loop in C++
• that are:
•  For loop
•  While loop
•  Do-While loop

For Loop
• A type of iterative statement in which the statements are executed for
a fixed number of times,
• the loop is usually used when you know how many times you want to
execute the code before entering loop.
• The general form of for loop is as under:

for (initialization counter; condition; increment/decrement counter)


For Loop

False
While Loop
• The for loop does something a fixed number of time. What happens if
you don’t know how many times
• you want to do something before you start the loop? In this case a
different kind of loop may be used:
While Loop
• A while loop statement repeatedly executes a target statement as
long as the given condition is true.
• The general form of while loop is:

while(condition)
Statements;
While Loop
do-while Loop
• In a while loop the test expression is evaluated at the beginning
loop.
• If the test expression is false then the loop body won’t be executed
at all. In some situation this is what you want.
do-while Loop
• But sometimes you want to guarantee that the loop body is executed
at least once, no matter what the initial state of the test expression is.
• When this is the case you should use the do-while loop, which places
the test expression at the end of the loop.
do-while Loop
• The general form of do-while loop is as under:
• do
•{
• statements;
• statements;
• statements;
•}
• while(test expression);
Summery
• Working with repetitive structures
• FOR, WHILE and DO-WHILE loops
Thank You
For your Patience

You might also like