Top Banner
Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1
125

Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

Dec 28, 2015

Download

Documents

Claude Carr
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

1

Boolean Expressions and Conditionals (If Statements)

CSE 1310 – Introduction to Computers and ProgrammingVassilis Athitsos

University of Texas at Arlington

Page 2: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

2

The boolean Type• Expressions of type boolean can only have two

values: true, or false.– true and false are reserved keywords in Java.

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0; boolean v1 = (a < 4.3); System.out.printf("v1 = %b\n", v1);

boolean v2 = (a == b); System.out.printf("v2 = %b\n", v2);

boolean v3 = (a != b); System.out.printf("v3 = %b\n", v3); }}

Output:

v1 = truev2 = falsev3 = true

Page 3: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

3

Comparisons of Numbers• The following operators compare numerical values (of type

double or int), and generate boolean results:

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0; System.out.printf("a = %.1f, b = %.1f\n", a,

b); System.out.printf("a == b: %b\n", a == b); System.out.printf("a != b: %b\n", a != b); System.out.printf("a > b: %b\n", a > b); System.out.printf("a >= b: %b\n", a >= b); System.out.printf("a < b: %b\n", a < b); System.out.printf("a <= b: %b\n", a <= b); }}

Output:

a = 3.2, b = 4.0a == b: falsea != b: truea > b: falsea >= b: falsea < b: truea <= b: true

Operator Meaning Operator Meaning

== equals != not equal to

> greater than >= greater than or equal to

< less than <= less than or equal to

Page 4: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

4

Using Parentheses• When you assign a boolean variable, use parentheses to make

it easy to read your code.• Even if your code runs correctly without parentheses,

parentheses are still recommended to make sure you don't get confused.

• Example: setting c equal to the value of "a equals b".

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean c = a == b;

boolean d = (a == b); }}

Preferred style(parenthesize)

Correct, but confusing(not recommended!)

Page 5: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

5

Using Parentheses• When you assign a boolean variable, use parentheses to make

it easy to read your code.• Even if your code runs correctly without parentheses,

parentheses are still recommended to make sure you don't get confused.

• Example: setting c equal to the value of "a equals b".

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean c = a == b;

boolean d = (a == b); }}

Preferred style(parenthesize)

Correct, but confusing(not recommended!)

What is the value of c in this example?

What is the value of d in this example?

Page 6: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

6

Using Parentheses• When you assign a boolean variable, use parentheses to make

it easy to read your code.• Even if your code runs correctly without parentheses,

parentheses are still recommended to make sure you don't get confused.

• Example: setting c equal to the value of "a equals b".

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean c = a == b;

boolean d = (a == b); }}

Preferred style(parenthesize)

Correct, but confusing(not recommended!)

What is the value of c in this example?

What is the value of d in this example?

They are both equal to false3.2 is NOT equal to 4.0.

Page 7: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

7

Comparing Numbers: Examples• Four ways of doing the same comparison (3.2 < 4.0)

– First way:

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a < b); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 = ???

Page 8: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

8

Comparing Numbers: Examples• Four ways of doing the same comparison (3.2 < 4.0)

– First way:

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a < b); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 = true

Page 9: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

9

Comparing Numbers: Examples• Four ways of doing the same comparison (3.2 < 4.0)

– Second way:

