Oop Slides Ch05

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

University of Juba

Object-Oriented Programming
with C++

By: David L. Aggrey


(MSc. Computer Science – University of Khartoum)

School of Computer Science and Information Technology


Object-Oriented Programming with C++
Course Outline
• Chapter 1: Introduction to OOP
• Chapter 2: C++ Programming Fundamentals
• Chapter 3: C Features of C++
• Chapter 4: Selection Control
• Chapter 5: Iteration Control
• Chapter 6: Arrays
• Chapter 7: Operators and References
• Chapter 8: Classes, Objects and Functions
• Chapter 9: Constructors, Destructors and Overloading
• Chapter 10: Inheritance
• Chapter 11: Templates
Chapter Four
Iteration Control (Looping)
By: David L. Aggrey
(MSc. Computer Science – University of Khartoum)
School of Computer Science and Information Technology
Object-Oriented Programming with C++
Chapter 5: Iteration Control
INTRODUCTION TO LOOPING
• Looping is a process in which set of statements are executed
repeatedly for a finite or infinite number of times.
• In our practical life we see lots of examples where some repetitive
tasks has to be performed like finding average marks of students of
a class, finding maximum salary of group of employees, counting
numbers etc.
• A loop is a block of statements with which are executed again and
again till a specific condition is satisfied.

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
INTRODUCTION TO LOOPING
• C++ provides three loops to perform repetitive action.
› while
› for
› do-while
• To work with any types of loop three things have to be performed :
› Loop control variable and its initialization.
› Condition for controlling the loop.
› Increment / decrement of control variable.

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
The While Loop
• The syntax of the while loop is:

• The statements inside { } is called body of the while loop.


• All the statements within the body are repeated till the condition specified in the parenthesis in
while is satisfied.
• As soon as condition becomes false the body is skipped and control is transferred to the next
statement outside the loop.
• Warning: There should be no semicolon after the while.
University of Juba – School of Computer Science and Information Technology
Object-Oriented Programming with C++
Chapter 5: Iteration Control
The While Loop Example

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
The While Loop Example

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
The Break Statement
• The Break statement is used to come out early from loop
without waiting for the condition to become false.
• When the break statement is encountered in the while loop
or any of the loops, the control immediately transfers to first
statement out of the loop i.e., loop is exited prematurely.
• If there is nesting of loops the break will exit only from the
current loop containing it.

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
The Break Statement Example

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
The Continue Statement
• The continue statement causes the remainder of the statements
following the continue to be skipped and continue with the next
iteration of the loop.
• We use continue statement to bypass certain number of statements
in the loop on the basis of some condition given by if generally.
• The syntax of continue statement is simply
continue;

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
The Continue Statement Example

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
The For Loop
• The for loop is most frequently used by programmers just
because of its simplicity.
• The syntax of for loop is given here:

• There are various other syntaxes of for loop which we will see
in a short while.
University of Juba – School of Computer Science and Information Technology
Object-Oriented Programming with C++
Chapter 5: Iteration Control
The For Loop Example

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
How does the For Loop work?
• The for loop works by first making t = 1 and then checking the
condition whether t<=10 if so it executes the next statement
following for i.e., cout.
• It then goes to the increment the value of t, checks the test
condition and executes the cout. This continues till the condition t <=
10 is true.
• The execution of the for loop end as soon as the testing condition
becomes false in this case t = 11 control comes out from loop and
program terminates.
University of Juba – School of Computer Science and Information Technology
Object-Oriented Programming with C++
Chapter 5: Iteration Control
Various Syntax of For Loop - Examples

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
Nesting of For Loop
• Nesting of for loop is used most frequently in many programming situations and one of the
most important usage is in displaying various patterns, implementing tabular structures etc.
• The syntax::

• For each iteration of first for loop (outer for loop) the inner for loop runs as it is part of the
body of outer for loop.
• The inner for loop has its own set of statements which executes till the condition for inner
for loop is true.

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
The Nested For Loop Example

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
The Nested For Loop Example

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
The Nested For Loop Example

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
The do-while Loop
• The last loop is the do-while loop. Its syntax is given below.
• It is similar to while loop with the difference that condition is checked
at the end, instead of checking it at the beginning.

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
Some Important Points Regarding do-while Loop
• It is also called bottom testing loop or exit controlled
loop as condition at the bottom of the loop.
• Regardless of the condition given at the end of block
the loop runs at least once.
• The do-while loop is mostly used for writing menu
driven programs.

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
Quiz 3 (15 Minutes)
Write a C++ program to prompt the user for inputs (student name, marks for
7 courses) and calculate the total, average marks, determine the grade (A: 80
- 100, B: 70 - <80, C: 60 - <70, D: 50 - <60 or F: <50 and also determine
whether the student passed or failed. Finally, the program should display:
Student Name: <input_name>
Number of Subjects: <input_number_of_subjects>
Total Marks: <calculated_total>
Average Matks: <calculated_average>
Grade: <deteremined_grade>
Status: <pass_or_fail>

University of Juba – School of Computer Science and Information Technology


Object-Oriented Programming with C++
Chapter 5: Iteration Control
NEXT CHAPTER

Chapter 6
Arrays and Strings
0923659012
University of Juba – School of Computer Science and Information Technology

You might also like