programming in oop/c++ · programming in oop/c++ by assistant professor dr. ali kattan introduction...
TRANSCRIPT
Programming in OOP/C++
ByAssistant Professor Dr. Ali Kattan
IntroductionLecture 2
1
Nested Loops
What is a loop?Loops, also known as “iterations”, means the repeated execution ofsome code. This is known as ”Loop”. In C++, there are 3 types ofloop structures shown below
Nested loop means a loop inside another loop.
2
Why we need nested loops?Think of the clock we use to tell time.
Notice that the big hand will move 60minutes then the big hand moves 1 hour.
So every hour, the big hand moves 60times.
Every 24 hours we complete a day.Clocks usually have from up to 12 hours.
The clock is like nested loops, the outer loop controls the hours and the inner loop controls the minutes.
3
Why we need nested loops?There are many applications that needs a loop inside another loop.
See the code below that simulate a clock. Outer loop is the hours.Inner loop is the minutes.(what if I want to add seconds?)
4
Basic ExamplesTo explain the idea behind nested loops we will introduce several examples. You must fully understand how these examples work.
Example (1): Multiplication Table
Look at the multiplication table on the right. You see numbers from 1 to 10 top, 1 to 10 side.
First line: 1x1, 1x2, 1x3, …1x10Second line: 2x1, 2x2, 2x3, ...2x10….Tenth line: 10x1, 10x2, 10x3, …10x10
The outer loop are the lines & the inner loop are the columns.
columns
lines
5
6
Example (1): Multiplication Table (for loop)
(Program explained on the board)
Example (1): Multiplication TableThe output of this program will be
These are just the results of multiplication
7
8
Example (1): Multiplication Table (while loop)
(Program explained on the board)
9
Example (1): Multiplication Table (do .. while loop)
(Program explained on the board)
Example (2): Letters triangleLet’s say we want to use nested loops to print the letters triangle shown.Notice that the last letter in the last line is ‘M’. Letters always starts with ‘A’. We can user char type in loops also
The outer loop are controls the last letter & the inner loop prints the columns starting always from A to the letter of the outer loop. 10
Example (3): stars triangleLet’s say we want to use nested loops to print the stars triangle shown.We have 6 lines and 6 columns of starts
The outer loop are controls the lines & the inner loop prints the columns stars.11
Example (4): up side down stars triangleLet’s say we want to use nested loops to print the up side down stars triangle shown. This is similar to the one before but inner loop count down.
The outer loop are controls the lines & the inner loop prints the columns stars.12
Example (5): combine (3) & (4)This is the same but one after another
13
Example (6): Factorial of a number Factorial of an integer can be computed for a range of numbers.For example factorial of 6 is written 6!6! = 1x2x3x4x5x6 = 720
The outer loop are controls the numbers & the inner loop prints the factorial. 14
Example (7): Factors for a numberFactors for an integer are all the numbers that it can be divided on without leaving remainder.
The outer loop controls the numbers & the inner loop prints the factors. 15
Example (8): Prime numbersA prime number is a number (integer), that can be divided only on 1 and on itself (example number 11, 7, 31, …)
The outer loop controls the numbers & the inner loop checks if the number is divisible or not. 16
Assignment 21) Rewrite the examples from (2) to (8) using:• while loops• do .. while loops.2) Write a full program to generate the following using nested for loops:
2 4 6 8 102 4 6 82 4 62 42
3) Challenge Questions: Write nested loops to generate the following shapes:
17
(A) (B)
18
Remember:
• If you don’t practice C++programming on the computer,YOUWILL NOT LEARN ANYTHING.
• To become a professionalprogrammer you must try all theexamples by yourself on acomputer.
How can I get the lecture notes?
19
• Check the website www.alikattan.org
Note: it will be ready AFTER 17-10-2018
• One hardcopy will be provided after the lecture for
photocopying.
• By email: [email protected] (if there is a problem only)