48
CMSC 150 CONDITIONAL EXECUTION CS 150: Mon 16 Jan 2012

CMSC 150 conditional execution

  • Upload
    wyanet

  • View
    48

  • Download
    0

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

Page 1: CMSC 150 conditional execution

CMSC 150CONDITIONALEXECUTION

CS 150: Mon 16 Jan 2012

Page 2: CMSC 150 conditional execution

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

Page 3: CMSC 150 conditional execution

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

Page 4: CMSC 150 conditional execution

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

Page 5: CMSC 150 conditional execution

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

Page 6: CMSC 150 conditional execution

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

Page 7: CMSC 150 conditional execution

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

Page 8: CMSC 150 conditional execution

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

Page 9: CMSC 150 conditional execution

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

Page 10: CMSC 150 conditional execution

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

Page 11: CMSC 150 conditional execution

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

Page 12: CMSC 150 conditional execution

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

Page 13: CMSC 150 conditional execution

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

Page 14: CMSC 150 conditional execution

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

Page 15: CMSC 150 conditional execution

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

Page 16: CMSC 150 conditional execution

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

Page 17: CMSC 150 conditional execution

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

Page 18: CMSC 150 conditional execution

// 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

Page 19: CMSC 150 conditional execution

// 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

Page 20: CMSC 150 conditional execution

// 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

Page 21: CMSC 150 conditional execution

// 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

Page 22: CMSC 150 conditional execution

Conditions Boolean expressions Must evaluate to true or false

Comparison operators: ==, <, >, <=, >=, !=

Compound expressions: Logical and: && Logical or: || Logical not: !

26

Page 23: CMSC 150 conditional execution

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

Page 24: CMSC 150 conditional execution

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

Page 25: CMSC 150 conditional execution

If / If-Else / If-Else-If if ( condition )

{ statement; }

if ( condition ){ statement; }else{ statement;}

if ( condition ){ statement; }else if ( condition ){ statement; }else{ statement;}

29

Page 26: CMSC 150 conditional execution

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

Page 27: CMSC 150 conditional execution

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

Page 28: CMSC 150 conditional execution

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

Page 29: CMSC 150 conditional execution

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

Page 30: CMSC 150 conditional execution

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

Page 31: CMSC 150 conditional execution

Compound Statements if ( condition )

{ statement; statement; statement; … }

if ( condition ){ statement; statement; statement; … }else{ statement; statement; statement; …}

To Java, this isa single

statement

35

Page 32: CMSC 150 conditional execution

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

Page 33: CMSC 150 conditional execution

Nested Conditionals if ( condition_1 )

{ if ( condition_2 ) { statement_1; } else { statement_2; }}

37

Page 34: CMSC 150 conditional execution

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

Page 35: CMSC 150 conditional execution

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

Page 36: CMSC 150 conditional execution

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

Page 37: CMSC 150 conditional execution

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

Page 38: CMSC 150 conditional execution

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

Page 39: CMSC 150 conditional execution

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

Page 40: CMSC 150 conditional execution

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

Page 41: CMSC 150 conditional execution

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

Page 42: CMSC 150 conditional execution

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

Page 43: CMSC 150 conditional execution

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

Page 44: CMSC 150 conditional execution

Let’s Hunt The Wumpus48

Page 45: CMSC 150 conditional execution

Our Maze

PIT

49

Page 46: CMSC 150 conditional execution

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

Page 47: CMSC 150 conditional execution

Location In The Maze

PIT

0 1 2

0

1

2

51

Page 48: CMSC 150 conditional execution

Handle Player Movement First…

0 1 2

0

1

2

52