Lecture 3
Lecture Topic:
For Loop
Syntax:
for (init; test;
statement) { statements; }
NULL statement:
for (int i =0; i <= 99; i++) { }
Inifinite
Loop:
for ( ; ; ) { if (?) break; }
Compound
Condition:
for (i=0, j=10; i <100; i++,
j+=2)
", " is permitted in increment initialization part of body of
FOR loop.
Do-While Loop
Like "repeat-until" loop, loop will execute at least once.
Syntax:
do {
statements;
} while (expression)
Continue
-- Abort out of the iteration.
Syntax:
continue; // break
out the current loop
continue identifier; // goto the labeled outer loop
Example:
foo : for
(int i =1; i <10; i++) { for ( int j=1; j <10; j++) { if (i%J==2)
continue foo; // break to the outer loop system.out.print(i * j + "
"); } }
Break
Syntax:
break; // jumps to
outside of the nearest loop
break identifier; // break to outside of the labeled nested loop
Example:
overbudget
= false; SS: for (i = 0; i <10; i++) { for (j=0; j <10; j++) { if
(coust[i+j] >10) { overbudget = true; break SS; // break out of {SS:
for(;;)} } } } if (overbudget) system.out.println("Ran out of money");
else system.out.println("Spend More ! ");
Data types and their corresponding class
Basic Type Corresponding Class boolean Boolean char Character
int Integer long Long float Float double Double
Example:
Integer foo = new Integer(10); // integer object foo =10 int i = 3;
// integer variable = 3
Packages
packages == like a library in C/C++.
Syntax:
import package typically;
import package classname;
import package * ;
Note: Extra imports are discarded, no extra linked code added.
Visibility
-- is field modifier in class
Type:
public -- world accessible protected -- class / subclass / package
default -- package access private protected -- class / subclass access private
-- accessible only within class
Example:
package graphics; public class circle
// add class circle to library { public int x, y; //(1) world access properties
or private protected x, y; //(2) class and subclass access void move(int
x1, y1) { x=x1; y=y1;} } package fruits; public class apple { int i; } //
adding apple class to library package fruits; public class plums { int y;
} // adding apple class to library package Sauce; import package fruits
*; // include another library public class chestnut { } ; // adding apple
class to library
Objects and Classes
--Objects have types
apple mash (apple a) // return type Class apple
{ a.c =0; return 1; }
class Point { // Class of Point
public double x, y; // data member of Point of class
}
Point p1 = new Point(); // create object of Point
p1.x = 10; // assign value to x
p1.y = 11; // assign value to y
Last Update: 9/23/96
Copyright ©1996 - Douglas Lyon
Lyon@cse.bridgeport.edu