Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2011 Introduction to Programming I Selections (II)

Similar presentations


Presentation on theme: "CS2011 Introduction to Programming I Selections (II)"— Presentation transcript:

1 CS2011 Introduction to Programming I Selections (II)
Chengyu Sun California State University, Los Angeles

2 Overview Common problems with if/else Logical operators
Conditional operator Switch statement Putting it all together Debugging with Eclipse

3 Common Error #1 if( x > 1 ); System.out.println( "Big X" );
else System.out.println( "Small X" );

4 Common Error #2 if( radius >= 0 ) area = radius * radius * PI;
System.out.println( area );

5 Common Error #3 if( x > y ) if( y > z ) System.out.println("x");
else System.out.println("y");

6 Common Error #4 double x = 1.0-0.1-0.1-0.1-0.1-0.1;
System.out.println(x == 0.5);

7 Bad Style #1 boolean even; if( x%2 == 0 ) even = true; else
even = false;

8 Bad Style #2 if( even == true ) System.out.println("it's even");

9 Bad Style #3 if (inState) { tuition = 5000;
System.out.println("The tuition is " + tuition); } else { tuition = 15000;

10 Bad Style #4 if (x > 10) System.out.println( "Big X" );
System.out.println( "small x" );

11 The Need for More Complex Conditions
A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. condition 1 condition 2 (with a negation) condition 3

12 Logical Operators Operates on boolean values and produces boolean results Operator Name ! Negation && And || Or ^ Exclusive or

13 Negation Operator Turns true to false and false to true ! true ??

14 And Operator The result is true only when both conditions (i.e. operands) are true true && true ?? true && false false && true false && false

15 Or Operator The result is true when either operand is true
true || true ?? true || false false || true false || false

16 Exclusive Or Operator The result is true when the two operands are different true ^ true ?? true ^ false false ^ true false ^ false

17 Logical Operator Examples
Check if a month is in winter (i.e. December, January, or February) Check if a year is a leap year

18 The Conditional Operator
A shorthand form of an if-else statement if ( x > 0 ) y = 1; else y = -1; y = x > 0 ? 1 : -1; boolean expression result if expression is true result if expression is false

19 Conditional Operator Examples
System.out.println((x < y && y < z) ? "sorted" : "not sorted" ); tax = (income > 10000) ? income * 0.2 : income * ; Check out the Operator Precedence Table

20 Switch Statement Branching based on a number of discreet values Winter
1 2 Winter Example: month 3 Spring 4 Spring 12 default Winter Invalid

21 Switch Statement Syntax
switch ( expression ) { case value1: statement(s); break; case value2: ... default: }

22 About Switch Statement Syntax
No need of {} for the statements for a case break is necessary to separate the cases – sometimes you may intentionally leave out break if multiple cases share the same statement(s) default is optional No break is necessary in default (because it's already at the end)

23 Put Everything Together – Lottery Example
Randomly generate a 2-digit number Prompt the user to enter a 2-digit number If the user input matches the lottery number in the exact order, the award is $10,000; otherwise If all digits in the user input match all digits in the lottery number, the award is $3,000; otherwise If one digit in the user input matches a digit in the lottery number, the award is $1,000.

24 Debug with Eclipse Set break points Debug As … Debug Perspective
Variables view Switch between perspectives Common debug commands Step Over and Step Into Terminate and Resume

25 Readings Chapter 3 of the textbook (there will be a quiz next week)


Download ppt "CS2011 Introduction to Programming I Selections (II)"

Similar presentations


Ads by Google