Object Oriented Programming-Lecture No 4
Object Oriented Programming-Lecture No 4
Lecture No: 4
Lecture Name: Loops
Lecture Instructor: Shahzad Khan
Loops
Want to do some thing a fixed number of times?
A loop is a control flow statement for specifying
iteration, which allows code to be executed repeatedly.
Automate a certain code to be executed multiple times
for example.
Calculating pay checks for 120 employees.
Printing out squares of all the numbers from 1-50
Loops are ideal for such cases .
Types Of Loops in C
There are three basic types of loops in C/C++ namely
following.
For Loops
While Loops
Do Loops
For Loops
Keyword “for” is used.
Syntax : for(initialize ; test ; Increment)
{
Block of code you want to execute repeatedly
}
Uses three different expressions
Initialize expression
Test expression
Increment expression.
Anatomy of a For Loop
Using for loops
Exploring for Loop
Count=0 is the initialize expression which initializes
the loop.
Count <5 is the test expression which tests the loop.
Count ++ is the increment expression which
increments the loop.
Initialize Expression
Initializes the loop variable. The initialization variable
shows the startup point of a loop.
It tells the loop from where to start counting.
The Test Expression.
The test expression is the expression which is test with
ever iteration of the loop.
It uses relational operators to check if a condition gets
true.
If the test expression is true the loop keeps on
executing.
If the test expression gets false loop gets terminated
the control is shifted outside the loop scope.
Increment Expression
Increments the loop variable in each iteration until the
test expression gets false.
We can also use a decrement operator in for loop.
Multiple Statements in Loops
While Loop
While loop is also a type of loop
A while loop in C programming repeatedly executes a
target statement(s) as long as a given condition is true.
The condition can be a single
When the condition becomes false, the program control
passes to the line immediately following the loop.
The key point to note is that a while loop might not
execute at all. When the condition is tested and the
result is false, the loop body will be skipped and the
first statement after the while loop will be executed.
Syntax of While Loop
while(condition)
{
statement(s) you want to execute repeatedly until the
condition become false;
}
While loop block diagram
Using a while loop
Do Loop
Unlike for and while loops, which test the loop condition at
the top of the loop, the do...while loop in C programming
checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except the fact
that it is guaranteed to execute at least one time.
the conditional expression appears at the end of the loop, so
the statement(s) in the loop executes once before the
condition is tested.
If the condition is true, the flow of control jumps back up to
do, and the statement(s) in the loop executes again. This
process repeats until the given condition becomes false.
Syntax of a do loop
do
{
statement(s);
}
while( condition );
Block diagram of do loop
Using a do loop