DOC PREVIEW
UMD CMSC 131 - Lecture Set #3

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Lecture Set #3:Conditional and Iterative StructuresControl Structures if branching if / else branching logical operators nesting of control structures proper indenting and spacing conventions java identifier naming conventions named constants while loop do-while loop for loopCMSC 131 Fall 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)Control Flow and Conditionals Control flow: the order in which statements are executed General rule: top to bottom Several Control Structures that change that Conditional statements: permit control flow to be dependent on (true/false) conditions if if-elseCMSC 131 Fall 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)if and if-elseThe if and if-else statements should have the following form:  if (condition) {statements; }  tests the condition if true statement is done; otherwise it is skipped if (condition) {statements1; } else { statements2; }  tests the condition if true, statements1 is done; otherwise statements2 is doneCMSC 131 Fall 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)2Java and White Spacehttp://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.htmlCMSC 131 Fall 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)You can add: carriage returns, spaces, tabswherever you want in JavaProperly used, this makes your program easier to read and understandLogical OperatorsUsed for forming more complex conditions. “and” &&if ( temp >= 97 && temp <= 99 ) {System.out.println( “Patient is healthy” );} “or” ||if ( months >= 3 || miles >= 3000 ) {System.out.println( “Change your oil” );} “not”: !if ( ! phone.equals( “301-555-1212” ) ) {System.out.println( “Sorry, wrong number” );}CMSC 131 Fall 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)Blocks What happens?if (i > 10) i = 10; saturate = true;  Desired: both i, saturate are set only when i > 10 Actual: only the i=10 statement is dependant Only one statement can be associated with if The saturate assignment statement is not part of the if Blocks solve this problemCMSC 131 Fall 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)3Blocks What happens?if (i > 10) i = 10; saturate = true; elsek = 100; Desired: both i, saturate are set only when i > 10 Actual: syntax error Only one statement can be associated with if The saturate assignment statement is not part of the if The else can’t find the if it belongs to Blocks solve this problem alsoCMSC 131 Fall 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)What Blocks Are Blocks are sequences of statements “glued together” into one Form:{<statement 1>;<statement 2>;…} Example:if (i > 10) {i = 10;saturate = true;} else {i = i+1;} if, if-else, {…} are statement constructors They take statement(s) and convert them into a new statement Implications: if statements, etc. can also appear inside (“be nested within”) one anotherCMSC 131 Fall 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)blockblockIssues with if-else Nested If/Elses can be Ugly and Confusing!  indent and block carefully The “Dangling Else” Problem Java rule: else is associated with “innermost” possible if Cascading Elses WE WILL USE { … } FOR ALL IF, IF-ELSE, IF-ELSE-IF, STATEMENTSCMSC 131 Fall 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)4In Projects You must use meaningful variable names it must tell the purpose of that variable – what it is meant to hold it can not have so much abbreviation that only you can read it You must use Java convention indenting and brace placement the indenting show the purpose in nesting with braces in the “Java determined” places with respect to the lines of code Java convention of capitalization of identifiers variables and methods start with lower case classes and interfaces start with upper case variables, methods, classes and interface use camelCase constants are all uppercase with underscores between words You must have “Fully Blocked” if statements and looping structures You must have all lines less than or equal to 80 columns of text You must use "named constants" for any literal values that will not change during program execution.CMSC 131 Fall 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)Named Constants If same value should be used in several places, how to ensure consistency? i.e. Check on temperature may be performed more than once i.e. Same prompt might be printed in several places final int MAX_OK_TEMP = 99; Just like a regular variable declaration/initialization, except… Special term final Necessity of initial value Any valid variable name will work, but convention is to use all capitals Difference from non-final variables: assignment attempt leads to error! literals (= named values)e.g.if (temp >= 212 || temp <= 32) …if (temp >= BOILING || temp <= FREEZING)e.g.System.out.print (“Enter integer: “);System.out.print (PROMPT);CMSC 131 Fall 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)Naming Rules and Conventions What is legal for variable names? Letters, digits, $, _ Can’t start variable name with digit Avoid reserved words Avoid names starting or ending with $ or _ Use camelCase: Variables & Methods use lower-case for first letter Classes/Interfaces use upper-case for first letter Naming Conventions: Standards developed over time.Variables and methods: Start with lowercase, and use uppercase for each new word:dataList2 myFavoriteMartian showMeTheMoneyClass names: Start with uppercase and uppercase for each new word:String JOptionPane MyFavoriteClassNamed constants (variables whose value never changes): All uppercase with underscores between words:MAX_LENGTH DAYS_PER_WEEK BOILING_POINT Make variable names not too long, not too short Bad: crtItm Bad : theCurrentItemBeingProcessed Good : currentItemCMSC 131 Fall 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)5Meaningful Variable Names Choose names for your variables to reflect their purpose not their type Make it readable to someone else Help prevent mistakes in order of the relational operatorsCMSC 131 Fall 2010 Jan Plane and Ben Bederson (adapted from Bonnie Dorr)BadGoodtypedValue == 5menuOption ==


View Full Document

UMD CMSC 131 - Lecture Set #3

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture Set #3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Set #3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Set #3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?