Lecture 6 If Else If and Loops Last time 1 Finish Scanner 2 if statements Today 1 More on if 2 Project assigned 3 Named constants in Java 4 Loops CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr Nested If Elses can be Ugly and Confusing Too many lines with only one Easy to get lost in indentation However we can use Java s innermost if rule for elses to program more clearly CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 1 The Dangling Else Problem Which if an else is associated with can be ambiguous Java rule else is associated with innermost possible if Good programming practice when in doubt use WE WILL USE FOR ALL IF IF ELSE IF ELSE IF STATEMENTS CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 2 Cascading Elses A common programming paradigm if something is true do one thing otherwise if something else is true do another thing otherwise if something else entirely is true do yet another thing otherwise take a default action How might we program this CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 3 A Common Programming Idiom Idiom convention if C1 S1 else if C2 S2 else Sn Note indentation and curly bracket conventions CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 4 In Projects You must use meaningful variable names and good indentation Java convention indenting showing the purpose braces in the correct places with respect to the lines Java convention of capitalization of identifiers Fully blocked if statements Lines less than or equal to 80 columns You must use named constants for any literal values that will not change during program execution CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 5 Variable Name Conventions What is legal for variable names Letters digits Can t start variable name with digit Avoid reserved words Use camelCase Variables Methods use lower case for first letter Classes Interfaces use upper case for first letter CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 6 Variable Name Conventions Examples 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 Spring 2007 Jan Plane adapted from Bonnie Dorr 7 Meaningful Variable Names Choose names for your variables to reflect their purpose Bad String string System out println Enter name string sc next if string equals John Doe Good String name System out println Enter name name sc next if name equals John Doe CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 8 Named Constants in Java Programs often contain literals values e g if temp 97 temp 99 e g System out print Enter integer If same value should be used in several places how to ensure consistency Check on temperature may be performed more than once Same prompt might be printed in several places Java answer named constants CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 9 Named Constants final int MAX OK TEMP 99 Just like a regular variable declaration except Special term final Necessity of initial value Any variable name will work but convention is to use all capitals Difference with regular values assignment attempt leads to error CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 10 Examples final int MIN OK TEMP final int MAX OK TEMP if temp MIN OK TEMP temp MAX OK TEMP 97 99 final String INT PROMPT Enter integer System out print INT PROMPT CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 11 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 We will study while do while now for loop later CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 12 while and do while Loops while and do while loops contain A statement called the body A boolean condition Idea the body is executed as long as the condition is true while loop The condition is tested before each body execution while condition body do while loop The condition is tested after each body execution do body while condition Main difference do while loop bodies always executed at least once because it is bottom tested rather than top tested CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 13 Example 11 public class Example11 public static void main String args int i 1 Loop counter while i 10 System out println i i i 1 Increment of loop counter ensures progress toward loop termination CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 14 Infinite Loops Loops can run forever if condition never becomes false Be careful when programming loops Add statements for termination into loop body first Make sure these statements are at end of body e g while i 10 System out println i i i 1 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 15 Example 11b Lots of Looping public class Example11b public static void main String args int i 1 long total 0 while i 1000000 total total i i i 1 System out println Total is total CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 16 Example 12 do while public class Example12 public static void main String args int i 1 do System out println i i i 1 while i 10 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 17 Variables Blocks and Scoping Variables can be declared anywhere in a Java program When are the declarations active After they are executed Only inside the block in which they are declared Scope rules formalize which variable declaration are active when Global variables scope is entire program Local variables scope is a block CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 18 Example 13 import java util Scanner public class Example13 public static void main String args int i 1 Scanner sc new Scanner System in do System out print Enter an integer from 1 to 10 int answer sc nextInt What is scope of while answer 1 answer 10 this declaration System out println Good job CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 19 Nested Loops while do while are statement
View Full Document