public class example1 { public static void main(String[] args) { double a = 3.2; boolean v1 = (a < 4.0); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 = true

Page 10: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

10

Comparing Numbers: Examples• Four ways of doing the same comparison (3.2 < 4.0)

– Third way:

public class example1 { public static void main(String[] args) { boolean v1 = (3.2 < 4.0); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 = true

Page 11: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

11

Comparing Numbers: Examples• Four ways of doing the same comparison (3.2 < 4.0)

– Fourth way:

public class example1 { public static void main(String[] args) { System.out.printf("v1 = %b\n", 3.2 < 4.0); }}

Output:

v1 = true

Page 12: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

12

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a < 4.3 - 2.6); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 =

Page 13: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

13

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a < 4.3 - 2.6); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 = false

Page 14: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

14

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a > 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 =

Page 15: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

15

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a > 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 = false

Page 16: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

16

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a >= 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 =

Page 17: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

17

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a >= 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 = true

Page 18: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

18

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a < 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 =

Page 19: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

19

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a < 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 = false

Page 20: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

20

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a <= 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 =

Page 21: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

21

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a <= 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 = true

Page 22: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

22

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a != 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 =

Page 23: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

23

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a != 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 = false

Page 24: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

24

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a == 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 =

Page 25: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

25

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a == 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

v1 = true

Page 26: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

26

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a = 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

Page 27: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

27

Comparing Numbers: Examples

public class example1 { public static void main(String[] args) { double a = 3.2; double b = 4.0;

boolean v1 = (a = 3.2); System.out.printf("v1 = %b\n", v1); }}

Output:

Error (does not run, we need == sign instead of = sign.

Very common error!!!

Page 28: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

28

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "hello"; boolean r = a.equals(b); System.out.printf("r = %b\n", r); }}

Output:

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Page 29: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

29

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "hello"; boolean r = a.equals(b); System.out.printf("r = %b\n", r); }}

Output:

r = true

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Page 30: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

30

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "Robert"; String b = a.substring(0, 3); boolean r = (b == "Rob"); System.out.printf("r = %b\n", r); }}

Output:

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Page 31: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

31

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "Robert"; String b = a.substring(0, 3); boolean r = (b == "Rob"); System.out.printf("r = %b\n", r); }}

Output:

r = false

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Very common bug!!!How do we fix it?

Page 32: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

32

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "Robert"; String b = a.substring(0, 3); boolean r = b.equals("Rob"); System.out.printf("r = %b\n", r); }}

Output:

r = true

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Very common bug!!!How do we fix it?Use b.equals("Rob")

Page 33: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

33

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "Hello"; boolean r = a.equals(b); System.out.printf("r = %b\n", r); }}

Output:

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Page 34: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

34

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "Hello"; boolean r = a.equals(b); System.out.printf("r = %b\n", r); }}

Output:

r = false

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

String comparisons arecase sensitive!!!

Page 35: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

35

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "world"; boolean r = (a.compareTo(b) < 0); System.out.printf("r = %b\n", r); }}

Output:

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Page 36: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

36

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "world"; boolean r = (a.compareTo(b) < 0); System.out.printf("r = %b\n", r); }}

Output:

r = true

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

hello comes beforeworld

Page 37: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

37

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "World"; boolean r = (a.compareTo(b) < 0); System.out.printf("r = %b\n", r); }}

Output:

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Page 38: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

38

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "World"; boolean r = (a.compareTo(b) < 0); System.out.printf("r = %b\n", r); }}

Output:

r = false

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Upper case letters comebefore lower case letters

Page 39: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

39

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "hello"; boolean r = (a.compareTo(b) == 0); System.out.printf("r = %b\n", r); }}

Output:

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Page 40: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

40

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "hello"; boolean r = (a.compareTo(b) == 0); System.out.printf("r = %b\n", r); }}

Output:

r = true

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

This is the same as doing:a.equals(b)

Page 41: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

41

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "Hello"; boolean r = (a.compareTo(b) == 0); System.out.printf("r = %b\n", r); }}

Output:

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Page 42: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

42

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "Hello"; boolean r = (a.compareTo(b) == 0); System.out.printf("r = %b\n", r); }}

Output:

r = false

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Again: comparisons are case sensitive, andUpper case letters comebefore lower case letters

Page 43: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

43

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "Hello"; boolean r = (a.compareTo(b) < 0); System.out.printf("r = %b\n", r); }}

Output:

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Page 44: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

44

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "Hello"; boolean r = (a.compareTo(b) < 0); System.out.printf("r = %b\n", r); }}

Output:

r = false

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Upper case letters comebefore lower case letters

Page 45: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

45

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "Hello"; boolean r = (a.compareTo(b) > 0); System.out.printf("r = %b\n", r); }}

Output:

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Page 46: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

46

Comparisons of Strings• The following operators compare strings:

• Java's version of alphabetical order: upper case letters come before lower case letters.

public class example1 { public static void main(String[] args) { String a = "hello"; String b = "Hello"; boolean r = (a.compareTo(b) > 0); System.out.printf("r = %b\n", r); }}

Output:

r = true

Syntax Meaning

