



|
|
Byron's CSC212 Web Site
|
Class Notes
Monday October 30 , 2000
|
|
|
The ``For'' Statemant, ``While'' Statement and Arrays.
Class Notes --
Monday October 30 , 2000
CSC212 - October 30, 2000
========================
Lecture Topic: The "For" Statement
Spec Form
for (<limit>;<test>;<incr>)
{
<statement - sequence>
}
Meaning
-------
<init>; Initialization
while (<test>) Test
{
<statement - sequence>
<incr>; Increment
}
Ex:
// code to sum the integers from 1 to 5.
int sum = 0;
for (int c = 1;c <= 5; c = c + 1)
{
sum = sum + c;
}
* The Equavelent "while" statement *
int c = 1;
while (c <= 5)
{
sum = sum + c
c = c + 1;
}
Execution:
sum -> 0 1 3 6 10 15 end
c -> 1 2 3 4 5 6
Assignments are "destructive"
Ex:
What does this do? A: Print a "Triangle of Stars"
for (int i = 25; i > 0; i = i - 1) "Outer"
{
for (int x = 1; x < i; x = x + 1) "Inner"
{
IO.print("*"); "Inner"
}
IO.println(); "Outer"
}
Translation: To "while" eqivalent
// Taking away the "outer for"
int i = 25
while(i > 0)
{
for (int x = 1; x < i; x = x + 1)
{
IO.print("*");
}
IO.println();
i = i - 1; Incrementation
}
// Taking away the "inner for"
int i = 25
while(i > 0)
{
int x = 1;
while(x<= i)
{
IO.print("*");
x = x + 1;
}
IO.println();
i = i - 1; Incrementation
}
Execution:
Input | Output
--------------------------------------------------
i -> 25 | ***** ... ** (25 times)
x -> 1 2 3 ... 25 |
|
i -> 24 | ***** ... * (24 times)
x -> 1 2 3 ... 24 |
etc.
DEF: An ARRAY is an object which contains some number
of objects of the "same" type, each of which may
be referenced.
___
A picture - | 2 |
|___| an ARRAY of int values
| 3 | of length 4.
|___|
| 5 |
|___|
| 7 |
|___|
Another Picture (2)
________
| "red" | An ARRAY of String values
|________| of length 3.
| "blue" |
|________|
|"yellow"|
|________|
Another Picture (3)
________
| circle |
|________| An ARRAY of circle values
| circle | of radius 3.
|________|
| circle |
|________|
ARRAY Declaration and instantiation for the first
picture.
int a[];
a = new int[4}; //create ARRAY
___
Example: a[] -> | 2 | a[0] = 2;
|___|
| 3 | a[1] = 3;
|___|
| 5 | a[2] = 5;
|___|
| 7 | a[3] = 7;
|___|
To establish and instantiate the array of picture 2
String primary[] = new String[3];
________
primary[] -> | "red" | primary [0]
|________|
| "blue" | primary [1]
|________|
|"yellow"| primary [2]
|________|
primary [0] = "red"
primary [1] = "blue"
primary [2} = "yellow"
For picture 3 - Create an ARRAY of 5 Circle cells.
Circle c[] = new Circle[5]
c[0] = new Circle [2.0];
c[1] = new Circle [1.0];
...
c[n] = new Circle [r];
Tasks:
Suppose a is an integer array and b is a String array.
Morover, both have been completely instantiated - thusly:
____ ________
a[] -> | 10 | b[] -> | "cat" |
|____| |________|
| 15 | | "dog" |
|____| |________|
| 20 | |"mouse" |
|____| |________|
| 25 | |"zebra" |
|____| |________|
| 30 |
|____|
1) Read a new value into the first ELT of array a.
a[0] = IO.read_int();
2) Add 10 to the first element of array a.
a[0] = a[0] + 10;
3) Display the values of array a, one per line, using
only one IO statement.
for (int x = 0; x < 4; x = x + 1)
{
IO.println(a[x]);
}
NOTE - YOU CAN'T DO THIS!:
# IO.print (a[0]);
IO.println(a[1]);
IO.println(a[2]);
IO.println(a[3]);
IO.println(a[4]);
=========================================================
123456789112345678921234567893123456789412345678951234567
|
|
|