Download presentation
Presentation is loading. Please wait.
1
COMP 14 Introduction to Programming Miguel A. Otaduy May 19, 2004
2
Review Topics Comparisons with text Tokenizer objects Using the debugger Closing curly brace signals the end of an if statement.
3
Review Flow of Execution
4
Two-Way Selection
5
The if-else Statement if (height <= MAX) { adjustment = 0; } else { adjustment = MAX-height; }
6
Nested if Statements The general syntax of a nested if statement is: if (condition1) { block1 } else if (condition2) { block2 } else { block3 }
7
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
8
The switch Statement
9
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
10
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
11
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
12
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
13
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; }
14
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] 1. What is printed if movieRating is 1 and movieName is "Gigli"? 2. What is printed if movieRating is 5 ?
15
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. 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
16
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)
17
To do Work on assignment 3. Read ch. 5.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.