a.equals(b) a equals b

a.compareTo(b) < 0 a comes before b in Java's version of alphabetical order

a.compareTo(b) == 0 a equals b

a.compareTo(b) > 0 a comes after b in Java's version of alphabetical order

Upper case letters comebefore lower case letters

Page 47: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

47

Logical Operators• The following logical operators can be used to

produce boolean results:

Syntax Meaninga || b a OR ba && b a AND b!a NOT a

Page 48: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

48

Truth Tables for or, and, notOR AND

a b a || b a && b

True True True True

True False True False

False True True False

False False False False

NOT

A !A

True False

False True

Page 49: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

49

Truth Tables for or, and, notOR AND

a b a || b a && b

True True True True

True False True False

False True True False

False False False False

NOT

A !A

True False

False True

public class example1 { public static void main(String[] args) { int x = 3; int y = 5; boolean m = (x == 3) && (y < 10); System.out.printf("m = %b\n", m); boolean n = (x == 3) && (y > 10); System.out.printf("n = %b\n", n); }}

Output:

Page 50: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

50

Truth Tables for or, and, notOR AND

a b a || b a && b

True True True True

True False True False

False True True False

False False False False

NOT

A !A

True False

False True

public class example1 { public static void main(String[] args) { int x = 3; int y = 5; boolean m = (x == 3) && (y < 10); System.out.printf("m = %b\n", m); boolean n = (x == 3) && (y > 10); System.out.printf("n = %b\n", n); }}

Output:

m = truen = false

Page 51: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

51

Truth Tables for or, and, notOR AND

a b a || b a && b

True True True True

True False True False

False True True False

False False False False

NOT

A !A

True False

False True

public class example1 { public static void main(String[] args) { int x = 3; int y = 5; boolean m = (x == 3) || (y < 10); System.out.printf("m = %b\n", m); boolean n = (x == 3) || (y > 10); System.out.printf("n = %b\n", n); }}

Output:

Page 52: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

52

Truth Tables for or, and, notOR AND

a b a || b a && b

True True True True

True False True False

False True True False

False False False False

NOT

A !A

True False

False True

public class example1 { public static void main(String[] args) { int x = 3; int y = 5; boolean m = (x == 3) || (y < 10); System.out.printf("m = %b\n", m); boolean n = (x == 3) || (y > 10); System.out.printf("n = %b\n", n); }}

Output:

m = truen = true

Page 53: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

53

Truth Tables for or, and, notOR AND

a b a || b a && b

True True True True

True False True False

False True True False

False False False False

NOT

A !A

True False

False True

public class example1 { public static void main(String[] args) { int x = 3; int y = 5; boolean m = !(x == 3); System.out.printf("m = %b\n", m); boolean n = !(x == 4); System.out.printf("n = %b\n", n); }}

Output:

Page 54: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

54

Truth Tables for or, and, notOR AND

a b a || b a && b

True True True True

True False True False

False True True False

False False False False

NOT

A !A

True False

False True

public class example1 { public static void main(String[] args) { int x = 3; int y = 5; boolean m = !(x == 3); System.out.printf("m = %b\n", m); boolean n = !(x == 4); System.out.printf("n = %b\n", n); }}

Output:

m = falsen = true

Page 55: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

55

Truth Tables for or, and, notOR AND

a b a || b a && b

True True True True

True False True False

False True True False

False False False False

NOT

A !A

True False

False True

public class example1 { public static void main(String[] args) { int x = 3; int y = 5; boolean m = ((x == y) && (x + y == 8)); System.out.printf("m = %b\n", m); boolean n = ((x == y) || (x + y == 8)); System.out.printf("n = %b\n", n); }}

Output:

Page 56: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

56

Truth Tables for or, and, notOR AND

a b a || b a && b

True True True True

True False True False

False True True False

False False False False

NOT

A !A

True False

False True

public class example1 { public static void main(String[] args) { int x = 3; int y = 5; boolean m = ((x == y) && (x + y == 8)); System.out.printf("m = %b\n", m); boolean n = ((x == y) || (x + y == 8)); System.out.printf("n = %b\n", n); }}

Output:

m = falsen = true

Page 57: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

57

Complicated Use of Operators• What does this code print?

