Week 4 - Decisions

# Group statements blocks style rule use indentation to

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: tolerance < # < answer + tolerance –  In the previous example we used 0.01 as the tolerance 30 Equality with Strings •  Strings have hidden abilities (methods) –  Accessed with the dot (.) after the identi er –  One is the equals method –  Another is the equalsIgnoreCase method •  Syntax string1.equals(string2) string1.equalsIgnoreCase(string2) 31 Example final String correctUserName = "ttrojan"; String username = ""; Scanner keyboard = new Scanner(System.in); System.out.print("Please enter your name: "); userName = keyboard.nextLine(); if (userName.equals(correctUserName)) { System.out.println(“Valid user name”); } else { System.out.println(“Invalid”); } 32 Nested if- else Statements •  An if- else statement can contain any sort of statement within it •  In particular it can contain another if- else statement –  An if- else may be nested within the if part –  An if- else may be nested within the else part –  An if- else may be nested within both parts 33 Nested Statements if (condition1) { if (condition2) { statement1; } else { statement2; } } else { if (condition3) { statement3; } else { statement4; } } Do this if condition1 is true Do this if condition1 is false 34 Nested Statements •  Each else is paired with the nearest unmatched if •  Braces are used group statements (blocks) •  Style rule –  Use indentation to show which if goes with which else 35 Nested Statements •  Subtly diﬀerent forms int x = 0; int x = 0; if (a > b) { if (c > d) x = 1; } else x = 2; if (a > b) if (c > d) x = 1; else x = 2; 36 Multibranch if- else Statements if (condition1) { statement1;...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern