comp 110 introduction to programming mr. joshua stough september 19, 2007

35
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Post on 22-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

COMP 110Introduction to Programming

Mr. Joshua StoughSeptember 19, 2007

Page 2: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Basic Review

• Terms you need to know– variable, type, value, expression,

statement, declaration, assignment, initialization, package, class, method, parameter, return type• What is the return type of

Integer.parseInt(String)• double x = 3.14596*Math.pow(rad,2);

• You need to learn what every line of code you write does.

Page 3: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

ReviewFlow of Execution

Page 4: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

ReviewRelational Operators

• Less than <• Greater than >• Equal to ==

– not assignment ‘=‘

• Not equal to !=• Less than or equal to <=• Greater than or equal to >=

Page 5: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

ReviewBoolean Operators

• NOT! (unary)!(2+2==5)

• AND&& (binary)(2+2==5) && (1+1==2)

• OR || (binary)(2+2==5) || (1+1==2)

true

false

true

Page 6: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Selection

• if statements• if-else statements• Nested if statements• switch statements

• Textbook Reference: Chapter 4 (pgs. 178-234)

Page 7: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Conditional Statements

• Let us choose which statement will be executed next– also called selection statements

• Java's conditional statements:– the if statement– the if-else statement– the switch statement

Page 8: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

One-Way Selection

• Syntax: if (expression) statement

Page 9: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

The if Statement

if ( condition ) statement;

if is a Javareserved word

The condition must be a boolean expression.It must evaluate to either true or false.

If the condition is true, the statement is executed.If it is false, the statement is skipped.

Page 10: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

The if Statement

if (sum > MAX) delta = sum - MAX;System.out.println ("The sum is " + sum);

First, the condition is evaluated. The value of sumis either greater than the value of MAX, or it is not.

If the condition is true, the assignment statement is executed.If it is not, the assignment statement is skipped.

Either way, the call to println is executed next.

Page 11: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Block Statements

Syntax:{

statement1statement2

.

.

.statementn

}

We use curly braces to group a set of individual statements.

This way we can have multiple statements execute based on a decision.

If we don't use curly braces, only a single statement will be executed based on the result of a decision.

Page 12: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

The if Statement

if (sum > MAX)delta = sum - MAX;

System.out.println ("The sum is " + sum);

A

if (sum > MAX) {delta = sum - MAX;

}System.out.println ("The sum is " + sum);

B

if (sum > MAX) {delta = sum - MAX;System.out.println ("The sum is " + sum);

}

C

Page 13: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Curly Braces

int num = 87, max = 25;if (num >= max*2) {

System.out.println ("apple");}

System.out.println ("orange");System.out.println ("pear");

int num = 87, max = 25;if (num >= max*2)

System.out.println ("apple");System.out.println ("orange");

System.out.println ("pear");

Page 14: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

State.java Example

Page 15: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

if Statement Gotcha

if (score >= 90);grade = "A";

No matter the result of the condition,grade will be assigned "A". The semicolonafter the if statement is a semantic error.

Page 16: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

In-Class Exercises

What is printed by the following code fragment?

int num = 87, max = 25;if (num >= max*2)

System.out.println ("apple");System.out.println ("orange");

System.out.println ("pear");

Write a code fragment that will print the value of val if val is less than MAX.

appleorangepear

if (val < MAX) {System.out.println (val);

}

Page 17: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Two-Way Selection

Page 18: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

The if-else Statement

if ( condition ) statement1;else statement2;

• If the condition is true, statement1 is executed; if the condition is false, statement2 is executed

• One or the other will be executed, but not both

Page 19: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

The if-else Statement

if (height <= MAX) {adjustment = 0;

}else {

adjustment = MAX-height;}

Page 20: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

In-Class Exercise

• What is the value of adjustment given the following values of height and MAX?

if (height <= MAX) {adjustment = 0;

}else {

adjustment = MAX-height;}

• height = 60, MAX = 80• height = 100, MAX = 75• height = 45, MAX = 45

0

0-25

Page 21: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Nested if Statements

• The statement (or block) executed as a result of an if statement can be another if statement (nested if).

• An else clause is always matched to the closest unmatched if.

• Closing curly brace signals the end of an if statement.

Page 22: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Nested if StatementsThe general syntax of a nested if statement is:

if (condition1) {block1

}else if (condition2) {block2

}else {block3

}

Page 23: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Nested if Statementsif (hasFourLegs) {

if (playsFetch) {System.out.println ("DOG");

} else {

System.out.println ("CAT");}

}else if (hasWings) {

System.out.println ("BIRD");}else {

System.out.println ("FISH");}

has four legs

does not have four legs

plays fetch

doesn't play fetch

has wingsdoesn't have wings

Page 24: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Min.java Example

Page 25: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

The switch Statement

Page 26: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

The switch Statement

• Provides another means to decide which statement to execute next

• Evaluates an expression, then attempts to match the result to one of several possible cases

• Each case contains a value and a list of statements

• The flow of control transfers to statement associated with the first value that matches

Page 27: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

The switch Statement• The general syntax of a switch statement is:

switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ...}

switchandcaseare

reservedwords

If expressionmatches value2,control jumpsto here

Page 28: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

The switch Statement

• Expression evaluated must be integral type– integer or character, not boolean or floating

point

• Case values must be constant (literal), not variable

• Can be implemented with nested if statements, but is much clearer with switch statements

Page 29: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

The switch Statement

• default– no case value matches the expression– if no default exists and no case value

matches the expression, no statements in the switch statement are executed

• break– processing jumps to statement

following the switch statement– usually at the end of each case

Page 30: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

The switch Statement

System.out.print ("Enter prize code: ");int prize = Integer.parseInt(keyboard.readLine());switch (prize){

case 1:System.out.println (``A Brand New Car!’’);break;

case 2:System.out.println (``A Trip to Hawaii!’’);break;

default:System.out.println (``Sorry, Try Again’’);break;

}

Page 31: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

GradeReport.java Example

Page 32: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

QuestionsAssume movieRating is an int and movieName is a String

switch (movieRating) {case 1:

System.out.println ("Run away!");if (movieName.equals("Gigli")) {

System.out.println ("Quickly!");}

case 2:System.out.println ("Save your money");break;

case 3:System.out.println ("It's OK");break;

}Run away!Quickly!Save your money

[Nothing]

1. What is printed if movieRating is 1 and movieName is "Gigli"?

2. What is printed if movieRating is 5?

Page 33: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Questionsif ((x > 0) && (y < 0)) {

z = x+y;System.out.println (“z = ” + z);

}System.out.print (“The sum is “);if (x+y < 0) { System.out.println (“negative.”)}else { System.out.println (“positive.”);}

z=1The sum is positive.The sum is negative.The sum is positive.

What is printed with the following values of x and y:1. x = 5, y = -4

2. x = -9, y = 53. x = 5, y = 5

Page 34: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Suggested Practice Problems• Answers are in back of textbook

• Ch 4 : #1, 2, 5, 8, 11

Page 35: COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007

Next Time in COMP 110

• Loops

• Reading Assignment: Chapter 5