public class example1 { public static void main(String[] args) { boolean m = (3 == 5) && (2 < 3) || (3 >= 0); System.out.printf("m = %b\n", m); }}

Page 58: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

58

Complicated Use of Operators• What does this code print?

• I don't know, and I don't want to know.– No need to memorize complex rules to predict this type of

behavior.– Use parentheses to make the meaning clear.

((3 == 5) && (2 < 3)) || (3 >= 0) true(3 == 5) && ((2 < 3) || (3 >= 0)) false

public class example1 { public static void main(String[] args) { boolean m = (3 == 5) && (2 < 3) || (3 >= 0); System.out.printf("m = %b\n", m); }}

Page 59: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

59

Conditionals - if statements• An if statement looks like this:

if (boolean){ if-line 1; if-line 2; … if-line m;}else{ else-line 1; else-line 2; … else-line n;}

• Meaning of an if statement: – if boolean is true, execute:if-line 1;if-line 2; …if-line n;and skip the else-lines.

– Otherwise, skip the if-lines, and execute:

else-line 1;else-line 2; …else-line n;

Page 60: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

60

An example of an if statementimport java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if (age < 21) { System.out.printf("How about some milk?\n"); } else { System.out.printf("How about some beer?\n"); } }}

Example Output 1:

How old are you? 18

Example Output 3:

How old are you? 24

Example Output 2:

How old are you? 21

Page 61: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

61

An example of an if statementimport java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if (age < 21) { System.out.printf("How about some milk?\n"); } else { System.out.printf("How about some beer?\n"); } }}

Example Output 1:

How old are you? 18How about some beer?

Example Output 3:

How old are you? 24How about some beer?

Example Output 2:

How old are you? 21How about some beer?

Page 62: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

62

import java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if ((age >= 40) && (age <= 60)) { System.out.printf("You are middle aged.\n"); System.out.printf("You are not young.\n"); System.out.printf("You are not old.\n"); } }}

Example Output 1:

How old are you? 18

Example Output 2:

How old are you? 45

Another example of an if statement

Page 63: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

63

Another example of an if statementimport java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if ((age >= 40) && (age <= 60)) { System.out.printf("You are middle aged.\n"); System.out.printf("You are not young.\n"); System.out.printf("You are not old.\n"); } }}

Example Output 1:

How old are you? 18

Example Output 2:

How old are you? 45You are middle aged.You are not young.You are not old.

Note: the else part of an if statement IS OPTIONAL.No else in this example.

Page 64: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

64

Another example of an if statementimport java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if ((age >= 40) && (age <= 60)) { System.out.printf("You are middle aged.\n"); System.out.printf("You are not young.\n"); System.out.printf("You are not old.\n"); } else { System.out.printf("You are not middle aged.\n"); System.out.printf("You are either young or old.\n"); } }}

Example Output 1:

How old are you? 18

Example Output 2:

How old are you? 45

Page 65: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

65

Another example of an if statementimport java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if ((age >= 40) && (age <= 60)) { System.out.printf("You are middle aged.\n"); System.out.printf("You are not young.\n"); System.out.printf("You are not old.\n"); } else { System.out.printf("You are not middle aged.\n"); System.out.printf("You are either young or old.\n"); } }}

Example Output 1:

How old are you? 18You are not middle aged.You are either young or old.

Example Output 2:

How old are you? 45You are middle aged.You are not young.You are not old.

Page 66: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

66

Checking Multiple Casespublic static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if (age < 18) { System.out.printf("You are not an adult.\n"); } else if (age < 40) { System.out.printf("You are a young adult.\n"); } else if (age < 60) { System.out.printf("You are middle aged.\n"); } else { System.out.printf("You are a senior citizen.\n"); } }

Example Output 1:

How old are you? 18

Example Output 3:

How old are you? 65

Example Output 2:

How old are you? 45

Page 67: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

67

Checking Multiple Casespublic static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if (age < 18) { System.out.printf("You are not an adult.\n"); } else if (age < 40) { System.out.printf("You are a young adult.\n"); } else if (age < 60) { System.out.printf("You are middle aged.\n"); } else { System.out.printf("You are a senior citizen.\n"); } }

Example Output 1:

How old are you? 18You are a young adult.

