Download presentation
Presentation is loading. Please wait.
1
CS 177 Week 4 Recitation Slides
Conditionals
2
Announcements Project 1 is due on Sep. 24th 9pm Mentor Program
Please attend if you have a hard time understanding the book, the professor and your recitation instructor
3
QUESTIONS???
4
x++ vs. ++x x = 0; y = (x++) + 1; => x = 1 and y = 1
4
5
Conditions in the if Any expression that evaluates to a boolean is legal in an if-statement Comparisons x > y length == 10 Methods returning a boolean str1.equals(str2) Chatacter.isDigit(gender) A boolean true isHard (boolean variable)
6
Equality Is it fine to use == to represent equality? Correct Wrong int
char anything else you can list? Wrong double String everything started with an uppercase letter 6
7
Exclusivity if (condition) statement1; else statement2;
statement1 and statement2 can not both be executed Either statement1 or statement2 will be executed Also, statement1 and statement2 can be statement blocks instead of a single statement 7
8
Nesting if int score = 65; char grade; if (score >= 80) {
if (score >= 90) grade = ‘A’; else grade = ‘B’; } else { if (score >= 70) grade = ‘C’; if (score >= 60) grade = ‘D’; else grade = ‘F’; System.out.println(grade);
9
if and else if int score = 65; char grade;
if (score >= 90) grade = ‘A’; else if (score >= 80) grade = ‘B’; else if (score >= 70) grade = ‘C’; else if (score >= 60) grade = ‘D’; else grade = ‘F’; System.out.println(grade); 9
10
Confusing indentation
int score = 65; char grade; if (score >= 80) if (score >= 90) grade = ‘A’; System.out.println(grade); else grade = ‘B’; Grade not initialized. Also syntax error! Use braces to prevent this kind of problem. 10
11
Switch Values can only be int or char literals.
Switch (data) { case value1: statement1; break; case value2: statement2; … default: default statements; } Values can only be int or char literals. Always write default statements. 11
12
Breaks in switch What’s wrong here? Switch (direction) { case ‘N’:
robot.goNorth(); case ‘S’: robot.goSouth(); case ‘E’: robot.goEast(); case ‘W’: robot.goWest(); default: System.out.println(“Invalid direction.”); } What’s wrong here?
13
Breaks in switch Test if a number between 1-9 is a prime number.
Switch (number) { case 2: case 3: case 5: case 7: System.out.println(number + “ is a prime number.”); default: System.out.println(“Not a prime number.”); } What do we miss here?
14
StdIn Implemented by the book’s author. Code reuse.
Leveraging on the work done by others makes your life easier. To use the APIs encapsulated in StdIn, you must have the file StdIn.java in the same directory of your program file.
15
StdIn int readInt() Read in the next int double readDouble()
Method Use int readInt() Read in the next int double readDouble() Read in the next double boolean readBoolean() Read in the next boolean char readChar() Read in the next char String readString() Read in the next String String readLine() Read in a whole line String readAll() Read in all remaining input
16
Example System.out.println(“How many sides does your shape have?”);
int sides = StdIn.readInt(); if( sides == 3 ) { System.out.println(“Enter the base and the height:”); double base = StdIn.readDouble(); double height = StdIn.readDouble(); System.out.println(“The area of your triangle is “ + (.5 * base * height )); } else if( sides == 4 ) System.out.println(“Enter the length and the width:”); double length = StdIn.readDouble(); double width = StdIn.readDouble(); System.out.println(“The area of your rectangle is “ + (length * width)); else System.out.println(“I don’t know what the area “ + “of your shape is.”);
17
More Example System.out.println(“How many names do you have for your group?(2 or 3)”); int number = StdIn.readInt(); System.out.println(“Enter the names:”); String member1 = StdIn.readString(); String member2 = StdIn.readString(); if( number == 3 ) { String member3 = StdIn.readString(); } else if( number != 2) System.out.println(“Invalid number.”);
18
Final QUESTIONS???
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.