cis 110: introduction to computer programmingcis110/11fa/lectures/05/cis...computer programming...
TRANSCRIPT
![Page 1: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/1.jpg)
CIS 110: Introduction to Computer Programming
Lecture 5
The Loop-the-Loop
(§ 2.3-2.4)
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 1
![Page 2: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/2.jpg)
Outline
1. For-loops!
2. Algorithm Design and Pseudocode
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 2
![Page 3: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/3.jpg)
Announcements
• Date of the final is tentatively:
MONDAY, DECEMBER 19th, 6-8 PM
– If you have a conflict, please let me know ASAP.
• Need more practice? Try Practice-it!
– Web-based tool where you can work on practice problems that are automatically checked online.
– Linked off of the course webpage.
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 3
![Page 4: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/4.jpg)
For Loops
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 4
![Page 5: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/5.jpg)
Redundancy in Patterns
• Problem: write a program that prints out successive cubes.
Output:
0^3 = 0
1^3 = 1
2^3 = 8
3^3 = 27
4^3 = 64
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 5
![Page 6: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/6.jpg)
A Solution with Our Current Tools
public class Cubes {
public static void main(String[] args) {
System.out.println(“0^3 = “ + 0 * 0 * 0);
System.out.println(“1^3 = “ + 1 * 1 * 1);
System.out.println(“2^3 = “ + 2 * 2 * 2);
System.out.println(“3^3 = “ + 3 * 3 * 3);
System.out.println(“4^3 = “ + 4 * 4 * 4);
}
}
9/21/2011 6 CIS 110 (11fa) - University of Pennsylvania
• Pretty obvious repetition, but we have no way of dealing with it…
![Page 7: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/7.jpg)
Our First For-loop
public class Cubes {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i + “^3 = ” + i * i * i);
}
}
}
9/21/2011 7 CIS 110 (11fa) - University of Pennsylvania
• The for loop construct allows us to express repetitive patterns in a structured way.
![Page 8: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/8.jpg)
The For-loop
• The For-loop is a control statement.
– Doesn’t do anything on its own, but instead controls the execution of other statements.
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 8
for (int i = 0; i < 5; i++) {
System.out.println(i + “^3 = ” + i * i * i);
}
Initialization Continuation
Test Update
Body
![Page 9: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/9.jpg)
For-loop Semantics
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 9
Initialization Test Body Next
Test is true
Test is false
Update
int i = 0 i < 5 println i++
Loop iteration
![Page 10: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/10.jpg)
Our Example, Step-by-step (1)
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 10
public class Cubes { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println(i + “^3 = ” + i * i * i); } } }
Iteration # Value of i Test Output
![Page 11: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/11.jpg)
Our Example, Step-by-step (2)
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 11
public class Cubes { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println(i + “^3 = ” + i * i * i); } } }
Iteration # Value of i Test Output
1 0 0 < 5 is true 0^3 = 0
![Page 12: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/12.jpg)
Our Example, Step-by-step (3)
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 12
public class Cubes { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println(i + “^3 = ” + i * i * i); } } }
Iteration # Value of i Test Output
1 0 0 < 5 is true 0^3 = 0
2 1 1 < 5 is true 1^3 = 1
![Page 13: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/13.jpg)
Our Example, Step-by-step (4)
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 13
public class Cubes { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println(i + “^3 = ” + i * i * i); } } }
Iteration # Value of i Test Output
1 0 0 < 5 is true 0^3 = 0
2 1 1 < 5 is true 1^3 = 1
3 2 2 < 5 is true 2^3 = 8
![Page 14: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/14.jpg)
Our Example, Step-by-step (5)
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 14
public class Cubes { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println(i + “^3 = ” + i * i * i); } } }
Iteration # Value of i Test Output
1 0 0 < 5 is true 0^3 = 0
2 1 1 < 5 is true 1^3 = 1
3 2 2 < 5 is true 2^3 = 8
4 3 3 < 5 is true 3^3 = 27
![Page 15: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/15.jpg)
Our Example, Step-by-step (6)
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 15
public class Cubes { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println(i + “^3 = ” + i * i * i); } } }
Iteration # Value of i Test Output
1 0 0 < 5 is true 0^3 = 0
2 1 1 < 5 is true 1^3 = 1
3 2 2 < 5 is true 2^3 = 8
4 3 3 < 5 is true 3^3 = 27
5 4 4 < 5 is true 4^3 = 64
![Page 16: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/16.jpg)
Our Example, Step-by-step (7)
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 16
public class Cubes { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println(i + “^3 = ” + i * i * i); } } }
Iteration # Value of i Test Output
1 0 0 < 5 is true 0^3 = 0
2 1 1 < 5 is true 1^3 = 1
3 2 2 < 5 is true 2^3 = 8
4 3 3 < 5 is true 3^3 = 27
5 4 4 < 5 is true 4^3 = 64
6 5 5 < 5 is false
![Page 17: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/17.jpg)
For-loop Bounds
• Different sorts of bounds are possible!
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 17
for (int i = 1; i <= 5; i++) { System.out.print(i + “ “); } for (int i = 5; i > 0; i--) { System.out.print(i + “ “); } for (int i = 256; i > 0; i /= 2) { System.out.print(i + “ “); } for (int i = -3; i < 10; i += 2) { System.out.print(i + “ “); }
Output: 1 2 3 4 5
Output: 5 4 3 2 1
Output: 256 128 64 32 16 8 4 2 1
Output: -3 -1 1 3 5 7 9
![Page 18: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/18.jpg)
“<“ versus “<=“ Bounds
• “i = 0; i < 5” versus “i = 1; i <= 5”:
– Both give 5 loop iterations.
– “i = 0, 1, 2, 3, 4” versus “i = 1, 2, 3, 4, 5”.
• “i = 0; i < 5” is less intuitive, more canonical
– Most computations are naturally zero-based.
– For homework 2, either is fine.
• Try to get used to the zero-based style.
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 18
![Page 19: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/19.jpg)
Yo Dawg, I Heard You Liked Loops
• Problem: draw a rectangle of stars.
*****
*****
*****
*****
*****
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 19
![Page 20: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/20.jpg)
Solution: Loopception
public class Rectangle {
public static void main(String[] args) {
// The outer for-loop controls the #/lines
for (int i = 0; i < 5; i++) {
// The inner for-loop controls the
// contents of a single line.
for (int j = 0; j < 5; j++) {
System.out.print(“*”);
}
System.out.println();
}
}
}
9/21/2011 20 CIS 110 (11fa) - University of Pennsylvania
![Page 21: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/21.jpg)
Careful with Your Nested Loop Bounds!
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 21
for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { System.out.print(“*”); } System.out.println(); } for (int i = 0; i < 5; i++) { for (int j = 0; j < i; j++) { System.out.print(“*”); } System.out.println(); } for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; i++) { System.out.print(“*”); } System.out.println(); }
*****
*****
*****
*****
*****
*
**
***
****
Infinite loop!
![Page 22: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/22.jpg)
Algorithm Design and Pseudocode
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 22
![Page 23: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/23.jpg)
Remember Compilation?
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 23
PUT THE JELLY ON THE BREAD AND THEN PUT IT ALL IN YOUR MOUTH
Ilagay ang halaya SA tinapay AT pagkatapos ay ilagay ito LAHAT SA INYONG bibig
You (the programmer)
The computer program Machine code
The computer (your new best friend)
The compiler
![Page 24: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/24.jpg)
English is Still Useful!
• Sometimes it is difficult to write an algorithm/computer program directly.
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 24
/* ??? */
Problem: ***** ***** ***** ***** *****
“Draw 5 rows each containing 5 stars”
Pseudocode!
![Page 25: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/25.jpg)
Pseudocode Helps Organize Your Thoughts
• Stuck and don’t know how to make progress?
– Write an English description of your solution.
– Transform that English description into code.
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 25
“Draw 5 rows each containing 5 stars”
for (Each of 5 rows) { Draw 5 stars. }
for (Each of 5 rows) { for (int i = 0; i < 5; i++) { System.out.print(“*”); } }
for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { System.out.print(“*”); } System.out.println(); }
![Page 26: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/26.jpg)
By Example: A Cone
• Problem: draw the following cone shape.
/\
/--\
/----\
/------\
/--------\
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 26
“Draw 5 rows each containing a section of the cone.”
for (int i = 0; i < 5; i++) { Draw a section of the cone System.out.println(); }
![Page 27: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/27.jpg)
Cone Sections
• Each line has the following form:
– <spaces> / <dashes> \
– Let’s find the pattern for each part!
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 27
Iteration/row (i) Spaces / Dashes \
0 4 1 0 1
1 3 1 2 1
2 2 1 4 1
3 1 1 6 1
4 0 1 8 1
/\ /--\ /----\ /------\ /--------\
• Formula for spaces: 4 - i • Formula for dashes: i * 2
![Page 28: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/28.jpg)
From Tables to Code
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 28
for (int i = 0; i < 5; i++) { Draw a section of the cone System.out.println(); }
for (int i = 0; i < 5; i++) { Draw spaces Draw / Draw dashes Draw \ System.out.println(); }
for (int i = 0; i < 5; i++) { for (int j = 0; j < 4 – i; j++) { System.out.print(“ “); } System.out.print(“/”); for (int j = 0; j < i * 2; j++) { System.out.print(“-”); } System.out.print(“\\”); System.out.println(); }
![Page 29: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/29.jpg)
Stop Being So Constant
• What value controls the height of the cone?
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 29
for (int i = 0; i < 5; i++) { for (int j = 0; j < 4 – i; j++) { System.out.print(“ “); } System.out.print(“/”); for (int j = 0; j < i * 2; j++) { System.out.print(“-”); } System.out.print(“\\”); System.out.println(); }
The height of the cone but not immediately obvious!
An example of a magic number.
Surprise! An indirect use of the height of the cone. If we change
the height, then this number needs to change as well!
![Page 30: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/30.jpg)
(Bad) Example: Quake III Arena
float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;
}
9/21/2011 30 CIS 110 (11fa) - University of Pennsylvania
• Note: code is written in C but should be (somewhat) understandable! • This is the exact source from the game! • The constant was a source of much debate! See Fast Inverse Square Root @ Wikipedia for more details.
![Page 31: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/31.jpg)
Solution to Magic Numbers: Class Constants
• Class constants let us “document” magic numbers by naming them and giving us a central place to change their values if necessary.
9/21/2011 CIS 110 (11fa) - University of Pennsylvania 31
public class Cone { public static final int HEIGHT = 5; public static void main(String[] args) { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < (HEIGHT – 1) – i; j++) { System.out.print(“ “); } System.out.print(“/”); for (int j = 0; j < i * 2; j++) { System.out.print(“-”); } System.out.print(“\\”); System.out.println(); } } }
![Page 32: CIS 110: Introduction to Computer Programmingcis110/11fa/lectures/05/cis...Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania](https://reader030.vdocuments.us/reader030/viewer/2022040618/5f2584a18791b3115b36f898/html5/thumbnails/32.jpg)
Syntax of Class Constants
• Constants are declared outside of methods but inside the class. – public static final <type> <name> = <value>;
• Constants are variables that cannot be reassigned.
• Convention is to NAME_THEM_LIKE_THIS. 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 32
public class Cone { public static final int HEIGHT = 5; public static void main(String[] args) { for (int i = 0; i < HEIGHT; i++) { /* Snip! */