9/11/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 5:If StatementsLast time:1. Variables and types2. Expressions in Java3. User input with Scanner objectsToday:1. Finish Scanner2. if statementsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Objects From Example 5:Scanner sc = new Scanner(System.in); sc is a variable Its type is …Scanner? What’s going on? Scanner is a class defined in java.util.Scanner System.in is a predefined object for keyboard input new Scanner(System.in) creates a new object in the Scannerclass and assigns it to sc Object? A bundle of data (instance variables) and operations (methods) A class defines both instance variables and methods for objects A class is also a type for objects new creates new objects in the given class We will learn (much) more about objects laterCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Control Flow and Conditionals Control flow: the order in which statements are executedGeneral rule: top to bottom Conditional statements permit control flow to be dependent on (true/false) conditions if if-elseCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3The if Statement Form:if ( <boolean-expression> ) <statement> Example:if (inchesOfSnow > 7)System.out.println( “Go home” ); The println statement is executed only if the variable “inchesOfSnow” is greater than 7 Otherwise, it is skippedboolean expression =conditionstatementCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Java and White Space You can add: carriage returns spaces tabswherever you want in Java Properly used, this makes your program easier to read and understand Proper indentation for if: If statement part is short, put on same line as condition Otherwise, put statement part on line below, indented by at least 2 spacesCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5Example 6public class Example6 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("Enter an integer: ");int i = sc.nextInt();if (i < 0)System.out.println("That was a negative number!");System.out.println("The number was " + i);}}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6The if-else Statement Form:if ( <condition> ) <statement 1>;else <statement 2>; Example:if (inchesOfSnow > 7)System.out.println(“Go home”);elseSystem.out.println(“Go to school”); If “inchesOfSnow” > 7, the first println statement is executed and the second is skipped Otherwise (i.e. inchesOfSnow ≤ 7), the first printlnstatement is skipped and the second is executedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7Indentation Convention for if-elseLine up if, else in same column, different lines If both if, else statements are short, put on same line as if / else If at least one is long: Put statements on different lines from if / else Indent these statement the same amount, at least 2 space from if / elseCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8Example 7public class Example7 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("Enter an integer: ");int i = sc.nextInt();if (i < 0)System.out.println("That was a negative number!");elseSystem.out.println("That was a non-negative number!");System.out.println("The number was: " + i);}}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9Blocks What happens?if (i > 10)i = 10;saturate = true;elsei = i+1; Desired: both i, saturate are set if i > 10 Actual: syntax error! Only one statement can be associated with if The saturate assignment statement is not part of the if The else has no if to belong to! Blocks solve this problemCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10What Blocks Are Blocks are sequences of statements “glued together” into one Form:{<statement 1>;<statement 2>;…} Example revisitedif (i > 10) {i = 10;saturate = true;}elsei = i+1;blockCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11Indentation Conventions for BlocksEitherif (…) {statement 1;statement 2;…} Orif (…){statement 1;statement 2;…}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland12Example 8public class Example8 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("Enter an integer: ");int i = sc.nextInt();if (i < 0) {System.out.println("That was a negative number!");System.out.println("I prefer positive ones, so I'll fix it...");i = -i;}else {System.out.println("That was a positive number!");System.out.println("That makes me happy.");}System.out.println("The number was " + i);}}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland13Logical OperatorsUsed for forming more complex conditions. “and” &&if ( temp >= 97 && temp <= 99 )System.out.println( “Patient is healthy” ); “or” ||if ( months >= 3 || miles >= 3000 )System.out.println( “Change your oil” ); “not”: !if ( ! phone.equals( “301-555-1212” ) )System.out.println( “Sorry, wrong number” );CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland14Example 9public class Example9 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("Enter an integer from 1 to 10: ");int i = sc.nextInt();if (i >= 1 && i <= 10)System.out.println("Good job!");elseSystem.out.println("You didn't follow instructions!");}}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland15Statement Constructors and Nestingif, if-else, {…} are statement constructors They take statement(s) and convert them into a new statement Example:if (i >= 1 && i <= 10)System.out.println("Good job!");elseSystem.out.println(“Oops!"); Two “sub-statements” come in A single big statement (if … else …) comes out Implications: if statements, etc. can also appear inside (“be nested within”) one anotherCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland16Example 10public class Example10 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("Enter an integer: ");int i = sc.nextInt();if (i < 0) {System.out.println("I don't like negative numbers!");if (i < -100)System.out.println("Also... that one is REALLY negative!");}else
View Full Document