Selection Flow Control - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Selection Flow Control

Description:

Strictly typed, so the declaration must specify the type of data to be held. ... Ternary operator ( ? : Return a value based on the truth value of a condition. ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 23
Provided by: rob1125
Category:

less

Transcript and Presenter's Notes

Title: Selection Flow Control


1
Selection Flow Control
  • Robert Salkin
  • CSI 205 OOP using Java
  • Summer 2005, 6W1

2
Quick Review
  • A variable is used to store a value for later
    use.
  • Must be declared before use.
  • Strictly typed, so the declaration must specify
    the type of data to be held.
  • Primitives hold a single value, while object
    variables may only reference an object.
  • Objects must be created before they are used.
  • Constants are variables that may only be set
    once.
  • Variables are given values using an assignment
    statement.
  • A variable must be present on the left side of
    the assignment operator ().

3
Quick Review
  • A string literal is a sequence of characters
    written between double quotes.
  • Usually used for output.
  • Can be referenced using a String variable.
  • String xhello
  • NOT a primitive.
  • Notice the capital S.

4
Quick Review
  • The online Java API (Application Programming
    Interface) is used as a reference for the
    predefined classes available for use.
  • http//java.sun.com/j2se/1.5.0/docs/api/index.html
  • Besides those in java.lang, you must import a
    class before you can use it.
  • To use Scanner, the class must be imported.
  • import java.util.Scanner
  • All of the java.util classes may be imported
    instead.
  • import java.util.
  • Importing all of the classes will not increase
    the size of the bytecode.

5
Quick Review
  • //Programmers name, etc.
  • / Description /
  • import java.util.Scanner
  • //Other imports here to use predefined classes.
  • public class MyClass
  • public static void main(String args)
  • Scanner inputnew Scanner(System.in)
  • //Other declarations and comments here.
  • //Your code and comments here.

6
Quick Review
  • To connect and edit
  • Start -gt Run -gt cmd -gt telnet eve.albany.edu
  • Login and press enter for terminal type, then run
    bash to allow for arrows and tab to work.
  • emacs MyClass.java
  • Ctrl-x-s saves, Ctrl-x-c closes.
  • To compile and run
  • /usr/local/usr/java/bin/javac MyClass.java
  • Bytecode is created and stored in MyClass.class
  • /usr/local/usr/java/bin/java MyClass
  • The program is run in the JVM
  • To submit
  • turnin-csi205 MyClass.java
  • turnin-csi205 v

7
Boolean Expression
  • A boolean expression is a statement that can be
    evaluated to either True or False.
  • Like a Yes or No question, but cannot result in a
    Maybe.
  • Boolean expressions use boolean logic,
    relational, and equality operators.

8
Equality Operators
  • Compare two values for equality.
  • True when values are equal.
  • ab
  • cD
  • e(Gf)
  • Compare two values for inequality.
  • True when values are not equal.
  • F!g
  • 0!h
  • p!(N-q)

9
Relational Operators
  • Four types
  • True when the value on the left side is less than
    the value on the right side.
  • 0ltb
  • True when the value on the left side is less than
    or equal to the value on the right side.
  • altb
  • True when the value on the left side is greater
    than the value on the right side.
  • bgtD
  • True when the value on the left side is greater
    than or equal to the value on the right side.
  • agt0

10
Logical Operators
  • AND ()
  • True if the truth value of both sides is true.
  • (aa)(bb)
  • false true
  • Cant be true -- right side will not be
    evaluated.
  • Called short circuit.
  • Use () to always evaluate both sides.
  • (cgt0)(cltG)
  • ((cd)y)(pgt0)

11
Logical Operators
  • OR ( two pipe symbols)
  • True if the truth value of at least one side is
    true.
  • (Agtb)(bltc)
  • true false
  • Cant be false right side will not be evaluated
  • Also called short circuit.
  • Use ( - single pipe) to always evaluate both
    sides.
  • ((bgtH)((ab)(agtD)))
  • (bgt0)(bltM)

12
Logical Operators
  • XOR Exclusive OR ()
  • True if the truth value of exactly one side is
    true.
  • true false
  • True.
  • true true
  • False.
  • false false
  • False.
  • xy

13
Logical Operators
  • NOT (!)
  • Unary, not Binary like AND, OR, and XOR.
  • True if the following value is false.
  • !(false)
  • True.
  • !(ab)
  • True unless a is equal to b.
  • !(bgtc)
  • !((bgtc)(df))
  • !(((x!y)(!(dltG)))(cltd))

14
Selection Statements
  • There are three types of selection statements.
  • if (and else)
  • If a condition is true, do X, otherwise do Y (or
    maybe nothing).
  • switch
  • Do something different for different values of a
    variable.
  • Ternary operator ( ? )
  • Return a value based on the truth value of a
    condition.
  • Will be covered later

15
If Statements
  • If a condition holds true, then run a block of
    code, otherwise dont run it.
  • if(ab)
  • System.out.printf(Equal!\n)
  • if((ad)(altc))
  • //Whitespace isnt ignored in string
  • //literals, so I dont want a line
  • //break between the two double quotes.
  • System.out.printf(
  • d is less than c!\n)

16
If..else Statements
  • If a condition holds true, run a block of code,
    otherwise run a different block of code.
  • //Assume a and b are ints with
  • //values assigned to them.
  • if(ab)
  • System.out.printf(Equal! Value is d.\n,a)
  • else
  • //Remember to include the variables to print.
  • System.out.printf(
  • Not Equal! ad bd\n,a,b)

17
Code Block
  • Code inside of curly braces () is called a
    block of code.
  • Blocks may nest.
  • if(xlty)
  • if(xltz)
  • System.out.printf(
  • x is less than y and z.\n)
  • else
  • System.out.printf(
  • x is only less than y, not z.\n)

18
Code Block
  • Variables declared inside of a block of code are
    said to be local to that block.
  • Local variables cannot be used outside of the
    block in which they were declared.
  • They are destroyed at the end of the block.

19
Block NOT Required
  • An if or else statement does not require curly
    braces if only one statement is contained in the
    block.
  • if(xgty)
  • System.out.printf(gt\n)
  • else
  • System.out.printf(lt\n)
  • But, use them anyway!!!

20
Dangling Else
  • An else belongs to the last if.
  • if(xgty)
  • if(xgtz)
  • System.out.printf(
  • x is big!\n)
  • else
  • System.out.printf(
  • x isnt so big.\n)
  • This shows why curly braces are important.

21
Switch Statement
  • A switch statement accept an expression that
    evaluates to a byte, short, int, or char, not a
    long.
  • The case label value is compared to the value of
    the expression in the parentheses following the
    keyword switch. If the values are equal, the code
    following that label is executed until a break
    statement or the end of the switch code block is
    reached.

22
Switch Statement
  • switch(xy)
  • case 0
  • System.out.printf(
  • Zero.\n)
  • break //Terminates the statement (ends the
    switch)
  • case NUMBER //What happens if xy equals
    NUMBER?
  • System.out.printf(
  • Number.\n)
  • case VALUE
  • System.out.printf(
  • Value.\n)
  • break
  • default
  • System.out.printf(
  • Default case reached!\n)
  • break
Write a Comment
User Comments (0)
About PowerShow.com