COMP 110 If Else and Case Statements - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

COMP 110 If Else and Case Statements

Description:

Incorrect submissions == lose a letter grade. Three incorrect submissions ... ask if they are voting in the democrat or republican primary, and then if they ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 14
Provided by: michele9
Category:
Tags: comp | case | statements

less

Transcript and Presenter's Notes

Title: COMP 110 If Else and Case Statements


1
COMP 110If / Else and Case Statements
  • Tabitha Peck M.S.
  • February 4, 2008
  • MWF 3-350 pm
  • Philips 367

1
2
Announcements
  • New Homework Policy
  • Incorrect submissions lose a letter grade
  • Three incorrect submissions zero credit
  • Check .jar files BEFORE you send them
  • See me if you are still having problems
  • Grades
  • Lab 1, Lab 2, Project 1
  • Posting Grades online?
  • Handing back grades?

2
3
Questions?
  • From Lab 2
  • No real problems
  • Never got the same problem twice

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

4
5
Gotcha (Syntax)
  • if (boolean expression)

DO NOT DO THIS!!!!!!!
6
Today in COMP 110
  • More if / else statements
  • Case statements

6
7
Tracing if / else code
  • I give you code and input
  • You give me output

7
8
Example
  • input 5? input 6?
  • int days 0
  • if (input lt 6)
  • System.out.print(I worked input days
    this week)
  • else
  • days 6 input 1
  • System.out.print(I worked days days
    of overtime)

8
9
Another Example
  • Write the if/else part of a program that
    determines if a person is old enough to vote
    based on their age. If the person is old enough
    to vote, ask if they are voting in the democrat
    or republican primary, and then if they are
    voting for Obama or Clinton, or McCain or Romney.

9
10
Switch Statements
  • switch (numOfSiblings)
  • case 0
  • System.out.print(An only child) break
  • case 1
  • System.out.print(Just one you say) break
  • case 2
  • System.out.print(Two siblings!) break
  • case 3
  • System.out.print(Big family!) break
  • default
  • System.out.print(I dont believe you)
    break

Controlling expression integer or char ONLY!
Case label
Break statement
10
11
Practice with Switch statements
  • Write a switch statement that takes as
    controlling expression your year in college (as
    in int) and outputs your year in college as
    freshman, sophomore, junior, senior, or super
    senior

12
Shorthand for if / else
  • if (n1 gt n2 )
  • max n1
  • else
  • max n2
  • max (n1 gt n2) ? n1 n2

12
13
Wednesday
  • Read 3.2
  • Loop Statements

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