Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "COMP 110 If / Else and Case Statements Tabitha Peck M.S. February 4, 2008 MWF 3-3:50 pm Philips 367 1."— Presentation transcript:

1 COMP 110 If / Else and Case Statements Tabitha Peck M.S. February 4, 2008 MWF 3-3:50 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? 3 From Lab 2 No real problems Never got the same problem twice

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 < 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 don’t believe you”); break; } 10 Controlling expression integer or char ONLY! Break statement Case label

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 12 if (n1 > n2 ) max = n1; else max = n2; max = (n1 > n2) ? n1 : n2;

13 Wednesday Read 3.2 Loop Statements 13


Download ppt "COMP 110 If / Else and Case Statements Tabitha Peck M.S. February 4, 2008 MWF 3-3:50 pm Philips 367 1."

Similar presentations


Ads by Google