Example Output 3:

How old are you? 65You are a senior citizen.

Example Output 2:

How old are you? 45You are middle aged.

Page 68: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

68

Successive ifs, vs. if-else ifHow will the program behavior change if we remove the else that is highlighted in red?

import java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if (age < 40) { System.out.printf("You are young.\n"); } else if (age < 60) { System.out.printf("You are middle aged.\n"); } else { System.out.printf("You are old.\n"); } }}

Page 69: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

69

Successive ifs, vs. if-else ifHow will the program behavior change if we remove the else that is highlighted in red?

Consider an age of 30.

import java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if (age < 40) { System.out.printf("You are young.\n"); } else if (age < 60) { System.out.printf("You are middle aged.\n"); } else { System.out.printf("You are old.\n"); } }}

Output with else if:

How old are you? 30

Page 70: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

70

Successive ifs, vs. if-else ifHow will the program behavior change if we remove the else that is highlighted in red?

Consider an age of 30.

import java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if (age < 40) { System.out.printf("You are young.\n"); } else if (age < 60) { System.out.printf("You are middle aged.\n"); } else { System.out.printf("You are old.\n"); } }}

Output with else if:

How old are you? 30You are young.

Page 71: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

71

Successive ifs, vs. if-else ifHow will the program behavior change if we remove the else that is highlighted in red?

Consider an age of 30.

import java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if (age < 40) { System.out.printf("You are young.\n"); } if (age < 60) { System.out.printf("You are middle aged.\n"); } else { System.out.printf("You are old.\n"); } }}

Output with else if

How old are you? 30You are young.

Output with two successive if statements:

How old are you? 30

Page 72: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

72

Successive ifs, vs. if-else ifHow will the program behavior change if we remove the else that is highlighted in red?

Consider an age of 30.

import java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if (age < 40) { System.out.printf("You are young.\n"); } if (age < 60) { System.out.printf("You are middle aged.\n"); } else { System.out.printf("You are old.\n"); } }}

Output with else if

How old are you? 30You are young.

Output with two successive if statements:

How old are you? 30You are young.You are middle aged.

Page 73: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

73

Successive ifs, vs. if-else ifThis is an example where using successive if statements, instead of an else if, leads to incorrect behavior.

import java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if (age < 40) { System.out.printf("You are young.\n"); } if (age < 60) { System.out.printf("You are middle aged.\n"); } else { System.out.printf("You are old.\n"); } }}

Output with else if

How old are you? 30You are young.

Output with two successive if statements:

How old are you? 30You are young.You are middle aged.

Page 74: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

74

The Importance of Indentation

This program is indented appropriately.

Every time we open a brace, we increase indentation.

Every time we close a brace, we decrease indentation.

Netbeans does this for you automatically, but may get confused every now and then, and then you need to fix the indentations manually.

import java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt(); if (age < 40) { System.out.printf("You are young.\n"); } else if (age < 60) { System.out.printf("You are middle aged.\n"); } else { System.out.printf("You are old.\n"); } }}

Page 75: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

75

The Importance of Indentation

This program is indented inappropriately.

Indentation does not change program behavior, but makes program harder to read, and mistakes harder to find.

import java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("How old are you? "); int age = in.nextInt();if (age < 40){System.out.printf("You are young.\n"); } if (age < 60) { System.out.printf("You are middle aged.\n"); } else {System.out.printf("You are old.\n");} } }

Page 76: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

76

Indentation on NetBeans• NetBeans can automatically indent your code.• Select Source->Format.• This will work only if your code is valid Java code. If

your code cannot run because of syntax errors, NetBeans may get confused about the correct indentation.

Page 77: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

77

Placement of Braces

public class example1 { public static void main(String[] args) { int a = 7; if (a > 5) { System.out.printf("a > 5.\n"); } }}

public class example1 { public static void main(String[] args) { int a = 7; if (a > 5) { System.out.printf("a > 5.\n"); } }}

