Week 4 - Decisions

Group statements blocks style rule use indentation to

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

View Full Document Right Arrow Icon
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 different 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

This note was uploaded on 02/17/2014 for the course ITP 109 taught by Professor Trinagregory during the Spring '12 term at USC.

Ask a homework question - tutors are online