if Statements ICS 111 Introduction to Computer Science

  • Slides: 21
Download presentation
if Statements • ICS 111: Introduction to Computer Science I – William Albritton •

if Statements • ICS 111: Introduction to Computer Science I – William Albritton • Information and Computer Sciences Department at the University of Hawai‘i at Mānoa • “I do not fear computers. I fear the lack of them. ” – Isaac Asimov 10/2/2020 © 2007 William Albritton 1

Terminology • Boolean – Models truth values • Either true or false – Examples

Terminology • Boolean – Models truth values • Either true or false – Examples • Boolean sushi. Is. Fresh = true; sushi. Is. Fresh = false; • Boolean ipod. Is. On = false; ipod. Is. On = true; – Named in honor of British logician George Boole (1815 -1864) • See example code at Boolean. Examples. java 10/2/2020 © 2007 William Albritton 2

Boolean Expression • A combination of operators & operands that returns true or false

Boolean Expression • A combination of operators & operands that returns true or false – Used in selection (if) & repetition (loop) statements – Evaluated from left to right – Operands are what the operators “operate” upon • 3 Kinds of Boolean operators 1. Equality operators: ==, != 2. Relational operators: >, >=, <, <= 3. Logical operators: &&, ||, ! (and, or, not) 10/2/2020 © 2007 William Albritton 3

Equality Operators • Equal or not equal – ==, != • Evaluated left to

Equality Operators • Equal or not equal – ==, != • Evaluated left to right • Returns true or false • Operands can be any primitive data type • Example code – Boolean is. Equal = (5 == 5); //is. Equal is true is. Equal = (7 != 5); //is. Equal is true 10/2/2020 © 2007 William Albritton 4

== And equals() For Objects • The equals() method is a boolean method –

== And equals() For Objects • The equals() method is a boolean method – Returns true or false based on the object’s data fields (values) • The == operator is a boolean operator – Returns true or false based on the object’s address • See example code for how the results may differ, depending on which you use – In Boolean. Examples. java, see method 3() • You only have to think about the differences when both operators are variables with address to objects 10/2/2020 © 2007 William Albritton 5

Relational Operators • Greater than, greater than or equal, less than or equal –

Relational Operators • Greater than, greater than or equal, less than or equal – >, >=, <, <= • Evaluated left to right • Returns true or false • Operands can be any primitive data type • Example code – Boolean is. Less = (5 < 5); //is. Equal is false Boolean is. Less. Or. Equal = (5 <= 5); //is. Less. Or. Equal is true 10/2/2020 © 2007 William Albritton 6

Logical Operators • and, or, not – &&, ||, ! • Evaluated left to

Logical Operators • and, or, not – &&, ||, ! • Evaluated left to right • Returns true or false • Operands have to be Boolean 10/2/2020 © 2007 William Albritton 7

Logical Operators • Logical and (&&) – Has 2 operands – Returns true if

Logical Operators • Logical and (&&) – Has 2 operands – Returns true if both operands are true, otherwise false • Logical or (||) – Has 2 operands – Returns true if at least one operands is true, otherwise false • Logical not (!) – Has only 1 operand – Returns the opposite of the operand 10/2/2020 © 2007 William Albritton 8

Logical Operators • Example code – Boolean bool 1 = (3 != 3) &&

Logical Operators • Example code – Boolean bool 1 = (3 != 3) && (1 <= 4); //false Boolean bool 2 = (!true) || (1. 1 < 2. 2); //true Boolean bool 3 = !(bool 1 && bool 2); //true Boolean bool 4 = (bool 1 || false) || ((bool 2 && bool 3) && (!bool 3)); //false 10/2/2020 © 2007 William Albritton 9

Class Exercise 1 • What is the output of the following code? – See

Class Exercise 1 • What is the output of the following code? – See Exercise 1. java • Try to solve it on your own • Then discuss the problem with your fellow students 10/2/2020 © 2007 William Albritton 10

