Presentation is loading. Please wait.

Presentation is loading. Please wait.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 1, 2005.

Similar presentations


Presentation on theme: "The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 1, 2005."— Presentation transcript:

1 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 1, 2005

2 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 2 Quiz Grades

3 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 3 Review Topics Comparisons with text Tokenizer objects Using the debugger Closing curly brace signals the end of an if statement.

4 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 4 Review Flow of Execution

5 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 5 Two-Way Selection

6 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 6 The if-else Statement if (height <= MAX) { adjustment = 0; } else { adjustment = MAX-height; }

7 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 7 Nested if Statements The general syntax of a nested if statement is: if (condition1) { block1 } else if (condition2) { block2 } else { block3 }

8 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 8 Nested if Statements if (hasFourLegs) { if (playsFetch) System.out.println ("DOG"); else System.out.println ("CAT"); } else if (hasWings) System.out.println ("BIRD"); else System.out.println ("FISH"); has four legs does not have four legs plays fetch doesn't play fetch has wings doesn't have wings

9 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 9 The switch Statement

10 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 10 The switch Statement Provides another means to decide which statement to execute next Evaluates an expression, then attempts to match the result to one of several possible cases Each case contains a value and a list of statements

11 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 11 The switch Statement The general syntax of a switch statement is: switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case... } switch and case are reserved words If expression matches value2, control jumps to here

12 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 12 The switch Statement Expression evaluated must be integral type ♦ integer or character, not boolean or floating point Case values must be constant (literal), not variable Can be implemented with nested if statements, but is much clearer with switch statements

13 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 13 The switch Statement default ♦ no case value matches the expression ♦ if no default exists and no case value matches the expression, no statements in the switch statement are executed break ♦ processing jumps to statement following the switch statement ♦ usually at the end of each case

14 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 14 The switch Statement System.out.print ("Enter prize code: "); int prize = Integer.parseInt(keyboard.readLine()); switch (prize) { case 1: System.out.println (“A Brand New Car!”); break; case 2: System.out.println (“A Trip to Hawaii!”); break; default: System.out.println (“Sorry, Try Again”); break; }

15 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 15 1. What is printed if movieRating is 1 and movieName is "Gigli"? 2. What is printed if movieRating is 5 ? Questions Assume movieRating is an int and movieName is a String switch (movieRating) { case 1: System.out.println ("Run away!"); if (movieName.equals("Gigli")) System.out.println ("Quickly!"); case 2: System.out.println ("Save your money"); break; case 3: System.out.println ("It's OK"); break; } Run away! Quickly! Save your money [Nothing]

16 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 16 What is printed with the following values of x and y : 1.x = 5, y = -4 2.x = -9, y = 5 3.x = 5, y = 5 Questions if ((x > 0) && (y < 0)) { z = x+y; System.out.println (“z = ” + z); } System.out.print (“The sum is “); if (x+y < 0) System.out.println(“negative.”) else System.out.println (“positive.”); z=1 The sum is positive. The sum is negative. The sum is positive.

17 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 17 Exercise Rock, Paper and Scissors Player 1 inputs (R)ock, (P)aper or (S)cissors Player 2 inputs (R)ock, (P)aper or (S)cissors Output message: ♦ Input of player A ♦ Input of player B ♦ Winner (or draw)

18 The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 18 To do Work on assignment 3. Read ch. 5.


Download ppt "The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 1, 2005."

Similar presentations


Ads by Google