Lecture 6 If Else If and Loops Last time 1 Finish Scanner 2 if statements Today 1 Project assigned 2 Named constants in Java 3 More on if 4 Loops 9 13 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Project 1 Is Assigned You may see the assignment on the CMSC 131 web site click Projects link It is due Tuesday 9 19 at 11 pm The project is open You may discuss solution ideas with other students as well as TAs instructors etc You should document outside help using Java comments at top of your solution file No code copying allowed Full policy is available from web site CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Hints for Success Start now Read entire assignment from beginning to end before starting to code Check out assignment from CVS now rather than later Follow the instructions exactly as much of grading is automated CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 From the Project Description You must use named constants for any literal values that will not change during program execution You must use meaningful variable names and good indentation CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 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 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 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 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 Examples final int MIN OK TEMP 97 final int MAX OK TEMP 99 if temp MIN OK TEMP temp MAX OK TEMP final String INT PROMPT Enter integer System out print INT PROMPT CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 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 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 Variable Name Conventions 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 2006 Rance Cleaveland 2006 University of Maryland 8 Dangle Else IfElseExample1 public class IfElseExample1 public static void main String args int x 4 if x 3 if x 2 System out println X else System out println Y CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 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 used CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 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 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 IfElseExample2 public class IfElseExample2 public static void main String args Scanner scanner new Scanner System in System out print Choose a number from 1 to 4 int choice scanner nextInt String nameOfValue if choice 1 nameOfValue ONE else if choice 2 nameOfValue TWO else if choice 3 nameOfValue THREE else nameOfValue FOUR System out println You chose nameOfValue CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12 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 Fall 2006 Rance Cleaveland 2006 University of Maryland 13 IfElseExample2BetterVersion public class IfElseExample2BetterVersion public static void main String args Scanner scanner new Scanner System in System out print Choose a number from 1 to 4 int choice scanner nextInt String nameOfValue if choice 1 nameOfValue ONE else if choice 2 nameOfValue TWO else if choice 3 nameOfValue THREE else nameOfValue FOUR System out println You chose nameOfValue CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 14 A Common Programming Idiom Idiom convention if C1 S1 else if C2 S2 else Sn Note indentation convention CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 15 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 Fall 2006 Rance Cleaveland 2006 University of Maryland 16 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 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 17 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 Fall 2006 Rance Cleaveland 2006 University of Maryland 18 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
View Full Document