1 / 48

CMSC 150 conditional execution

CMSC 150 conditional execution. CS 150: Mon 16 Jan 2012. Sequential Execution. import java.util.Scanner ; public class ProgramFive { public static void main(String [] args ) { Scanner keyboard = new Scanner( System.in ); System.out.println ( “Enter sphere radius:“ );

wyanet
Download Presentation

CMSC 150 conditional execution

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 150conditionalexecution CS 150: Mon 16 Jan 2012

  2. Sequential Execution import java.util.Scanner; public class ProgramFive { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter sphere radius:“ ); double radius = keyboard.nextDouble(); double pi = 3.14159; double volume = (4.0 / 3.0) * pi * radius * radius * radius; System.out.println( “The volume of a sphere with radius “ + radius + “ is “ + volume ); } } Java executes statements step-by-step in exactly the order you have them

  3. Sequential Execution import java.util.Scanner; public class ProgramFive { public static voidmain(String[] args) { Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter sphere radius:“ ); double radius = keyboard.nextDouble(); double pi = 3.14159; double volume = (4.0 / 3.0) * pi * radius * radius * radius; System.out.println( “The volume of a sphere with radius “ + radius + “ is “ + volume ); } }

  4. Sequential Execution import java.util.Scanner; public class ProgramFive { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter sphere radius:“ ); double radius = keyboard.nextDouble(); double pi = 3.14159; double volume = (4.0 / 3.0) * pi * radius * radius * radius; System.out.println( “The volume of a sphere with radius “ + radius + “ is “ + volume ); } }

  5. Sequential Execution import java.util.Scanner; public class ProgramFive { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter sphere radius:“ ); double radius = keyboard.nextDouble(); double pi = 3.14159; double volume = (4.0 / 3.0) * pi * radius * radius * radius; System.out.println( “The volume of a sphere with radius “ + radius + “ is “ + volume ); } }

  6. Sequential Execution import java.util.Scanner; public class ProgramFive { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter sphere radius:“ ); double radius = keyboard.nextDouble(); double pi = 3.14159; double volume = (4.0 / 3.0) * pi * radius * radius * radius; System.out.println( “The volume of a sphere with radius “ + radius + “ is “ + volume ); } }

  7. Sequential Execution import java.util.Scanner; public class ProgramFive { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter sphere radius:“ ); double radius = keyboard.nextDouble(); double pi = 3.14159; double volume = (4.0 / 3.0) * pi * radius * radius * radius; System.out.println( “The volume of a sphere with radius “ + radius + “ is “ + volume ); } }

  8. Sequential Execution import java.util.Scanner; public class ProgramFive { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter sphere radius:“ ); double radius = keyboard.nextDouble(); double pi = 3.14159; double volume = (4.0 / 3.0) * pi * radius * radius * radius; System.out.println( “The volume of a sphere with radius “ + radius + “ is “ + volume ); } }

  9. Sequential Execution import java.util.Scanner; public class ProgramFive { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter sphere radius:“ ); double radius = keyboard.nextDouble(); double pi = 3.14159; double volume = (4.0 / 3.0) * pi * radius * radius * radius; System.out.println( “The volume of a sphere with radius “ + radius + “ is “ + volume ); } }

  10. Sequential Execution import java.util.Scanner; public class ProgramFive { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter sphere radius:“ ); double radius = keyboard.nextDouble(); double pi = 3.14159; double volume = (4.0 / 3.0) * pi * radius * radius * radius; System.out.println( “The volume of a sphere with radius “ + radius + “ is “ + volume ); } }

  11. How Is This Different? import java.util.Scanner; public class ProgramFive { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); double radius = keyboard.nextDouble(); double pi = 3.14159; double volume = (4.0 / 3.0) * pi * radius * radius * radius; System.out.println( “Enter sphere radius:“ ); System.out.println( “The volume of a sphere with radius “ + radius + “ is “ + volume ); } }

  12. How Is This Different? import java.util.Scanner; public class ProgramFive { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); double radius = keyboard.nextDouble(); double pi = 3.14159; double volume = (4.0 / 3.0) * pi * radius * radius * radius; System.out.println( “Enter sphere radius:“ ); System.out.println( “The volume of a sphere with radius “ + radius + “ is “ + volume ); } }

  13. Now, How Is This Different? import java.util.Scanner; public class ProgramFive { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter sphere radius:“ ); double radius = keyboard.nextDouble(); double volume = (4.0 / 3.0) * pi * radius * radius * radius; double pi = 3.14159; System.out.println( “The volume of a sphere with radius “ + radius + “ is “ + volume ); } }

  14. Now, How Is This Different? import java.util.Scanner; public class ProgramFive { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter sphere radius:“ ); double radius = keyboard.nextDouble(); double volume = (4.0 / 3.0) * pi * radius * radius * radius; double pi = 3.14159; System.out.println( “The volume of a sphere with radius “ + radius + “ is “ + volume ); } } Will not compile! Trying to use a variable before it is declared

  15. Find the Runtime Error import java.util.Scanner; public class ExampleProgram { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter an integer:” ); intfirstNumber = keyboard.nextInt(); System.out.println( “Enter another integer:” ); intsecondNumber = keyboard.nextInt(); double ratio = (double) firstNumber / secondNumber; System.out.println( “The ratio is “ + ratio ); } }

  16. Find the Runtime Error import java.util.Scanner; public class ExampleProgram { public static void main(String[] args) { Scanner keyboard = new Scanner( System.in ); System.out.println( “Enter an integer:” ); intfirstNumber = keyboard.nextInt(); System.out.println( “Enter another integer:” ); intsecondNumber = keyboard.nextInt(); double ratio = (double) firstNumber / secondNumber; System.out.println( “The ratio is “ + ratio ); } } potentially…

  17. Conditional Execution // lots of your great code … if ( condition ) { statement; } … // lots more of your great code Java will execute the statement only if the condition is true

  18. Conditional Execution // lots of your great code … double ratio = 0.0; if ( secondNumber != 0 ) { ratio = (double) firstNumber / secondNumber; } … // lots more of your great code a Boolean condition

  19. Conditional Execution // lots of your great code … double ratio = 0.0; if ( secondNumber != 0 ) { ratio = (double) firstNumber / secondNumber; } … // lots more of your great code Java will compute the ratio only if the condition is true (i.e., non-zero second #)

  20. Note The Difference… // lots of your great code … double ratio = 0.0; if ( secondNumber != 0 ) { ratio = (double) firstNumber / secondNumber; } … // lots more of your great code // lots of your great code … if ( secondNumber != 0 ) { double ratio = (double) firstNumber / secondNumber; } … // lots more of your great code

  21. Note The Difference… // lots of your great code … double ratio = 0.0; if ( secondNumber != 0 ) { ratio = (double) firstNumber / secondNumber; } … // lots more of your great code // lots of your great code … if ( secondNumber != 0 ) { double ratio = (double) firstNumber / secondNumber; } … // lots more of your great code the variable ratio will not be accessible outside the if statement!

  22. Conditions • Boolean expressions • Must evaluate to true or false • Comparison operators: • ==, <, >, <=, >=, != • Compound expressions: • Logical and: && • Logical or: || • Logical not: !

  23. Truth Tables P, Q can be any Boolean expression (perhaps compound)

  24. Compound Expressions • firstNumber == 0 && secondNumber == 0 • firstNumber < 0 && secondNumber < 0 • firstNumber > 0 && !(secondNumber > 0) • firstNumber == 0 || secondNumber == 0 • firstNumber < 0 && firstNumber > 0 • firstNumber < 0 || firstNumber > 0 • !( firstNumber == 0 || secondNumber == 0 )

  25. If / If-Else / If-Else-If • if ( condition ) { statement; } • if ( condition ) { statement; } else { statement; } • if ( condition ) { statement; } else if ( condition ) { statement; } else { statement; }

  26. Understanding Conditionals if ( secondNum != 0 ) { ratio = firstNum / secondNum; } if ( secondNum == 0 ) { System.out.println( “Divide by zero!” ); } if ( secondNum != 0 ) { ratio = firstNum / secondNum; } else { System.out.println( “Divide by zero!” ); }

  27. Understanding Conditionals String sign; if ( number == 0 ) { sign = “non-negative”; } else if ( number < 0 ) { sign = “negative”; } else if ( number > 0 ) { sign = “positive”; } String sign; if ( number == 0 ) { sign = “non-negative”; } if ( number < 0 ) { sign = “negative”; } if ( number > 0 ) { sign = “positive”; }

  28. Understanding Conditionals String sign; if ( number == 0 ) { sign = “non-negative”; } else if ( number < 0 ) { sign = “negative”; } else { sign = “positive”; } String sign; if ( number == 0 ) { sign = “non-negative”; } if ( number < 0 ) { sign = “negative”; } else { sign = “positive”; }

  29. Understanding Conditionals String sign; if ( number == 0 ) { sign = “non-negative”; } else if ( number < 0 ) { sign = “negative”; } else { sign = “positive”; } String sign; if ( number == 0 ) { sign = “non-negative”; } if ( number < 0 ) { sign = “negative”; } else { sign = “positive”; }

  30. Understanding Conditionals String sign; if ( number == 0 ) { sign = “non-negative”; } else if ( number < 0 ) { sign = “negative”; } else { sign = “positive”; } String sign; if ( number == 0 ) { sign = “non-negative”; } if ( number < 0 ) { sign = “negative”; } if ( number > 0 ) { sign = “positive”; }

  31. Compound Statements • if ( condition ) { statement; statement; statement; … } • if ( condition ) { statement; statement; statement; … } else { statement; statement; statement; … } To Java, this is a single statement

  32. Compound Statements • if ( condition ) { statement; statement; statement; … } • if ( condition ) { statement; statement; statement; … } else { statement; statement; statement; … } Statements inside can be anything… Even another if statement!

  33. Nested Conditionals • if ( condition_1 ) { if ( condition_2 ) { statement_1; } else { statement_2; } }

  34. Nested Conditionals • if ( condition_1 && condition_2 ) { statement_1; } if ( !condition_2 ) { statement_2; } • if ( condition_1 ) { if ( condition_2 ) { statement_1; } else { statement_2; } }

  35. Nested Conditionals • if ( condition_1 && condition_2 ) { statement_1; } if ( !condition_2 ) { statement_2; } • if ( condition_1 ) { if ( condition_2 ) { statement_1; } else { statement_2; } }

  36. Nested Conditionals • if ( condition_1 && condition_2 ) { statement_1; } if ( condition_1 && !condition_2 ) { statement_2; } • if ( condition_1 ) { if ( condition_2 ) { statement_1; } else { statement_2; } }

  37. Changing The Flow ... System.out.println( “Enter another integer:” ); intsecondNumber = keyboard.nextInt(); if ( secondNumber != 0 ) { double ratio = (double) firstNumber / secondNumber; System.out.println( “The ratio is “ + ratio ); } else { System.out.println( “Divide by zero error!” ); } ... 21 firstNumber 7 secondNumber

  38. Changing The Flow ... System.out.println( “Enter another integer:” ); intsecondNumber = keyboard.nextInt(); if ( secondNumber != 0 ) { double ratio = (double) firstNumber / secondNumber; System.out.println( “The ratio is “ + ratio ); } else { System.out.println( “Divide by zero error!” ); } ... 21 firstNumber 7 secondNumber

  39. Changing The Flow ... System.out.println( “Enter another integer:” ); intsecondNumber = keyboard.nextInt(); if ( secondNumber != 0 ) { double ratio = (double) firstNumber / secondNumber; System.out.println( “The ratio is “ + ratio ); } else { System.out.println( “Divide by zero error!” ); } ... 21 firstNumber 7 secondNumber 3 ratio

  40. Changing The Flow ... System.out.println( “Enter another integer:” ); intsecondNumber = keyboard.nextInt(); if ( secondNumber != 0 ) { double ratio = (double) firstNumber / secondNumber; System.out.println( “The ratio is “ + ratio ); } else { System.out.println( “Divide by zero error!” ); } ... 21 firstNumber 7 secondNumber 3 ratio

  41. Changing The Flow ... System.out.println( “Enter another integer:” ); intsecondNumber = keyboard.nextInt(); if ( secondNumber != 0 ) { double ratio = (double) firstNumber / secondNumber; System.out.println( “The ratio is “ + ratio ); } else { System.out.println( “Divide by zero error!” ); } ... 21 firstNumber 0 secondNumber

  42. Changing The Flow ... System.out.println( “Enter another integer:” ); intsecondNumber = keyboard.nextInt(); if ( secondNumber != 0 ) { double ratio = (double) firstNumber / secondNumber; System.out.println( “The ratio is “ + ratio ); } else { System.out.println( “Divide by zero error!” ); } ... 21 firstNumber 0 secondNumber

  43. Changing The Flow ... System.out.println( “Enter another integer:” ); intsecondNumber = keyboard.nextInt(); if ( secondNumber != 0 ) { double ratio = (double) firstNumber / secondNumber; System.out.println( “The ratio is “ + ratio ); } else { System.out.println( “Divide by zero error!” ); } ... 21 firstNumber 0 secondNumber

  44. Let’s Hunt The Wumpus

  45. Our Maze PIT

  46. Our Rules • Player can move one step per turn • up, down, left, or right • cannot move off the maze (grid) • Find the gold  win • Find the wumpus  lose • Find the pit  lose a turn • Player gets only four turns, otherwise draw PIT

  47. Location In The Maze 0 2 1 0 PIT 1 2

  48. Handle Player Movement First… 0 2 1 0 1 2

More Related