CS256 Computer Science I Kevin Sahr, PhD Lecture 15: The If-Statement 1 Flow of Control the order of statement execution default is top-to-bottom 2 Conditional Statements we can change the flow of control using conditional statements conditional statements let us choose which statement to execute next they include: -statements -else statements switch statements (discussed later) for -statements the choice of what to execute next is determined by a boolean expression known as the -statement condition 3 Conditional Statements for -statements the choice of what to execute next is determined by a boolean expression known as the -statement condition 4
answer width The If-Statement syntax: (condition)» statement; boolean expression condition is true, execute statement. condition is false, skip statement. in either case, continue on with the statements that follow the -statement 5 The If-Statement condition evaluated true false statement 6 double width; = Input.readDouble( Enter width: ); (width < 0.0) Output.showMessage( NEGATIVE WIDTH ); Output.showMessage( I am here. ); user enters a negative width, outputs NEGATIVE NUMBER then I am here. user enters a positive width, outputs just I am here 7 int answer; = Input.readInt( Enter the answer: ); (answer == 42) Output.showMessage( YES! ); Output.showMessage( Here I am. ); user enters 42, outputs YES! then Here I am. otherwise outputs just Here I am. 8
length length length double max = 100.0, length; = Input.readDouble( Enter length: ); (length > max) length = max; Output.showValue( length is now, length); user enters a number greater than max, length is assigned the value max otherwise the value of length is unchanged 9 double max = 100.0, length; = Input.readDouble( Enter length: ); (length > max) length = max; Output.showValue( length is now, length); user enters a number greater than max, length is assigned the value max but shouldn t we tell the user we changed it? requires more than one statement inside the 10 Block Statement a block statement is several statements grouped together with curly braces { } can be used anywhere a single statement can be used for example, inside an -statement remember to put a comment on your } s 11 double max = 100.0, length; = Input.readDouble( Enter length: ); (length > max) Block Statement Output.showMessage( Warning: max length exceeded. ); Output.showValue( Length set to, max); length = max; } // { Output.showValue( length is now, length); user enters a number greater than max, user is warned that a change was made length is assigned the value max 12
Indentation Revisited Remember that indentation is for the human reader, and is ignored by the computer (total > MAX) Ouput.showMessage("Error!!"); errorcount = errorcount + 1; Despite what is implied by the indentation, the third line will execute whether the condition is true or not 13 The If-Else Statement the -statement allows our program to decide whether or not to execute a statement we can choose between executing two dferent statements by adding an else-clause to make an else statement 14 The If-Else Statement syntax: (condition)» statement1; else» statement2; the condition is true, execute statement1. the condition is false, execute statement1. one or the other is executed, never both statement1 and/or statement2 could be block statements 15 The If-Else Statement condition evaluated true false statement1 statement2 16
boolean s see Smaller.java and Payroll.java 17 Boolean Input/Output the Input and Output classes do not provide methods for direct boolean input and output can use an -else for output: b =...; b is a boolean expression (b) Output.showMessage( b is true ); else Output.showMessage( b is false ); 18 String Equals Method EX: String quit = new String( QUIT ); String inp = Input.readString(?: ); (inp.equals(quit)) Output.showMessage( bye ); else Output.showMessage( continuing ); output would depend on the String the user input 19 Person Class the Person is another object class example Person class API, compiled class file Person.class, and an example program PersonApp.java are on the class website spend some time looking at this example 20
Output.showMessage(p1.getName() Output.showMessage(p1.getName() Another Boolean Return Type Note in the Person class API the method: public boolean getisalive() This method has a return type of boolean can be invoked anywhere a boolean expression can be used 21 Boolean Return Type s assume we have created a Person object p1 we can invoke the getisalive method on the right hand side of a boolean assignment statement. EX: boolean isalive = p1.getisalive(); we can invoke the getisalive method in the condition of a conditional statement. EX: (p1.getisalive()) + " is alive."); else + " is dead."); 22 Lecture 15 Vocabulary flow of control conditional statement condition block statement -else statement 23