First way:{ placed under ifThat is what I usually do.

Second way:{ placed at the end of the if line.This is also fine, if you want to do it that way.

Page 78: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

78

Braces on NetBeans• Source->Format will automatically set the position of

braces for you.• Again, this will work only if your code is valid Java

code, that can run.• You can set some preferences for automatic

formatting using Tools->Options.– Select Editor at the top.– Select Formatting.– In "Category" select braces. You can specify if you want

braces on their own in a new line, or at the end of the current line.

Page 79: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

79

Not Using Bracespublic class example1 { public static void main(String[] args) { int a = 7; if (a > 5) System.out.printf("a > 5.\n"); }}

public class example1 { public static void main(String[] args) { int a = 7; if (a > 5) System.out.printf("a > 5.\n"); }}

These two examples do not use braces under if.

This is legal, but it can lead to bugs when you add more lines.

STRONGLY NOT RECOMMENDED

Page 80: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

80

Not Using Braces - Example of Bugpublic class example1 { public static void main(String[] args) { int a = 3; if (a > 5) System.out.printf("a = %d.\n", a);

System.out.printf("a > 5.\n"); }}

Not using braces under if: it is legal, but it can lead to bugs when you add more lines.

STRONGLY NOT RECOMMENDED

What will this example print?

How many if-lines are there?

Page 81: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

81

Not Using Braces - Example of Bugpublic class example1 { public static void main(String[] args) { int a = 3; if (a > 5) System.out.printf("a = %d.\n", a);

System.out.printf("a > 5.\n"); }}

Not using braces under if: it is legal, but it can lead to bugs when you add more lines.

STRONGLY NOT RECOMMENDED

What will this example print?a > 5

How many if-lines are there?Just one (if you do not use braces under if, there can only be one if-line).

The top example does the same thing as the bottom example.

public class example1 { public static void main(String[] args) { int a = 3; if (a > 5) { System.out.printf("a = %d.\n", a); } System.out.printf("a > 5.\n"); }}

Page 82: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

82

Not Using Braces - Example of Bugpublic class example1 { public static void main(String[] args) { int a = 3; if (a > 5) System.out.printf("a = %d.\n", a);

System.out.printf("a > 5.\n"); }}

Not using braces under if: it is legal, but it can lead to bugs when you add more lines.

STRONGLY NOT RECOMMENDED

If you wanted two if-lines, you should have used braces, as shown on the bottom example on this slide.

public class example1 { public static void main(String[] args) { int a = 3; if (a > 5) { System.out.printf("a = %d.\n", a); System.out.printf("a > 5.\n"); } }}

Page 83: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

83

Another Common BugWhat will this print?public class example1 {

public static void main(String[] args) { int a = 3; if (a > 5); { System.out.printf("a = %d.\n", a); System.out.printf("a > 5.\n"); } }}

Page 84: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

84

Another Common BugWhat will this print?

a = 3.a > 5.

What is the problem?

public class example1 { public static void main(String[] args) { int a = 3; if (a > 5); { System.out.printf("a = %d.\n", a); System.out.printf("a > 5.\n"); } }}

Page 85: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

85

Another Common BugWhat will this print?

a = 3.a > 5.

What is the problem?

Semicolon on the if line.

public class example1 { public static void main(String[] args) { int a = 3; if (a > 5); { System.out.printf("a = %d.\n", a); System.out.printf("a > 5.\n"); } }}

public class example1 { public static void main(String[] args) { int a = 3; if (a > 5) { System.out.printf("a = %d.\n", a); System.out.printf("a > 5.\n"); } }}

The bottom example shows the fixed version.

Page 86: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

86

• Write a program that:– Asks the user to enter the name of

the month.

– Prints"M has X days"where M is the month and X is the correct number of days.

– If the user did not enter a valid month name, the program prints"M is not a valid month"

Conditionals with Strings: An Example

Page 87: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

87

Conditionals with Strings: An Example

import java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter the name of a month: "); String m = in.next(); if ((m.equals("January")) || (m.equals("March")) || (m.equals("May")) || (m.equals("July")) || (m.equals("August")) || (m.equals("October")) || (m.equals("December"))) { System.out.printf("%s has 31 days.\n", m); } else if ((m.equals("April")) || (m.equals("June")) || (m.equals("September")) || (m.equals("November"))) { System.out.printf("%s has 30 days.\n", m); } else if (m.equals("February")) { System.out.printf("%s has 28 or 29 days.\n", m); } else { System.out.printf("%s is not a valid month.\n", m); } }}

Page 88: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

88

The String indexOf Method• Suppose that variables str1 and str2 are strings.• Suppose you want to see if str1 contains str2.• You can call str1.indexOf(str2).• If str1 contains str2, indexOf returns the FIRST

position where str2 appears in str1.• If str1 does NOT contain str2, indexOf returns -1.

Page 89: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

89

indexOf Example• Write a program that:

– Asks the user to enter a single letter.– If the user enters a string with more than one letter, exit

the program.– If the letter is a vowel, print that it is a vowel– Else, print that the letter is not a vowel.

Page 90: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

90

indexOf Example

import java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter a single letter: "); String c = in.next(); if (c.length() != 1) { System.out.printf("invalid input.\n"); System.exit(0); } String vowels = "aeiouAEIOU"; int result = vowels.indexOf(c); if (result != -1) { System.out.printf("%s is a vowel.\n", c); } else { System.out.printf("%s is not a vowel.\n", c); } }}

Page 91: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

91

indexOf Example

import java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter a single letter: "); String c = in.next(); if (c.length() != 1) { System.out.printf("invalid input.\n"); System.exit(0); } String vowels = "aeiouAEIOU"; int result = vowels.indexOf(c); if (result != -1) { System.out.printf("%s is a vowel.\n", c); } else { System.out.printf("%s is not a vowel.\n", c); } }}

Note: if we want the program to finish, we write:

System.exit(0);

Page 92: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

92

Version without indexOf

import java.util.Scanner;

public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter a single letter: ");

String c = in.next(); if (c.length() != 1) { System.out.printf("invalid input.\n"); System.exit(0); }

if (c.equals("a") || c.equals("e") || c.equals("i") || c.equals("o") || c.equals("u") || c.equals("A") || c.equals("E") || c.equals("I") || c.equals("O") || c.equals("U")) { System.out.printf("%s is a vowel.\n", c); } else { System.out.printf("%s is not a vowel.\n", c); } }}

