Program Control - PowerPoint PPT Presentation

About This Presentation
Title:

Program Control

Description:

Class exercise. Write a program that prompts the user to enter a letter & outputs on the screen ... Class exercise. Write a program that prompts the user for ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 34
Provided by: williama3
Learn more at: http://www2.hawaii.edu
Category:
Tags: control | program

less

Transcript and Presenter's Notes

Title: Program Control


1
Program Control
  • ICS 111
  • Introduction to Computer Science II
  • William M. Albritton, Information and Computer
    Sciences Department at University of Hawaii at
    Manoa

2
Vocabulary
  • Primitive data type
  • A built-in data type
  • Closely models the computers 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

3
Integer Data Types
  • Type Storage Range
  • byte 1 byte (8 bits) -128 to 127
  • short 2 bytes (16 bits) -32,768 to 32,767
  • int 4 bytes (32 bits) -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

4
Floating-point Data Types
  • Type Storage Range
  • float 4 bytes (32 bits) -3.4 x 1038 to 3.4 x
    1038
  • (7 significant digits)
  • double 8 bytes (64 bits) -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

5
Two Kinds of Division
  • Integer division
  • Cuts-off digits after the decimal point (does
    not round up)
  • int x 1/2 //x0
  • Floating point division
  • Returns a floating point (decimal point) value
  • double d 1.0/2.0 //d0.5
  • d (double)1/2 // d0.5
  • d 1/2 // d0.0

6
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 newline '\n'
  • char tab '\t'
  • char double_quote '\"'
  • char single_quote '\''
  • char backslash '\\'

7
Boolean Data Type
  • Models truth values
  • Either true or false
  • boolean variable true
  • variable false

8
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
  • Javas wrapper classes correspond to the 8
    primitive data types
  • Byte, Short, Integer, Long, Float, Double,
    Character, Boolean

9
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

10
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
  • Equality operators , !
  • Relational operators gt, gt, lt, lt
  • Logical operators , , ! (and, or, not)

11
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(xfalse)
  • x can be boolean type, or boolean expression, or
    boolean (predicate) method

12
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

13
Increment Decrement
  • Preincrement operator
  • Add 1, then assign/use value
  • int a 3
  • int b a //b4, a4
  • Postincrement operator
  • Assign/use old value, then add 1
  • int a 3
  • int b a //b3, a4
  • a 3
  • int c 10 a //c13, a4

14
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

15
Conditional Operator
  • if (x gt y)
  • a x y
  • else
  • a x y
  • Can also be written as (conditional operator)
  • a (x gt y) ? (x y) (x y)
  • (boolean expression) ? (return 1st value if true)
    (return 2nd value if false)

16
Short Circuit Evaluation
  • Shortcut for evaluating logical operators
  • Logical or will stop at leftmost true operand
  • int a 3, b 3
  • if( (a4) (b2) )
  • System.out.println("a"a",b"b)
  • //a4,b3

17
Short Circuit Evaluation
  • Shortcut for evaluating logical operators
  • Logical and will stop at leftmost false operand
  • int a 3, b 3
  • if( (a3) (b2) )
  • System.out.println("a"a",b"b)
  • //a4,b3

18
Example Code Class Exercise
  • See Example2.java for examples on
  • Increment decrement
  • Assignment operators
  • Conditional operator
  • Short circuit evaluation
  • Class exercise
  • See Tracing2.java

19
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

20
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

21
Selection 3 Ways
  • If structure (single selection)
  • If-else structure (double, multiple selection)
  • if(condition1)
  • statement1
  • else if(condition2)
  • statement2
  • . . .
  • . . .
  • else
  • statementN
  • Switch structure (multiple selection)

22
If Statement
  • If no parenthesis, then only the 1st statement
    following the if statement is associated with it
  • if(a gt b)
  • c 3
  • d 4
  • So d4 will always be evaluated

23
If/else Ambiguity
  • If one if statement doesnt have an ending else
    another one does, may be ambiguous
  • Resolved by associating the else with the closest
    if
  • if(a gt b)
  • if(c gt d)
  • e 3
  • else
  • e 4
  • Must use braces to associate with the outside if
  • if(a gt b)
  • if(c gt d)
  • e 3
  • else
  • e 4

24
Switch Statement
  • Handles a series of decisions
  • Must include a break statement
  • If not, each case statement will be executed
    until encounter another break statement or reach
    end
  • Several cases can execute the same statements
  • By listing case statements one after another
  • Can only use char or int data types
  • int i 5 char c 'e'
  • switch(i) switch(c)
  • case 99 ... case 'a' ...

25
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

26
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

27
Repetition 3 Ways
  • while (condition) statements
  • while(55) System.out.println("infinite
    loop")
  • dostatements while(condition)
  • do System.out.println("one") while(1 lt 0)
  • for(initialize condition increment/decrement)
  • for(int i0 ilt50 ii10) System.out.println("
    five")

28
for while
  • for(expr1expr2expr3)
  • statement
  • Is equivalent to
  • expr1
  • while(expr2)
  • statement
  • expr3
  • This will create an infinite loop
  • for() . . .

29
Comma in For Loops
  • Can put commas in for loops
  • Evaluated left to right
  • int a0,b0
  • for(a0, b10 altb a, b--)
  • System.out.println("a"a
  • ",b"b)

30
Nested Loops
  • Can have loops within loops (or any other
    combination of nested repetition selection
    statements)
  • for(a0 alt5 a2) System.out.println("a"a)
  • for(b3 bgt0 b--)
  • System.out.println("b"b) //inner loop
  • //outer loop

31
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

32
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
    classs main method, while instance methods need
    to be called from an object of that class
  • See Coding2.java

33
Vocabulary
  • The following vocabulary from these slides will
    be on the next exam
  • Primitive data type
  • Wrapper class
  • Boolean expression
  • Flow of control
  • Selection statement
  • Repetition statements
Write a Comment
User Comments (0)
About PowerShow.com