COMP 110 Branching Statements and Boolean Expressions Catie
COMP 110 Branching Statements and Boolean Expressions Catie Welsh February 2, 2011 1
Announcements � Program 1 Due Today by 11: 59 pm today � Program 2 Assigned Today � Lab 2 Due Friday by 1: 00 pm 2
Questions? 3
Today in COMP 110 Formatting decimals Review Worksheet If/Else statements Boolean Expressions 4
Formatting Decimals �import java. text. *; �Decimal. Format � df = new Decimal. Format("0. 00"); df. format(my. Variable); � Example code on class website 5
Review Worksheet public class My. Program { public static void main(String[] args) { String my. String = “This is a string”; int len = my. String. length(); System. out. print(“the length is “ + len); String short. String = my. String. substring(10); } } 6
Integer division � Double � int my. Double = (1 / 2) * 5. 0; i = 1 / 2; O 0. 5 7
Classes that mary is an object of class Person, and suppose that increase. Age is a method of class Person that uses one argument, an integer. Write the invocation of the method increase. Age for the object mary using the argument 5. � Suppose Person mary = new Person; mary. increase. Age(5); 8
Flow Chart 9
Flow Chart Check time; if (time < 7 am) { take bus; } else //time >= 7 am { take subway; } Reach school; 10
Java Example public class Flow. Chart { public static void main(String[] args) { System. out. println("Give me an integer: "); Scanner keyboard = new Scanner(System. in); int input. Int = keyboard. next. Int(); Start Prompt User for int NO Is user input greater than 5? Print: “small number” import java. util. *; if( input. Int > 5) { System. out. println("Big number"); } else { System. out. println("Small number"); } YES Print: “big number” } } 11
Java Comparison Operators == != > >= < <= Equal to Not equal to Greater than or equal to Less than or equal to 12
Boolean Expressions �True of False �Example expressions ◦ 5 == 3; ◦ Variable <= 6; ◦ my. Int != temp; �if (boolean expression) { statements } 13
&& (and) � What if you need multiple expressions to be true � (expression) && … ◦ Expressions go in ( ) � Will only be true if ALL statements are true 14
|| (or) � What if you need ONE expression to be true out of many expressions � (expression) || … ◦ Expressions go in ( ) � Will be true if ONE expression is true 15
Boolean Operators � FIGURE 3. 7 The Effect of the Boolean Operators && (and), || (or), and ! (not) on Boolean values
Gotcha == � var 1 = var 2 � var 1 == var 2 (boolean expression) ◦ Error!!!!!!! � Do (assignment statement) NOT use == to compare Strings ◦ string 1 == string 2 //BAD ◦ string 1. equals(string 2); //GOOD 17
Comparing Strings �Syntax String. equals(Other_String) String. equals. Ignore. Case(Other_String)
If Without Else � You can use just an if statement � if (boolean expression) { (statements) } the rest of your code 19
Nested If Else if (boolean expression) { stuff goes here } else { more stuff } } else 20
Pseudocode in Flowchart Format Start Prompt User for int input. Int == 0 What is the integer? input. Int > 1 input. Int == 1 Print: “hello” Print: “how are you” Print: “how may I help you” 21
import java. util. *; public class Flow. Chart { public static void main(String[] args) { System. out. println("Give me an integer: "); Scanner keyboard = new Scanner(System. in); int input. Int = keyboard. next. Int(); if ( input. Int == 0) System. out. println(”hello"); else if ( input. Int == 1) System. out. println(”how are you"); else System. out. println(”how may I help you"); } } 22
Friday � Recitation � Bring Laptop � Bring Book 23
- Slides: 23