Doable, but painful.

Would be even more painful if you were checking consonants instead of vowels.

Page 93: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

93

Variable Scope• Variables do not live forever.• Failing to take that into account leads to problems.• Let's look at an example.• Let's write a program that:

– Asks the user to enter an integer.– If the integer is >= 0, it creates a variable result and sets it

equal to the square of the integer.– If the integer is < 0, it creates a variable result and sets it

equal to 10 * the integer.– It prints out the value of variable result.

Page 94: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

94

Variable Scope• This version will not

run.• Why?

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 95: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

95

Variable Scope• This version will not

run.• Why? Java will

complain that "it cannot find symbol" result, on the line shown in red.

• How do we fix this?

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 96: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

96

Variable Scope• This version will also

not run.• Why?

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 97: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

97

Variable Scope• This version will also

not run.• Why? Java will

complain that "it cannot find symbol" result, on the line shown in red.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 98: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

98

Lifetime of a Variable• What is the reason

for these errors?• Each variable has a

lifetime. – It is born in a line of

code.– It dies at some point.

• To understand the lifetime of variable, we must understand the concept of a block of code.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 99: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

99

Blocks of Code and Braces• A block of code is a

chunk of code that:– Starts with an

opening brace {.– Ends with the

corresponding closing brace }.

• Example:– What block of

code does the highlighted line belong to?

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 100: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

100

Blocks of Code and Braces• A block of code is a

chunk of code that:– Starts with an

opening brace {.– Ends with the

corresponding closing brace }.

• Example:– What block of

code does the highlighted line belong to?

– The answer is shown in red.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 101: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

101

Blocks of Code and Braces• A block of code is a

chunk of code that:– Starts with an

opening brace {.– Ends with the

corresponding closing brace }.

• Example:– What block of

code does the highlighted line belong to?

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 102: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

102

Blocks of Code and Braces• A block of code is a

chunk of code that:– Starts with an

opening brace {.– Ends with the

corresponding closing brace }.

• Example:– What block of

code does the highlighted line belong to?

– The answer is shown in red.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 103: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

103

Birth of a Variable• Each variable has a

lifetime. – It is born in a line of

code.– It dies at some point.

• Where is a variable born?– At the line where it is

declared.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 104: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

