COMP 14 Introduction to Programming Adrian Ilie July 5 2005 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Writing Selection Statements What Type of Construct To Use Only need to execute additional statements if condition is trueif Need to execute separate additional statements based on if the condition is if else true or false Need to execute different statements nested if else based on multiple conditions Need to execute different statements based on an expression thatswitch evaluates to a char or int 2 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Writing Selection Statements Write the outline of the selection statement keywords curly braces case and break statements for switch Write the expression boolean expression or condition expression or variable evaluating to char or int Write statements that execute based on the condition 3 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Example Write a selection statement that sets grade to F if score is less than 60 and sets grade to P if score is greater than or equal to 60 1 Choose construct if else 2 Write outline if else 4 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Example Write a selection statement that sets grade to F if score is less than 60 and sets grade to P if score is greater than or equal to 60 3 Add condition if score 60 else 5 Adrian Ilie 4 Add statements if score 60 grade F else grade P The UNIVERSITY of NORTH CAROLINA at CHAPEL Questions What type of selection statement should be used 1 Print Male if gender is M and Female if gender is F if else 2 Print the state associated with areaCode where areaCode switch is an int 3 Print the maximum of three integers nested if else 6 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Question Assume that the lengths of 3 sides of a triangle are given in the integer variables side1 side2 and side3 Write code that will print Equilateral if all three sides are equal Isosceles if only two sides are equal Scalene if no sides are equal equilateral isosceles scalene First what selection construct should be used nested if else 7 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Answer side1 side2 and side3 are lengths of the sides of a triangle if side1 side2 side2 side3 side1 side3 no sides are equal System out println Scalene else if side1 side2 side2 side3 all sides are equal System out println Equilateral else two sides are equal System out println Isosceles 8 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Today Repetition statements while loops for loops 9 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Why Is Repetition Needed Want to add 5 integers to find their average 1 2 3 4 5 10 declare a variable for each integer initialize the sum read in the user input one at a time sum the numbers take the average Want to add 1000 integers to find their average declare one variable for input initialize the sum read in one line of user input add to the sum repeat steps 3 and 4 take the average 1000 times Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Repetition Statements Allow us to execute a statement multiple times Often referred to as loops Controlled by boolean expressions like selection or conditional statements Java has three kinds of repetition statements the while loop the for loop the do loop 11 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Loops Must use a loop control variable controls how many times to loop 4 Parts to Every Loop initialization set loop control variable before condition condition when to stop update change the loop control variable body actions to repeat 12 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Typical Uses of Loops Repeat a section of code a specified number of times counter controlled Repeat a section of code reading input until a specific value is read sentinelcontrolled Repeat a section of code reading input until a valid value is entered input validation Repeat a section of code reading from a file until the end of the file is reached EOFcontrolled Repeat a section of code until a boolean variable becomes false flag controlled 13 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The while Loop Syntax while condition while is a reserved word loop body If the condition is true the loop body is executed Then the condition is evaluated again The loop body is executed repeatedly until the condition becomes false 14 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The while Loop Syntax while expression statement Expression is always true in an infinite loop Statements must change value of expression to false 15 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The while Loop Example final int LIMIT 3 int count 0 boolean condition initializatio n count LIMIT while System out println count count loop body update Output 0 1 2 All done System out println All done 16 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The while Loop What s Going On final int LIMIT 3 int count 0 LIMIT 3 count 0 1 while count LIMIT 2 3 System out println count Output count 0 1 2 System out println All done All done 17 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The while Loop final int LIMIT 3 int count 0 while count LIMIT count System out println count Output 1 2 3 All done System out println All done 18 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The while Loop final int LIMIT 3 int count 0 while count LIMIT System out println count System out println All done 19 Adrian Ilie Output 0 0 0 0 0 0 0 0 0 0 The UNIVERSITY of NORTH CAROLINA at CHAPEL The while Loop final int LIMIT 3 int count 0 while count LIMIT System out println count count Output 0 1 2 3 All done System out println All done 20 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL The while Loop If the condition of a while statement is false initially the loop body is never executed The body of a while loop will execute zero or more times 21 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Counter Controlled while Loop Used when we know exactly the number of times to execute the loop Basic Form counter 0 while counter N counter N is the number of times the loop should execute 22 Adrian Ilie The UNIVERSITY of NORTH CAROLINA at CHAPEL Example final int NUM STUDENTS 100 int sum 0 double average int i 0 initialize loop control var Often we use i j k as counter variable names while i NUM STUDENTS sum Integer parseInt inFile readLine i update loop control variable average double sum
View Full Document