Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decisions, Decisions, Decisions Conditional Statements In Java.

Similar presentations


Presentation on theme: "Decisions, Decisions, Decisions Conditional Statements In Java."— Presentation transcript:

1 Decisions, Decisions, Decisions Conditional Statements In Java

2 Conditional Statements in Java How would you explain to the Java compiler how to do the absolute value function? How would you explain to the Java compiler how to do the absolute value function? Seems simple enough in math Seems simple enough in math All programming languages have some form of decision structure based on logic. All programming languages have some form of decision structure based on logic. The absolute value logic is: The absolute value logic is: If the value is negative, multiply it by -1 If the value is negative, multiply it by -1 In Java it would be written as: In Java it would be written as: if( value < 0 ) { value = -1 * value; }

3 The if statement The syntax for an if statement in Java is: The syntax for an if statement in Java is: if( condition ) { programming statements… } If the condition is true, java begins executing the code in the {}. Otherwise, the code in the {} is skipped If the condition is true, java begins executing the code in the {}. Otherwise, the code in the {} is skipped Note the style!!! Note the style!!! Indent Braces line up

4 Conditional Operators Symbol in JavaMeaning ==Equals !=Not Equal <Less Than <=Less Than or Equal >Greater Than >=Greater Than or Equal Common mistake is to use = (assignment) when you meant == (test if equal)

5 What will the code print? int testValue = 5; if( testValue > 1 ) { System.out.println( “The testValue is > 1” ); } System.out.println( “End of Program” ); The condition, testValue > 1 is true The condition, testValue > 1 is true Code in the {} will be executed Code in the {} will be executed This code prints: This code prints: The testValue is >1 End of Program

6 What will the code print? int testValue = 5; if( testValue < 5 ) { System.out.println( “The testValue is < 5” ); } System.out.println( “End of Program” ); The condition, testValue > 1 is false The condition, testValue > 1 is false Code in the {} will be skipped Code in the {} will be skipped This code prints: This code prints: End of Program

7 What is the output?

8 Exercises with if Copy and paste the code into a new project called IfTest1: Copy and paste the code into a new project called IfTest1: public class IfTest1 { public static void main( String[] args ) { System.out.println( “Enter a your age” ); Scanner input = new Scanner( System.in ); int age = input.nextInt(); final int LISCENCE_AGE = 16; if( age >= LISCENCE_AGE ) { System.out.println( “You are old enough to get your driver’s license” ); } System.out.println( “Thank you” ); }} Run the program and enter some test values. Run the program and enter some test values. Alter the code to print “You are old enough to vote” if the age is greater than or equal to 18 Alter the code to print “You are old enough to vote” if the age is greater than or equal to 18 Alter the code to print “You may pay the child rate” if the age is less than 13 Alter the code to print “You may pay the child rate” if the age is less than 13

9 if … else statements Another programming feature with decisions is the if… else statement Another programming feature with decisions is the if… else statement Suppose you’re charging admission for a theatre and the ticket prices are for children or adults. Suppose you’re charging admission for a theatre and the ticket prices are for children or adults. Adult: age >= 18 Adult: age >= 18 Child: not an adult Child: not an adult We can write this as the following sentence: We can write this as the following sentence: If age is 18 or higher, charge adult price, otherwise charge child price If age is 18 or higher, charge adult price, otherwise charge child price The new keyword in our problem is otherwise which translates in Java to else The new keyword in our problem is otherwise which translates in Java to else

10 In java the code for the movie theatre is: In java the code for the movie theatre is: final double ADULT_PRICE = 10.75; final double CHILD_PRICE = 8.25; int ticketPrice; if( age >= 18 )//this person is an adult { ticketPrice = ADULT_PRICE; } else//this person is not an adult, therefore a child { ticketPrice = CHILD_PRICE; ticketPrice = CHILD_PRICE;} System.out.println( “Your ticket price is: “ + ticketPrice ); if … else statements

11 The syntax for if … else in java is: The syntax for if … else in java is: if( condition ) { programming statements if true }else{ programming statements if false } If the original condition is true, then the code in the {} associated with the if statement is executed If the original condition is true, then the code in the {} associated with the if statement is executed Otherwise, the code is ignored and the code in the {} associated with the else statement is executed Otherwise, the code is ignored and the code in the {} associated with the else statement is executed if … else

