2.DataTypes Control Statements Operators
2.DataTypes Control Statements Operators
I Career Craft
Data Types
Variable Declarations
Simple form <datatype> Example int total; Optional initialization at declaration <data type> <identifier> = <init value>; Example int total = 0;
<identifier>;
Examples
int counter; int numStudents = 583; float gpa; double batAvg = .406; char gender; char gender = f; boolean isSafe; boolean isEmpty = true; String personName; String streetName = North Avenue;
char byte
short int long float double void
2 1
2 4 8 4 8
127
32,767
2,147,483,647
9,223,372,036,854,775,807
(byte) 0
(short) 0 0 0L 0.0F 0.0D
Approx Approx
Decision Statements
if statement omitting else branch: if (condition) statement1;
Decision Statements
if statement - single statement bodies: if (condition) statement1; else statement2; if statement - multiple statement blocks: if (condition) { statements; } else { statements; } // if
Useful when making a selection among multiple values of the same variable.
Not useful when selecting among values of different variables.
Switch syntax
switch (<variable>) { case <value>: // whatever code you want break; // usually need this case <value>: // whatever code you want break; // usually need this default: // whatever code you want break; // optional }
switch (number) { case 1: System.out.println ("One"); break; case 2: System.out.println ("Two"); break; case 3: System.out.println ("Three"); break; default: System.out.println("Not 1, 2, or 3"); break; // Needed??? } // switch
This might work without the default case, but would be poor technique
For safety and good programming practice, always include a 'default' case.
Switch
switch (month) { case 4: case 6: case 9: case 11: numdays = 30; break; case 2: numdays = 28; if(leap) numdays = 29; break; default: /* Good idea? */ numdays = 31; }
Java example:
<get first value> while (condition) { <process value> <get next value> }
Note: Check is made before entering loop thus it is possible that loop may not execute
Test Last Loop: Body of loop is guaranteed to be executed at least once. Useful when termination condition is developed inside loop.
Java example: int count; for (count = 1; count <= 50; count ++) { <loop-body> }
Secret!
A for Loop can always be converted to a while loop i = 0; while (i < 10) { System.out.println(i); i++; } for(i = 0; i < 10; i++) { System.out.println(i); }
This will help you understand the sequence of operations of a for loop
Operators
Binary Operators
Ternary Operators
Arithmetic Operators
Sign Unary Operators + Increment and Decrement Operators ++ - Basic Arithmetic Operators + * / %
Relational Operators
Greater than (>) Less than (<) Greater than or equal to (>=) Less than or equal to (<=) Equal to (==) Not equal to (!=)
Logical Operators
Short-circuit Logical AND (&&) Short-circuit Logical OR OR (||) Boolean Inversion-NOT (!)
Assignment Operators
= += -= *= /= %= &= |= ^=
Advanced Operators
Shortcut if-else statement (?:) Dot operator (.) Cast operator (<type>) new
instanceof
Equality
Arrays
If you want to keep track of many values, you need many variables All of these variables need to have names What if you need to keep track of hundreds or thousands of values?
Multiple values
An array lets you associate one name with a fixed (but possibly large) number of values All values must have the same type
The values are distinguished by a numerical index between 0 and array size minus 1
0 1 2 3 4 5 6 7 8 9
myArray 12 43
83 14 -57 109 12
myArray 12 43 myArray[0]
myArray[9]
myArray 12 43
83 14 -57 109 12
Examples: x = myArray[1]; // sets x to 43 myArray[4] = 99; // replaces 14 with 99 m = 5; y = myArray[m]; // sets y to -57 z = myArray[myArray[9]];// sets z to 109
Array values
An array may hold any type of value
All values in an array must be the same type For example, you can have: An array of integers An array of Strings An array of Person An array of arrays of Strings An array of Object
Arrays of objects
Suppose you declare and define an array of objects:
Person[ ] people = new Person[20];
Arrays of arrays
The elements of an array can be arrays Once again, there is a special syntax Declaration: int[ ][ ] table; (or int table[ ][ ];) Definition: table = new int[10][15]; Combined: int[ ][ ] table = new int[10][15]; The first index (10) is usually called the row index; the second index (15) is the column index An array like this is called a two-dimensional array
0 1
1 3 2 7
2
6 8
For example, table[1][1] contains 6 table[2][1] contains 8, and table[1][2] is array out of bounds To retrieve the values: for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { System.out.println ( table[i][j] ); } }
2 1 3 6 2 7 8
int[ ][ ] table = new int[3][2]; The length of this array is the number of rows: table.length is 3 Each row contains an array To get the number of columns, pick a row and ask for its length: table[0].length is 2 But remember, rows may not all be the same length