480 likes | 590 Views
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:“ );
E N D
CMSC 150conditionalexecution 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:“ ); 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
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 ); } }
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 ); } }
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 ); } }
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 ); } }
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 ); } }
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 ); } }
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 ); } }
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 ); } }
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 ); } }
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 ); } }
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 ); } }
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
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 ); } }
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…
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
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
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 #)
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
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!
Conditions • Boolean expressions • Must evaluate to true or false • Comparison operators: • ==, <, >, <=, >=, != • Compound expressions: • Logical and: && • Logical or: || • Logical not: !
Truth Tables P, Q can be any Boolean expression (perhaps compound)
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 )
If / If-Else / If-Else-If • if ( condition ) { statement; } • if ( condition ) { statement; } else { statement; } • if ( condition ) { statement; } else if ( condition ) { statement; } else { statement; }
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!” ); }
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”; }
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”; }
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”; }
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”; }
Compound Statements • if ( condition ) { statement; statement; statement; … } • if ( condition ) { statement; statement; statement; … } else { statement; statement; statement; … } To Java, this is a single statement
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!
Nested Conditionals • if ( condition_1 ) { if ( condition_2 ) { statement_1; } else { statement_2; } }
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; } }
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; } }
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; } }
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
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
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
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
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
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
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
Our Maze PIT
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
Location In The Maze 0 2 1 0 PIT 1 2
Handle Player Movement First… 0 2 1 0 1 2