Introduction - University of Hawaii

Download Report

Transcript Introduction - University of Hawaii

Program Control
• ICS 111
– Introduction to Computer Science II
• William M. Albritton, Information and Computer
Sciences Department at University of Hawai’i at
Manoa
Vocabulary
• Primitive data type
– A built-in data type
•
•
•
•
Closely models the computer’s memory
Predefined by the Java language
Manipulated by built-in operators
Do not have methods or instance variables (very
different than objects)
– Java has 8 primitive data types
• byte, short, int, long, double,
float, boolean, char
Integer Data Types
• Type
Storage
Range
• byte
• short
• int
1 byte (8 bits)
2 bytes (16 bits)
4 bytes (32 bits)
-128 to +127
-32,768 to +32,767
-2,147,483,648 to
+2,147,483,647
• long
8 bytes (64 bits)
-9,223,372,036,854,775,808 to
+9,223,372,036,854,775,807
Floating-point Data Types
• Type
Storage
Range
• float
4 bytes (32 bits)
• double
8 bytes (64 bits)
-3.4 x 1038 to +3.4 x 1038
(7 significant digits)
-1.7 x 10308 to +1.7 x 10308
(15 significant digits)
• Example declarations
– float f = 123.456f; //f for float
– double d = 123.456; //double by default
Two Kinds of Division
• Integer division
– “Cuts-off” digits after the decimal point (does not
round up)
• int x = 1/2; //x==0
• Floating point division
– Returns a floating point (decimal point) value
• double d = 1.0/2.0; //d==0.5
• d = (double)1/2; // d==0.5
• d = 1/2; // d==0.0
Character Data Type
• Stores a 2 byte (16 bit) Unicode character
– Have to use single quotes around the character
• char ch = 'a';
– Some special characters, which are called escape
sequences, begin with a backslash character (\)
• char
• char
• char
• char
• char
newline = '\n';
tab = '\t';
double_quote = '\"';
single_quote = '\'';
backslash = '\\';
Boolean Data Type
• Models truth values
– Either true or false
• boolean variable = true;
• variable = false;
Vocabulary
• Wrapper class
– A class that represents a primitive data type
• Used to store the primitive data type as an object
• Useful when a primitive data type has to be
manipulated by objects
– Java’s wrapper classes correspond to the 8
primitive data types
• Byte, Short, Integer, Long, Float,
Double, Character, Boolean
Wrapper Class
• Example declarations
– int x = 5; //primitive data type
– Integer y = new Integer(5);
//wrapper class
• Wrapper classes may have useful static
methods & static variables
– String five = new String("25");
– int z = Integer.parseInt(five);
//returns integer value 25
Vocabulary
•
Boolean expression
–
A combination of operators & operands that
returns true or false
•
•
•
–
Used in selection & repetition statements
Evaluated from left to right
Operands are what the operators “operate” upon
3 Kinds of boolean operators
1. Equality operators: ==, !=
2. Relational operators: >, >=, <, <=
3. Logical operators: &&, ||, ! (and, or, not)
Logical Operators
•
Logical and, or, not: &&, ||, !
–
–
–
–
Evaluated left to right
Returns true or false
Operands have to be boolean
Often see if(!x), which means
if(x==false)
• x can be boolean type, or boolean
expression, or boolean (predicate)
method
Example Code & Class Exercise
•
See Example.java for examples with
–
–
–
–
•
Primitive data types
Wrapper classes
Relational operators
Logical operators
Class Exercise
–
Tracing exercise
•
See Tracing.java
Increment & Decrement
•
Preincrement operator
–
Add 1, then assign/use value
• int a = 3;
• int b = ++a; //b=4, a=4
•
Postincrement operator
–
Assign/use old value, then add 1
•
•
•
•
int
int
a =
int
a = 3;
b = a++; //b=3, a=4
3;
c = 10 + a++; //c=13, a=4
Assignment Operators
x = x + 5;
– Can also be written as x += 5;
a *= b + 5;
– Evaluates to a = a *(b + 5);
– And not
a = a * b + 5;
Conditional Operator
if (x > y)
a = x * y;
else
a = x + y;
– Can also be written as (conditional operator)
a = (x > y) ? (x * y) : (x + y);
– (boolean expression) ? (return 1st value if true) :
(return 2nd value if false)
Short Circuit Evaluation
•
Shortcut for evaluating logical operators
–
Logical or: will stop at leftmost true operand
int a = 3, b = 3;
if( (++a==4) || (++b==2) )
System.out.println("a="+a+",b="+b);
//a=4,b=3
Short Circuit Evaluation
•
Shortcut for evaluating logical operators
–
Logical and: will stop at leftmost false operand
int a = 3, b = 3;
if( (++a==3) && (++b==2) )
System.out.println("a="+a+",b="+b);
//a=4,b=3
Example Code & Class Exercise
•
See Example2.java for examples on
–
–
–
–
•
Increment & decrement
Assignment operators
Conditional operator
Short circuit evaluation
Class exercise
–
See Tracing2.java
Vocabulary
•
Flow of control
–
Order of execution of statements in a program
•
•
•
Usually one statement is executed after another, from
the first statement in a program to last statement
Methods alter flow of control, by having control (also
called the “program counter” or “instruction pointer”)
jump to the method that is invoked (called), and then
jump back to the next line after the method call (return
address) in the program
Within a method, the execution order of statements is
controlled by selection & repetition statements
Vocabulary
•
Selection statement
–
Chooses which statement to be executed next
•
•
–
Also called “conditional statement”
Uses a boolean expression to choose which statement
or group of statements to execute
3 kinds of selection statements
• if, if-else, switch
Selection – 3 Ways
1. If structure (single selection)
2. If-else structure (double, multiple selection)
if(condition1)
statement1;
else if(condition2)
statement2;
. . .
. . .;
else
statementN;
3. Switch structure (multiple selection)
If Statement
•
If no parenthesis, then only the 1st statement
following the if statement is associated with it
if(a > b)
c = 3;
d = 4;
–
So d=4; will always be evaluated
If/else Ambiguity
•
If one if statement doesn’t have an ending else &
another one does, may be ambiguous
–
Resolved by associating the else with the closest if
if(a > b)
if(c > d)
e = 3;
else
e = 4;
–
Must use braces to associate with the outside if
if(a > b){
if(c > d)
e = 3;
}
else
e = 4;
Switch Statement
•
•
Handles a series of decisions
Must include a break statement
–
•
Several cases can execute the same statements
–
•
If not, each case statement will be executed until
encounter another break statement or reach end
By listing case statements one after another
Can only use char or int data types
int i = 5;
switch(i){
case 99: ...
char c = 'e';
switch(c){
case 'a': ...
Example Code
•
See Example3.java for examples on
–
•
Selection statements
Class exercise
–
Write a program that prompts the user to enter a
letter & outputs on the screen whether the letter
is a vowel or consonant
•
Use method char charAt(int position) to
access a character (char) in a String
•
See Coding.java
Vocabulary
•
Repetition statement
–
Used to execute statements over and over
•
•
–
Also called a “loop statement”, or “repetition structure”
Uses a boolean expression to repeatedly execute a
statement or group of statements
3 kinds of repetition statements
• while, do-while, for
Repetition – 3 Ways
1. while (condition) {statements;}
•
while(5==5){
System.out.println("infinite loop");
}
2. do{statements} while(condition);
•
do{
System.out.println("one");
}while(1 < 0);
3. for(initialize; condition; increment/decrement)
•
for(int i=0; i<50; i=i+10) {
System.out.println("five");
}
for = = while
•
for(expr1;expr2;expr3)
statement;
– Is equivalent to:
•
expr1;
while(expr2){
statement;
expr3;
}
– This will create an infinite loop:
•
for(;;){ . . .}
Comma in For Loops
•
•
Can put commas in for loops
Evaluated left to right
int a=0,b=0;
for(a=0, b=10; a<b; a++, b--)
System.out.println("a="+a+
",b="+b);
Nested Loops
• Can have loops within loops (or any
other combination of nested repetition
& selection statements)
• for(a=0; a<5; a+=2){
System.out.println("a="+a);
for(b=3; b>0; b--){
System.out.println("b="+b);
} //inner loop
} //outer loop
Alter Flow of Control
• break;
–
Immediately exit from while, for, do/while, or
switch statements
• continue;
–
–
Skips the remaining statements
Performs the next iteration of while, for, or
do/while loop
Example Code & Class Exercise
•
See Example4.java for examples on
–
•
Repetition statements
Class exercise
–
Write a program that prompts the user for integer
x, then uses x as input for static methods: int
Factorial(int x) and instance method
int Fibonnaci(int x)
•
•
Static methods of a class can be called from that
class’s main method, while instance methods need to
be called from an object of that class
See Coding2.java
Vocabulary
•
The following vocabulary from these slides
will be on the next exam
1.
2.
3.
4.
5.
6.
Primitive data type
Wrapper class
Boolean expression
Flow of control
Selection statement
Repetition statements