programming in oop/c++ · programming in oop/c++ by assistant professor dr. ali kattan introduction...

19
Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops

Upload: others

Post on 09-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

Programming in OOP/C++

ByAssistant Professor Dr. Ali Kattan

IntroductionLecture 2

1

Nested Loops

Page 2: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

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

Page 3: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

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

Page 4: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... 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

Page 5: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

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

Page 6: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

6

Example (1): Multiplication Table (for loop)

(Program explained on the board)

Page 7: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

Example (1): Multiplication TableThe output of this program will be

These are just the results of multiplication

7

Page 8: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

8

Example (1): Multiplication Table (while loop)

(Program explained on the board)

Page 9: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

9

Example (1): Multiplication Table (do .. while loop)

(Program explained on the board)

Page 10: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

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

Page 11: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

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

Page 12: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

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

Page 13: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

Example (5): combine (3) & (4)This is the same but one after another

13

Page 14: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

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

Page 15: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

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

Page 16: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

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

Page 17: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

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)

Page 18: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

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.

Page 19: Programming in OOP/C++ · Programming in OOP/C++ By Assistant Professor Dr. Ali Kattan Introduction Lecture 2 1 Nested Loops. ... hours and the inner loop controls the minutes. 3

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)