Download lec04

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CMSC 150
CONDITIONAL
EXECUTION
CS 150: Mon 16 Jan 2012
Sequential Execution
6
import java.util.Scanner;
Java executes
public class ProgramFive
statements
{
public static void main(String[] args)
step-by-step
{
Scanner keyboard = new Scanner( System.in in
); exactly the order
you have them
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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?
15
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?
16
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?
17
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?
18
import java.util.Scanner;
Will not compile!
public class ProgramFive
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner( System.in );
Trying to use a variable
before it is declared
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 );
}
}
Find the Runtime Error
19
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:” );
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 );
}
}
Find the Runtime Error
20
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:” );
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…
Conditional Execution
21
// lots of your great code
Java will execute the
…
statement only if the
if ( condition )
condition is true
{
statement;
}
…
// lots more of your great code
Conditional Execution
22
// lots of your great code
a Boolean
condition
…
double ratio = 0.0;
if ( secondNumber != 0 )
{
ratio = (double) firstNumber / secondNumber;
}
…
// lots more of your great code
Conditional Execution
23
// lots of your great code
…
double ratio = 0.0;
if ( secondNumber != 0 )
{
ratio = (double) firstNumber / secondNumber;
}
…
will compute the ratio
// lots more of your great code Java
only if the condition is true
(i.e., non-zero second #)
Note The Difference…
24
// lots of your great code
// lots of your great code
…
…
double ratio = 0.0;
if ( secondNumber != 0 )
if ( secondNumber != 0 )
{
{
ratio =
double ratio =
(double) firstNumber /
(double) firstNumber /
secondNumber;
secondNumber;
}
}
…
…
// lots more of your great code
// lots more of your great code
Note The Difference…
25
// lots of your great code
// lots of your great code
…
…
double ratio = 0.0;
if ( secondNumber != 0 )
if ( secondNumber != 0 )
{
{
ratio =
double ratio =
(double) firstNumber /
(double) firstNumber /
secondNumber;
secondNumber;
}
}
…
…
// lots more of your great code
// lots more of your great code
the variable ratio
will not be accessible
outside the if statement!
Conditions
26

Boolean expressions
Must evaluate to true or false

Comparison operators:



==, <, >, <=, >=, !=
Compound expressions:
Logical and:
 Logical or:
 Logical not:

&&
||
!
Truth Tables
27
P
Q
P && Q
P
Q
P || Q
T
T
T
T
T
T
T
F
F
T
F
T
F
T
F
F
T
T
F
F
F
F
F
F
P
!P
T
F
F
T
P, Q can be any Boolean expression (perhaps compound)
Compound Expressions
28

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
29

if ( condition )
{
statement;
}

if ( condition )
{
statement;
}
else
{
statement;
}

if ( condition )
{
statement;
}
else if ( condition )
{
statement;
}
else
{
statement;
}
Understanding Conditionals
30
if ( secondNum != 0 )
if ( secondNum != 0 )
{
{
ratio = firstNum / secondNum;
}
ratio = firstNum / secondNum;
}
else
{
if ( secondNum == 0 )
System.out.println(
{
“Divide by zero!” );
System.out.println(
}
“Divide by zero!” );
}
Understanding Conditionals
31
String sign;
String sign;
if ( number == 0 )
if ( number == 0 )
{
{
sign = “non-negative”;
sign = “non-negative”;
}
}
else if ( number < 0 )
if ( number < 0 )
{
{
sign = “negative”;
sign = “negative”;
}
}
else if ( number > 0 )
if ( number > 0 )
{
{
sign = “positive”;
}
sign = “positive”;
}
Understanding Conditionals
32
String sign;
String sign;
if ( number == 0 )
if ( number == 0 )
{
{
sign = “non-negative”;
sign = “non-negative”;
}
}
else if ( number < 0 )
if ( number < 0 )
{
{
sign = “negative”;
sign = “negative”;
}
}
else
else
{
{
sign = “positive”;
}
sign = “positive”;
}
Understanding Conditionals
33
String sign;
String sign;
if ( number == 0 )
if ( number == 0 )
{
{
sign = “non-negative”;
sign = “non-negative”;
}
}
else if ( number < 0 )
if ( number < 0 )
{
{
sign = “negative”;
sign = “negative”;
}
}
else
else
{
{
sign = “positive”;
}
sign = “positive”;
}
Understanding Conditionals
34
String sign;
String sign;
if ( number == 0 )
if ( number == 0 )
{
{
sign = “non-negative”;
sign = “non-negative”;
}
}
else if ( number < 0 )
if ( number < 0 )
{
{
sign = “negative”;
sign = “negative”;
}
}
else
if ( number > 0 )
{
{
sign = “positive”;
}
sign = “positive”;
}
Compound Statements
35

if ( condition )
{
statement;
statement;
statement;
…
}
To Java, this is
a single statement

if ( condition )
{
statement;
statement;
statement;
…
}
else
{
statement;
statement;
statement;
…
}
Compound Statements
36

if ( condition )
{
statement;
statement;
statement;
…
}
else
{
statement;
statement;
statement;
Statements inside can be anything…
…
Even another if statement!
}
if ( condition )
{
statement;
statement;
statement;
…
}

Nested Conditionals
37

if ( condition_1 )
{
if ( condition_2 )
{
statement_1;
}
else
{
statement_2;
}
}
Nested Conditionals
38

if ( condition_1 )

if ( condition_1 && condition_2 )
{
{
statement_1;
if ( condition_2 )
}
{
statement_1;
}
if ( !condition_2 )
else
{
statement_2;
{
statement_2;
}
}
}
Nested Conditionals
39

if ( condition_1 )

if ( condition_1 && condition_2 )
{
{
statement_1;
if ( condition_2 )
}
{
statement_1;
}
if ( !condition_2 )
else
{
statement_2;
{
statement_2;
}
}
}
Nested Conditionals
40

if ( condition_1 )

if ( condition_1 && condition_2 )
{
{
statement_1;
if ( condition_2 )
}
{
statement_1;
}
if ( condition_1 && !condition_2 )
else
{
statement_2;
{
statement_2;
}
}
}
Changing The Flow
41
...
firstNumber
21
secondNumber
7
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!” );
}
...
Changing The Flow
42
...
firstNumber
21
secondNumber
7
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!” );
}
...
Changing The Flow
43
...
firstNumber
21
secondNumber
7
ratio
3
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!” );
}
...
Changing The Flow
44
...
firstNumber
21
secondNumber
7
ratio
3
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!” );
}
...
Changing The Flow
45
...
firstNumber
21
secondNumber
0
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!” );
}
...
Changing The Flow
46
...
firstNumber
21
secondNumber
0
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!” );
}
...
Changing The Flow
47
...
firstNumber
21
secondNumber
0
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!” );
}
...
Let’s Hunt The Wumpus
48
Our Maze
49
PIT
Our Rules
PIT
50

Player can move one step per turn


up, down, left, or right
cannot move off the maze (grid)
 win
 lose
 lose a turn

Find the gold
Find the wumpus
Find the pit

Player gets only four turns, otherwise draw


Location In The Maze
51
0
1
2
0
1
2
PIT
Handle Player Movement First…
52
0
0
1
2
1
2
Related documents