12 What Will the Code Print? public class IfElseTest { public static void main( String[] args ) { int hoursPastMidNight = 11; if( hoursPastMidNight <= 12 )//entered a negative! { System.out.println( “It is ” + hoursPastMidNight + “am” ); }else{ hoursPastMidNight -= 12;//convert to 12 hour time System.out.println( “It is ” + hoursPastMidNight + “pm” ); System.out.println( “It is ” + hoursPastMidNight + “pm” );} System.out.println( “End of program” ); }} The first statement is true The first statement is true The code in the {} attached to the if is executed The code in the {} attached to the if is executed The code in the {} attached to the else is ignored The code in the {} attached to the else is ignored The result is: The result is: It is 11am End of Program

13 What Will the Code Print? public class IfElseTest { public static void main( String[] args ) { int hoursPastMidNight = 21; if( hoursPastMidNight <= 12 )//entered a negative! { System.out.println( “It is ” + hoursPastMidNight + “am” ); }else{ hoursPastMidNight -= 12;//convert to 12 hour time System.out.println( “It is ” + hoursPastMidNight + “pm” ); } System.out.println( “End of program” ); }} The first statement is false The first statement is false The code in the {} attached to the if is ignored The code in the {} attached to the if is ignored The code in the {} attached to the else is executed The code in the {} attached to the else is executed The result is: The result is: It is 9pm End of Program

14 What is the output?

15 Example Copy and paste the program into a new project Copy and paste the program into a new project public class IfElseTest { public static void main( String[] args ) { System.out.print( “Please enter a positive integer: “ ); Scanner input = new Scanner( System.in ); int userInput = input.nextInt(); if( userInput < 0 )//entered a negative! { System.out.println( “I said a POSITIVE!” ); System.out.println( “What’s the matter with you!” ); }else{ System.out.println( “Thank you for following the instructions” ); } System.out.println( “End of program” ); }} Compile and run the program Compile and run the program Alter the code to see if a person is old enough to drive, if not, calculate how many years they have to wait and tell them to come back in that many years Alter the code to see if a person is old enough to drive, if not, calculate how many years they have to wait and tell them to come back in that many years Alter the code to check how many times a person has been late to class this year. Give them a mean message if it’s more than 5 times! Alter the code to check how many times a person has been late to class this year. Give them a mean message if it’s more than 5 times!

16 Block Statements In our class I will always use block statements for conditions and loops. In our class I will always use block statements for conditions and loops. A condition or loop with only one statement in the body does not require a block statement: A condition or loop with only one statement in the body does not require a block statement: Ex. Ex. if( age > 16 ) { System.out.println( “You may get your liscence” ); } Ex. Ex. if( age > 16 ) System.out.println( “You may get your liscence” ); Can you spot the error? Can you spot the error? You can omit the block statements if you wish, but it is likely to result in more errors You can omit the block statements if you wish, but it is likely to result in more errors

17 Block Statements Another advantage of block statements is clarity. Another advantage of block statements is clarity. Which if does the else belong to? Which if does the else belong to? If we add braces it is clearer which it belongs to If we add braces it is clearer which it belongs to

18 Fixing Errors

19

20 Tracing by hand

21

22 And finally, if…else if…else What about when there are several conditions that are related? What about when there are several conditions that are related? In order to ensure that only one statement is executed, we need to chain the if’s together. In order to ensure that only one statement is executed, we need to chain the if’s together. The next two slides show an example where the limitations of only having if are illustrated The next two slides show an example where the limitations of only having if are illustrated

23 A Whole Bunch of if’s if( richterScale < 1 ) { System.out.println( “Didn’t even feel it” ); } if( richterScale < 4 ) { System.out.println( “All my dishes broke :(” ); } if( richterScale < 7 ) { System.out.println( “Whole lotta shakin going on!” ); } if( richterScale < 10 ) { System.out.println( “Run for the hills!” ); } What does the code print if richterScale = 8? What does the code print if richterScale = 8? Run for the hills! What does the code print if richterScale = 5? What does the code print if richterScale = 5? Whole lotta shakin going on! Run for the hills! True False True

24 Fixing the previous example if( richterScale < 1 ) { System.out.println( “Didn’t even feel it” ); } else if( richterScale < 4 ) { System.out.println( “All my dishes broke :(” ); } else if( richterScale < 7 ) { System.out.println( “Whole lotta shakin going on!” ); }else{ System.out.println( “Run for the hills!” ); } What does the code print if richterScale = 8? What does the code print if richterScale = 8? Run for the hills! What does the code print if richterScale = 5? What does the code print if richterScale = 5? Whole lotta shakin going on! True, ignore anything else in the group False No if’s were true, so automatically go here

25 if...else if…else Java evaluates EVERY if statement. Java evaluates EVERY if statement. The only exception is when they are joined by the reserved word else The only exception is when they are joined by the reserved word else Java evaluates a sequence of if/else if/else statements until the FIRST one is true. Java evaluates a sequence of if/else if/else statements until the FIRST one is true. If none are true then the else is evaluated If none are true then the else is evaluated Only one set of statements in the group will be evaluated Only one set of statements in the group will be evaluated

26 if...else if…else syntax if( condition ) { statements if true } else if( condition ) { statements if true }…… else if( condition ) { statements if true }else{ statements if all others in the group were false }

27 What is Printed? int age = 21; if( age <= 12 )//12 or younger { System.out.println( "Your cost is $5.50" ); } else if( age <=19 ) //older than 12 but 19 or younger { System.out.println( "Your cost is $8.25" ); } else if( age <= 64 ) //older than 19 but 64 or younger { System.out.println( "Your cost is $13.75" ); } else //older than 64 { System.out.println( "Your cost is $5.50" ); } Your cost is $13.75 Your cost is $13.75 False True, everything else below ignored


Download ppt "Decisions, Decisions, Decisions Conditional Statements In Java."

Similar presentations


Ads by Google