Upload
wyanet
View
48
Download
0
Tags:
Embed Size (px)
DESCRIPTION
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:“ ); - PowerPoint PPT Presentation
Citation preview
CMSC 150CONDITIONALEXECUTION
CS 150: Mon 16 Jan 2012
Sequential Executionimport 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 statementsstep-by-stepin exactly the
orderyou have them
6
Sequential Executionimport 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 Executionimport 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 Executionimport 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 Executionimport 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 Executionimport 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
Sequential Executionimport 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 );
}}
12
Sequential Executionimport 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 );
}}
13
Sequential Executionimport 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 );
}}
14
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 );
}}
15
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 );
}}
16
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 );
}}
17
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
18
Find the Runtime Errorimport java.util.Scanner;
public class ExampleProgram {
public static void main(String[] args){
Scanner keyboard = new Scanner( System.in );
System.out.println( “Enter an integer:” ); int firstNumber = keyboard.nextInt();
System.out.println( “Enter another integer:” ); int secondNumber = keyboard.nextInt();
double ratio = (double) firstNumber / secondNumber;
System.out.println( “The ratio is “ + ratio );}
}
19
Find the Runtime Errorimport java.util.Scanner;
public class ExampleProgram {
public static void main(String[] args){
Scanner keyboard = new Scanner( System.in );
System.out.println( “Enter an integer:” ); int firstNumber = keyboard.nextInt();
System.out.println( “Enter another integer:” ); int secondNumber = keyboard.nextInt();
double ratio = (double) firstNumber / secondNumber;
System.out.println( “The ratio is “ + ratio );}
}
potentially…
20
Java will execute the statement only if the condition is
true
Conditional Execution
// lots of your great code…if ( condition ){ statement;} …// lots more of your great code
21
// lots of your great code…double ratio = 0.0;if ( secondNumber != 0 ){ ratio = (double) firstNumber / secondNumber;} …// lots more of your great code
Conditional Execution
a Boolean condition
22
// lots of your great code…double ratio = 0.0;if ( secondNumber != 0 ){ ratio = (double) firstNumber / secondNumber;} …// lots more of your great code
Conditional Execution
Java will compute the ratio
only if the condition is true
(i.e., non-zero second #)
23
// lots of your great code…double ratio = 0.0;if ( secondNumber != 0 ){ ratio = (double) firstNumber / secondNumber;} …// lots more of your great code
Note The Difference…// lots of your great code…
if ( secondNumber != 0 ){ double ratio = (double) firstNumber / secondNumber;} …// lots more of your great code
24
// lots of your great code…double ratio = 0.0;if ( secondNumber != 0 ){ ratio = (double) firstNumber / secondNumber;} …// lots more of your great code
Note The Difference…// 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!
25
Conditions Boolean expressions Must evaluate to true or false
Comparison operators: ==, <, >, <=, >=, !=
Compound expressions: Logical and: && Logical or: || Logical not: !
26
Truth Tables
P Q P && QT T TT F FF T FF F F
P Q P || QT T TT F TF T TF F F
P !PT FF T
P, Q can be any Boolean expression (perhaps compound)
27
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 )
28
If / If-Else / If-Else-If if ( condition )
{ statement; }
if ( condition ){ statement; }else{ statement;}
if ( condition ){ statement; }else if ( condition ){ statement; }else{ statement;}
29
Understanding Conditionalsif ( secondNum != 0 )
{
ratio = firstNum / secondNum;
}
else
{
System.out.println(
“Divide by zero!” );
}
if ( secondNum != 0 )
{
ratio = firstNum / secondNum;
}
if ( secondNum == 0 )
{
System.out.println(
“Divide by zero!” );
}
30
Understanding ConditionalsString 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”;
}
31
Understanding ConditionalsString 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”;
}
32
Understanding ConditionalsString 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”;
}
33
Understanding ConditionalsString 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”;
}
34
Compound Statements if ( condition )
{ statement; statement; statement; … }
if ( condition ){ statement; statement; statement; … }else{ statement; statement; statement; …}
To Java, this isa single
statement
35
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!
36
Nested Conditionals if ( condition_1 )
{ if ( condition_2 ) { statement_1; } else { statement_2; }}
37
Nested Conditionals if ( condition_1 )
{ if ( condition_2 ) { statement_1; } else { statement_2; }}
if ( condition_1 && condition_2 ){ statement_1;}
if ( !condition_2 ){ statement_2;}
38
Nested Conditionals if ( condition_1 )
{ if ( condition_2 ) { statement_1; } else { statement_2; }}
if ( condition_1 && condition_2 ){ statement_1;}
if ( !condition_2 ){ statement_2;}
39
Nested Conditionals if ( condition_1 )
{ if ( condition_2 ) { statement_1; } else { statement_2; }}
if ( condition_1 && condition_2 ){ statement_1;}
if ( condition_1 && !condition_2 ){ statement_2;}
40
firstNumber
Changing The Flow...
System.out.println( “Enter another integer:” );int secondNumber = 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!” );}
...
secondNumber
21
7
41
Changing The Flow...
System.out.println( “Enter another integer:” );int secondNumber = 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!” );}
...
firstNumber
secondNumber
21
7
42
Changing The Flow...
System.out.println( “Enter another integer:” );int secondNumber = 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!” );}
...
firstNumber
secondNumber
21
7
ratio 3
43
Changing The Flow...
System.out.println( “Enter another integer:” );int secondNumber = 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!” );}
...
firstNumber
secondNumber
21
7
ratio 3
44
firstNumber
Changing The Flow...
System.out.println( “Enter another integer:” );int secondNumber = 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!” );}
...
secondNumber
21
0
45
Changing The Flow...
System.out.println( “Enter another integer:” );int secondNumber = 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!” );}
...
firstNumber
secondNumber
21
0
46
Changing The Flow...
System.out.println( “Enter another integer:” );int secondNumber = 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!” );}
...
firstNumber
secondNumber
21
0
47
Let’s Hunt The Wumpus48
Our Maze
PIT
49
Our RulesPIT
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
50
Location In The Maze
PIT
0 1 2
0
1
2
51
Handle Player Movement First…
0 1 2
0
1
2
52