For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

26
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science

Transcript of For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Page 1: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

For loops, nested loopsand scopes

Jordi CortadellaDepartment of Computer Science

Page 2: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Outline

• For loops

• Scopes

• Nested loops

Introduction to Programming © Dept. CS, UPC 2

Page 3: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Calculate xy Algorithm: repeated multiplication x x x x

y times

y x i p=xi

4 3 0 14 3 1 34 3 2 94 3 3 274 3 4 81

Introduction to Programming © Dept. CS, UPC 3

Page 4: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Calculate xy

// Pre: y 0// Returns xy

int power(int x, int y) { int p = 1; int i = 0; while (i < y) { // Repeat y times p = px; i = i + 1; // p = xi

} return p;}

Introduction to Programming © Dept. CS, UPC 4

Page 5: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Calculate xy

int i = 0;while (i < y) { p = px; i = i + 1;}

// Pre: y 0// Returns xy

int power(int x, int y) { int p = 1; for (int i = 0; i < y; i = i + 1) { p = px; } return p;}

Introduction to Programming © Dept. CS, UPC 5

Page 6: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Calculate xy

int i = 0;while (i < y) { p = px; i = i + 1;}

// Pre: y 0// Returns xy

int power(int x, int y) { int p = 1; for (int i = 0; i < y; ++i) p = px; return p;}

i = i + 1

Introduction to Programming © Dept. CS, UPC 6

Page 7: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Factorial

// Pre: n 0// Returns n!int factorial(int n) { int f = 1; for (int i = 1; i <= n; ++i) f = fi; return f;}

int factorial(int n) { int f = 1; for (int i = n; i > 0; --i) f = fi; return f;}

Introduction to Programming © Dept. CS, UPC 7

Page 8: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Up-down sequence• Write a program that reads a positive integer

n and prints and up-down sequence(one number per line)

• Example (n=6):

1 2 3 4 5 6 5 4 3 2 1

Introduction to Programming © Dept. CS, UPC 8

Page 9: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

// This program reads a positive integer (n)// and prints and up-down sequence// (one number per line).// Example: 1 2 3 … n-1 n n-1 … 3 2 1