Terminology • if statement – Uses a Boolean expression to choose which statements to

Terminology • if statement – Uses a Boolean expression to choose which statements to execute • Also called “conditional statement”, or “selection statement” 10/2/2020 © 2007 William Albritton 11

if Statements • Syntax – if(Boolean expression){ //code to be executed } • How

if Statements • Syntax – if(Boolean expression){ //code to be executed } • How does it work? – First, the Boolean expression is evaluated – If it is true, then the body (code within the curly brackets) gets executed – If it is false, then the body of the if statement DOES NOT get executed 10/2/2020 © 2007 William Albritton 12

REMEMBER THIS • BOOLEAN values (true or false) MUST be used inside the parentheses

REMEMBER THIS • BOOLEAN values (true or false) MUST be used inside the parentheses of “if statements” • Code example – See Simple. If. Statements. java • Here are two examples of very simple if statements • This program doesn’t do much • This program is just trying to emphasize that BOOLEAN values (true or false) MUST be used inside the parentheses of “if statements” 10/2/2020 © 2007 William Albritton 13

Using if Statements • Typically, we use “if statements” to make choices • Code

Using if Statements • Typically, we use “if statements” to make choices • Code example – See Spam. Musubi. java • Note that the code in only one of the “if statement” bodies executed each time the program is run • Also note that the last statement, which is outside all the “if statement” bodies, is always executed 10/2/2020 © 2007 William Albritton 14

Class Exercise 2 • Implement the following algorithm – Ask the user to input

Class Exercise 2 • Implement the following algorithm – Ask the user to input two integers – Check to see if the 1 st integer is larger, the 2 nd integer is larger, or if they are equal – Output an appropriate message 10/2/2020 © 2007 William Albritton 15

Scope • Similar to methods, the scope for variables declared within an if statement

Scope • Similar to methods, the scope for variables declared within an if statement body is limited from the variable declaration to the closing right curly bracket ("}") – These variables are also called “local variables” – See Scope. Example. java • Local variables radius & area are not visible outside the if statement body 10/2/2020 © 2007 William Albritton 16

Bug Using == Or equals() • An easy mistake to make is using the

Bug Using == Or equals() • An easy mistake to make is using the equality operator (==) instead of the equals operator to compare objects – Double x = new Double(3. 0); – Double y = new Double(3. 0); – (x == y) returns false • This compares the address of the two objects – (x. equals(y)) returns true • This compares the values stored within the two objects 10/2/2020 © 2007 William Albritton 17

Class Exercise 3 • See Exercise 3. java – What is the output of

Class Exercise 3 • See Exercise 3. java – What is the output of the program? – What are the local variables? – What is the scope of each local variable? 10/2/2020 © 2007 William Albritton 18

Nested If Statements • You can put it if statements within if statements –

Nested If Statements • You can put it if statements within if statements – Don’t forget to match the opening & closing curly brackets • if(Boolean expression. A){//A open //code to be executed if(Boolean expression. B){//B open //code to be executed }//B close }//A close – See example Nested. java 10/2/2020 © 2007 William Albritton 19

if Statement Bugs • Common programming errors – Forgetting to match the left &

if Statement Bugs • Common programming errors – Forgetting to match the left & right parenthesis • if(x%2)==0) // error! • if((x%2)==0) //correct – Using an expression other than boolean (true or false) • if(x%2) //error! • if((x%2)==0) //correct 10/2/2020 © 2007 William Albritton 20

if Statement Bugs II • Common programming errors – Forgetting to match the left

if Statement Bugs II • Common programming errors – Forgetting to match the left & right curly brackets • if(x==0){ System. out. println("Zero"); //error! • if(x==0){ System. out. println("Zero"); } //correct • Avoid these bugs by using the “Generate CSD” button as often as possible – You should check after writing every 2 -3 lines of code 10/2/2020 © 2007 William Albritton 21