03 - CS133: Developing Programming Principles Lecture 3...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS133: Developing Programming Principles Lecture 3 Programming Style, Boolean Expressions, If Statements Documentation and style • Make sure you use meaningful variable names. – classes, variables: noun phrases – methods: verb phrases • Add comments to make your code easier to understand (i.e., use English) • Add comments to your program to identify who wrote it, what it does, etc. CS133 Course Notes Lecture 3, Slide 2 1 2 Commented example import java.util.*; /** Hello program * @author Student McStudentson * @version 2 * Get the name of the user and output it along with a greeting. */ public class Example { public static void main (String args) { // Set up keyboard input Scanner myScanner = new Scanner(System.in); // Input name System.out.println("Enter your first name: "); String name = myScanner.nextLine(); System.out.println("Hello, " + name + "!"); } } CS133 Course Notes Lecture 3, Slide 3 Programming style tips • { and } should line up in the same column – DrJava does this for you! (<tab> key) • Place one instruction per line. • Use whitespace to separate code blocks – Briefly comment each code block • Comment and format code as you program CS133 Course Notes Lecture 3, Slide 4 3 4 if-else statements • Formal definition: A statement which allows a program to choose an action depending on the value of a boolean expression. • Example in plain English: If the temperature is greater than zero it will rain. Otherwise, it will snow. CS133 Course Notes Lecture 3, Slide 5 if-else statements continued • Syntax: if (Boolean_Expression) { Statement_List_1; } else { Statement_List_2; } CS133 Course Notes Lecture 3, Slide 6 5 6 if-else statement example • Example: if (temperature > 0) { System.out.println("It’s raining."); } else { System.out.println("It’s snowing."); } CS133 Course Notes Lecture 3, Slide 7 Compound statements • Put {} around multiple statements. – Around single statements too • The result is a compound statement treated as a single statement. • Most commonly used in if-else statements, as we had already seen, and loops. CS133 Course Notes Lecture 3, Slide 8 7 8 Importance of { } • Consider: if (instruction.equals("QUIT")) System.out.println("Game over"); System.out.println("The winner is " + winner); Importance of { } continued • Consider: if (numGuesses < maxGuesses) if (guess == actualNum) System.out.println("Correct"); else System.out.println("Out of guesses."); • What is printed when instruction is "QUIT"? When it is "CONTINUE"? CS133 Course Notes Lecture 3, Slide 9 • What is displayed when guess is correct? When it is wrong? Guesses remaining? CS133 Course Notes Lecture 3, Slide 10 9 10 Importance of { } continued • The outcome of code can be significantly different than intended depending on the absence or presence of braces. • Be sure to trace code as written, not as intended. • General Rule: use braces in all if statements, even if only one statement within body of if statement CS133 Course Notes Lecture 3, Slide 11 Boolean expressions • Must give a true or false value when evaluated. Recall a boolean variable can be either true or false. • Named after George Boole (1815-1864), inventor of Boolean algebra (using AND, NOT, OR with true/false values). CS133 Course Notes Lecture 3, Slide 12 11 12 Boolean expressions • either true or false. Math notation = ≠ > ≥ < ≤ CS133 Logical operators • Used to combine boolean expressions Java example Name Equal to Not equal to Greater than Greater than or equal to Less than Less than or equal to Java notation == != > >= < <= Course Notes Name balance == 0 income != tax expenses > income Java notation && || ! Java example stoveOn == true && stoveHot == true height < 10 || height > 20 !(myString.length() <= 5) AND OR points >= 60 pressure < max expenses <= income Lecture 3, Slide 13 CS133 Course Notes Lecture 3, Slide 14 NOT From p. 135 of text 13 14 Using AND and OR • English example: If the difference between the number of apples and oranges is at most 3, output, “Close”. Otherwise, output, “Far”. Using AND and OR continued int numApples, numOranges; if ((numApples - numOranges <= 3 && numApples - numOranges >= 0) || (numOranges - numApples <= 3 && numOranges - numApples >= 0)) { System.out.println("Close"); } else { System.out.println("Far"); } CS133 Course Notes Lecture 3, Slide 16 CS133 Course Notes Lecture 3, Slide 15 15 16 Testing for equality • When comparing primitive values, use ==. • When comparing objects, use the equals method (usually). • Strings also have an equalsIgnoreCase method • Generally, do not use == when comparing objects (like Strings). – The meaning of == is different for objects. CS133 Course Notes Lecture 3, Slide 17 Comparing Strings • Compare Strings with equals(otherString) and equalsIgnoreCase(otherString). • Example: String first = myScanner.nextLine(); String second = myScanner.nextLine(); if (first.equals(second)) { System.out.println("The two strings are identical."); } else { System.out.println("The two strings are different."); } CS133 Course Notes Lecture 3, Slide 18 17 18 Other forms of if-else statements • No else part Use if you want some code to be executed only if a condition is true. Same as having an empty else statement. Example: if (temperature > 32) { System.out.println("It’s hot"); System.out.println("I'm melting!"); } CS133 Course Notes Lecture 3, Slide 19 Nested statements • We can put statements inside each other. • There is no limit to how many times this can be done. CS133 Course Notes Lecture 3, Slide 20 19 20 Nested statements continued if (numGuesses < maxGuesses) { if (guess == actualNum) { System.out.println("Correct"); } else { System.out.println("Wrong"); } } else { System.out.println("Out of guesses."); } Multibranch statements • We can have more than two choices in an if-else statement. • Exactly one branch will be chosen. CS133 Course Notes Lecture 3, Slide 21 CS133 Course Notes Lecture 3, Slide 22 21 22 Multibranch statements continued • Example: if (grade == 'A') { System.out.println("Excellent!"); } else if (grade == 'B') { System.out.println("Good"); } else if (grade == 'C') { System.out.println("Passed"); } else if (grade == 'D') { System.out.println("Passed"); } else if (grade == 'F') { System.out.println("Failed"); } CS133 Course Notes Lecture 3, Slide 23 Multibranch statements continued • Example, rewritten: if (grade == 'A') { System.out.println("Excellent!"); } else if (grade == 'B') { System.out.println("Good"); } else if (grade == 'F') { System.out.println("Failed"); } else { System.out.println("Passed"); } CS133 Course Notes Lecture 3, Slide 24 23 24 ERROR: undefined OFFENDING COMMAND: STACK: ...
View Full Document

Ask a homework question - tutors are online