int main() { int n; cin >> n;

// up sequence: 1 2 3 … n-2 n-1 n for (int i = 1; i <= n; ++i) cout << i << endl;

// down sequence: n-1 n-2 … 3 2 1 for (int i = n - 1; i > 0; --i) cout << i << endl;}

Up-down sequence

Same name, different variables

Introduction to Programming © Dept. CS, UPC 9

Page 10: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Summary• A for loop is a special type of repetitive statement

with a loop counter.

• It is naturally used when the number of iterations is known before entering the loop.

• Recommendations:– Declare the loop counter locally (when possible).– Update the loop counter by a constant (++i, --i).– Do not modify the loop counter inside the for loop.

Introduction to Programming © Dept. CS, UPC 10

Page 11: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

// This program reads three numbers and// prints the same numbers in ascending order.

int main() { int x, y, z; cin >> x >> y >> z; if (x > y) { // Swap x y int t = x; x = y; y = t; } // We know that x y if (y > z) { // Swap y z int t = y; y = z; z = t; } // We know that z is the largest number if (x > y) { // Swap x y int t = x; x = y; y = t; } // We know that x y z cout << x << " " << y << " " << z << endl;}

Sort three numbers

9 5 2

5 9 2

5 2 9

2 5 9

Dif

fere

nt

vari

able

s

Introduction to Programming © Dept. CS, UPC 11

Page 12: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Print min and max// This program reads two numbers and prints// the minimum and the maximum (in this order).

int main() { int x, y; cin >> x >> y; if (x > y) { int min = y; int max = x; } else { int min = x; int max = y; }

cout << min << " " << max << endl;}

Wrong!min and maxare not visible

Introduction to Programming © Dept. CS, UPC 12

Page 13: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Print min and max// This program reads two numbers and prints// the minimum and the maximum (in this order).

int main() { int x, y; cin >> x >> y; int min, max; if (x > y) { min = y; max = x; } else { min = x; max = y; }

cout << min << " " << max << endl;}

Scope formin and max

Introduction to Programming © Dept. CS, UPC 13

Page 14: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

{ // a and b are not visible int a = 1, b = 20; // a and b are visible cout << a << endl; // prints 1 { // c is not visible, a and b are visible cout << a + b << endl; // prints 21 int b = 5, c = 4; // a, c and the inner b are visible, // but the outer b is not visible cout << a + b << endl; // prints 6 cout << c << endl; // prints 4 } // c is not visible cout << b << endl; // prints 20}

Scopes and visibility

Introduction to Programming © Dept. CS, UPC 14

Page 15: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Summary

• Variables are only visible within their scope.

• Recommendations:– Declare and use variables as locally as possible.– When possible, initialize variables with their

declaration.– Use meaningful names (e.g., min, count, avg, …)

• Do not try to reuse variables: fewer variables does not imply less memory.

Introduction to Programming © Dept. CS, UPC 15

Page 16: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Drawing a rectangle• Write a program that reads the dimensions of

a rectangle (x, y) and prints x columns and y rows of asterisks.

• Example (x=8, y=5):

Introduction to Programming © Dept. CS, UPC 16

Page 17: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Drawing a rectangle

// Reads the dimensions (x, y) of a rectangle// and prints x columns and y rows of asterisks.

int main() { int x, y; cin x >> y; drawRectangle(x, y);}

Introduction to Programming © Dept. CS, UPC 17

Page 18: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

// Prints a rectangle with <ncols> columns and// <nrows> rows of asterisks.void drawRectangle(int ncols, int nrows) { for (int i = 0; i < nrows; ++i) { printRow(ncols); }}

// Prints a row of n asterisks.void printRow(int n) { for (int i = 0; i < n; ++i) cout << ""; cout << endl;}

Drawing a rectangle

Introduction to Programming © Dept. CS, UPC 18

Page 19: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Ordered declaration of functions

void printRow(int n) { . . .}

void drawRectangle(int ncols, int nrows) { . . . // uses printRow}

int main() { . . . // uses drawRectangle}

Introduction to Programming © Dept. CS, UPC 19

Page 20: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

// Reads the dimensions (x, y) of a rectangle// and prints x columns and y rows of asterisks.

int main() { int x, y; cin x >> y; for (int r = 0; r < y; ++r) { for (int c = 0; c < x; ++c) cout << ""; cout << endl; }}

Drawing a rectangle: nested loops

Print row with x asterisks

Introduction to Programming © Dept. CS, UPC 20

Page 21: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Drawing a right equilateral triangle• Write a program that reads an integer n and

prints a right equilateral triangle with the length of the cathetus being n.

• Example (n=7):

Introduction to Programming © Dept. CS, UPC 21

Page 22: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Drawing a right equilateral triangle

Observation: row r has r asterisks

row r

Introduction to Programming © Dept. CS, UPC 22

Page 23: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

// Reads an integer n and prints a right// equilateral triangle with the length// of the cathetus being n.

int main() { int n; cin >> n; for (int r = 1; r <= n; ++r) { for (int c = 0; c < ?; ++c) cout << ""; cout << endl; }}

Drawing a right equilateral triangle

Print row r with r asterisks

Introduction to Programming © Dept. CS, UPC 23

Page 24: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

// Reads an integer n and prints a right// equilateral triangle with the length// of the cathetus being n.

int main() { int n; cin >> n; for (int r = 1; r <= n; ++r) { for (int c = 0; c < r; ++c) cout << ""; cout << endl; }}

Drawing a right equilateral triangle

Introduction to Programming © Dept. CS, UPC 24

Page 25: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

oooooooooooooooooooooooooooooooo

ExercisesDraw the following shapes (for any n):

Chess board

Introduction to Programming © Dept. CS, UPC 25

Page 26: For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.

Summary• Nested loops are useful when treating multi-

dimensional data, e.g., rows/columns, height/width, matrices, etc.

• Recommendations:– Use for loops if the number of iterations is known

before entering the loop.– Use a different local counter for each loop.

Introduction to Programming © Dept. CS, UPC 26