COMP 110 Branching Statements and Boolean Expressions - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

COMP 110 Branching Statements and Boolean Expressions

Description:

COMP 110 Branching Statements and Boolean Expressions – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 22
Provided by: michele9
Category:

less

Transcript and Presenter's Notes

Title: COMP 110 Branching Statements and Boolean Expressions


1
COMP 110Branching Statements and Boolean
Expressions
  • Tabitha Peck M.S.
  • January 28, 2008
  • MWF 3-350 pm
  • Philips 367

1
2
Announcements
  • Program 1 Due Today
  • Hand in Pseudocode after class
  • Program 2 Assigned Today
  • Lab 2 Due Friday

2
3
Questions?
3
4
Today in COMP 110
  • Formatting decimals
  • Review Worksheet
  • If/Else statements
  • Boolean Expressions

4
5
Formatting Decimals
  • import java.text.
  • DecimalFormat df new DecimalFormat("0.00")
  • df.format(myVariable)
  • Example code on class website

5
6
Review Worksheet
  • public class MyProgram
  • public static void main(String args)
    String myString This is a string
  • int len myString.length()
  • System.out.print(the length is len)
  • String shortString myString.substring(10)

6
7
Integer division
  • Double myDouble (1 / 2) 5.0
  • int i 1 / 2

O
0.5
7
8
Classes
  • Suppose that mary is an object of class Person,
    and suppose that increaseAge is a method of class
    Person that uses one argument, an integer. Write
    the invocation of the method increaseAge for the
    object mary using the argument 5.

Person mary new Person
mary.increaseAge(5)
8
9
Flow Chart
9
10
Flow Chart
Check time if (time lt 7am) take bus else
//time gt 7am take subway Reach school
10
11
Java Example
import java.util. public class FlowChart
public static void main(String
args) System.out.println("Give
me an integer") Scanner keyboard new
Scanner(System.in) int inputInt
keyboard.nextInt() if(
inputInt gt 5)
System.out.println("Big number")
else
System.out.println("Small number")

Start
Prompt User for int
Is user input greater than 5?
NO
YES
Print small number
Print big number
11
12
Java Comparison Operators
12
13
Boolean Expressions
  • True of False
  • Example expressions
  • 5 3
  • Variable lt 6
  • myInt ! temp
  • if (boolean expression)
  • statements

13
14
(and)
  • What if you need multiple expressions to be true
  • (expression) (expression)
  • Expressions go in ( )
  • Will only be true if ALL statements are true

14
15
(or)
  • What if you need ONE expression to be true out of
    many expressions
  • (expression) (expression)
  • Expressions go in ( )
  • Will be true if ONE expression is true

15
16
Gotcha
  • var1 var2 (assignment statement)
  • Error!!!!!!!
  • var1 var2 (boolean expression)
  • Do NOT use to compare Strings
  • string1 string2 //BAD
  • string1.equals(string2) //GOOD

16
17
If Without Else
  • You can use just an if statement
  • if (boolean expression)
  • (statements)
  • the rest of your code

17
18
Nested If Else
  • if (boolean expression)
  • if (boolean expression)
  • stuff goes here
  • else
  • more stuff
  • else

18
19
Start
Prompt User for int
What is the integer?
inputInt 0
inputInt gt 1
inputInt 1
Print how may I help you
Print hello
Print how are you
19
20
import java.util. public class FlowChart
public static void main(String
args) System.out.println("Give
me an integer") Scanner keyboard new
Scanner(System.in) int inputInt
keyboard.nextInt() if (
inputInt 0) System.out.println(
hello") else if ( inputInt
1) System.out.println(how are you")
else System.out.println(how may I help you")

20
21
Friday
  • Recitation
  • Bring Laptop
  • Bring Book

21
Write a Comment
User Comments (0)
About PowerShow.com