104

Death of a Variable• Where is a variable

born?– At the line where it is

declared.

• Where does a variable die?– Find the innermost

block containing the variable declaration .

– The variable dies at the end of that block.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 105: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

105

Scope of a Variable• Where is a variable

born?– At the line where it is

declared.

• Where does a variable die?– Find the innermost

block containing the variable declaration .

– The variable dies at the end of that block.

• The lines of code where a variable is alive are called the scope of that variable.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 106: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

106

Variable Scope• Where is variable

number born?import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 107: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

107

Variable Scope• Where is variable

number born?– At the line where it is

declared.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 108: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

108

Variable Scope• Where is variable

number born?– At the line where it is

declared.

• Where does variable number die?

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 109: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

109

Variable Scope• Where is variable

number born?– At the line where it is

declared.

• Where does variable number die?– The innermost block

where containing the declaration of number is shown in red.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 110: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

110

Variable Scope• Where is variable

number born?– At the line where it is

declared.

• Where does variable number die?– The innermost block

where containing the declaration of number is shown in red.

– So, number dies when we get outside that block.

– In short, number dies at the end of the program.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 111: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

111

Variable Scope• So, what is the scope

of variable number?import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 112: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

112

Variable Scope• So, what is the scope

of variable number?• It is the lines

between its declaration and its death.– Shown in red.

• It cannot be used before or after those lines.

• This is fine, the scope of number is what it should be.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 113: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

113

Variable Scope• What is the scope of

variable result?import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 114: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

114

Variable Scope• What is the scope of

variable result?• In this code, there

are two independent variables called result.– The first one has its

scope shown in red.– The second one has

its scope shown in green.

• Obviously, none of them is alive at the printf line.– That is why Java

complaints.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 115: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

115

Variable Scope• What is the scope of

variable result in this example?

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 116: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

116

Variable Scope• What is the scope of

variable result in this example?

• It is shown in red on this slide.

• Obviously, in this example, result is not alive either at the else part or at the printf line at the end.– That is why Java

complains.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 117: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

117

Variable Scope• How do we make

this code correct?import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 118: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

118

Variable Scope• How do we make

this code correct?• We need to make

sure that we have a single variable, called result, that is alive:– at the if part.– at the else part.– at the printf

statement at the end.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 119: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

119

Variable Scope• The solution is to

declare result before the if part.

• What is the scope of result now?

import java.util.Scanner; // correct code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); int result; if (number >= 0) { result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 120: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

120

Variable Scope• The solution is to

declare result before the if part.

• What is the scope of result now?

• It is shown in red on this slide.

import java.util.Scanner; // correct code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); int result; if (number >= 0) { result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 121: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

121

Variable Scope• We do not have to

assign a value to result when we declare it.

• Java is sure that, when it is time to print the value at the end, result has received a value.

• How can it be sure?

import java.util.Scanner; // correct code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); int result; if (number >= 0) { result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 122: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

122

Variable Scope• We do not have to

assign a value to result when we declare it.

• Java is sure that, when it is time to print the value at the end, result has received a value.

• How can it be sure?– Because result is

assigned a value both by the if part and by the else part.

import java.util.Scanner; // correct code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); int result; if (number >= 0) { result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }}

Page 123: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

123

Variable Scope• What is going to

happen here?– We removed the else

part.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); int result; if (number >= 0) { result = number * number; } System.out.printf("result = %d\n", result); }}

Page 124: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

124

Variable Scope• What is going to

happen here?– We removed the else

part.

• Java will refuse to run this code.

• Reason: if number < 0, then result never receives a value.

• Before running a program, Java must prove to itself that all variables receive values before they are used.

import java.util.Scanner; // incorrect code

public class example1{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); int result; if (number >= 0) { result = number * number; } System.out.printf("result = %d\n", result); }}

Page 125: Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.

125

More Examples of Conditionals• Determining if integer K is a divisor of integer N.• Determining if a day is a weekend.• Determining if a day is a weekday or a weekend.• Determining if a month is a summer month.• Determining the season of a month.• Determining if a year is a leap year.• Calculating tax.• Translating English to Spanish.

– More accurately: translating a few English words to Spanish.

• Determining the weekday for a date in February 2015.