Lecture Set 3 Conditional and Iterative Structures Control 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 loop CMSC 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 else CMSC 131 Fall 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr if and if else The 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 done CMSC 131 Fall 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 1 Java and White Space You can add carriage returns spaces tabs wherever you want in Java Properly used this makes your program easier to read and understand http java sun com docs codeconv html CodeConv TOC doc html CMSC 131 Fall 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr Logical Operators Used 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 problem CMSC 131 Fall 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 2 Blocks What happens if i 10 i 10 saturate true else k 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 also CMSC 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 block else i i 1 block 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 another CMSC 131 Fall 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr Issues with if else Nested If Elses can be Ugly and Confusing The Dangling Else Problem indent and block carefully Java rule else is associated with innermost possible if Cascading Elses WE WILL USE FOR ALL IF IF ELSE IF ELSE IF STATEMENTS CMSC 131 Fall 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 3 In 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 CMSC 131 Fall 2010 Jan Plane and not change during program execution Ben Bederson adapted from Bonnie Dorr Named Constants If same value should be used in several places how to ensure consistency final int MAX OK TEMP 99 i e Check on temperature may be performed more than once i e Same prompt might be printed in several places 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 Use camelCase Letters digits Can t start variable name with digit Avoid reserved words Avoid names starting or ending with or 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 showMeTheMoney Class names Start with uppercase and uppercase for each new word String JOptionPane MyFavoriteClass Named 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 currentItem CMSC 131 Fall 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr 4 Meaningful 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 operators Bad Good typedValue 5 menuOption 5 integer 13 age 13 input1 45 input2 100 height 45 weight 100 val1 100 val1 999 non3dgt 100 non3dgt 999 CMSC 131 Fall 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr Loops in Java So far our programs execute every program statement at most once Often we want to perform operations more than once Loops allow statements to be executed multiple times Loop types in Java Sum all numbers from 1 to 10 Repeatedly prompt user for input while do while for Call iteration CMSC 131 Fall 2010 Jan Plane and Ben Bederson adapted from Bonnie Dorr while and do while Loops while and do while loops contain A statement called the body A boolean condition Idea the body is executed one more time as long as the condition is true while loop The
View Full Document