Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 14 Introduction to Programming

Similar presentations


Presentation on theme: "COMP 14 Introduction to Programming"— Presentation transcript:

1 COMP 14 Introduction to Programming
Michele Weigle - COMP 14 - Spring 2004 COMP 14 Introduction to Programming Mr. Joshua Stough February 9, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

2 Review Flow of Execution
Michele Weigle - COMP 14 - Spring 2004 Review Flow of Execution

3 Review Relational Operators
Michele Weigle - COMP 14 - Spring 2004 Review Relational Operators Less than < Greater than > Equal to == not assignment ‘=‘ Not equal to != Less than or equal to <= Greater than or equal to >=

4 Review Boolean Operators
Michele Weigle - COMP 14 - Spring 2004 Review Boolean 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

5 Michele Weigle - COMP 14 - Spring 2004
Selection if statements if-else statements Nested if statements switch statements Textbook Reference: Chapter 4 (pgs )

6 Conditional Statements
Michele Weigle - COMP 14 - Spring 2004 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 Conditional statement give us the power to make basic decisions about flow of control

7 Michele Weigle - COMP 14 - Spring 2004
One-Way Selection Syntax: if (expression) statement

8 Michele Weigle - COMP 14 - Spring 2004
The if Statement The condition must be a boolean expression. It must evaluate to either true or false. if is a Java reserved word if ( condition ) statement; If the condition is true, the statement is executed. If it is false, the statement is skipped.

9 Michele Weigle - COMP 14 - Spring 2004
The if Statement if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum); First, the condition is evaluated. The value of sum is 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.

10 Michele Weigle - COMP 14 - Spring 2004
Block Statements Syntax: { statement1 statement2 . 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.

11 Michele Weigle - COMP 14 - Spring 2004
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 Code A and Code B do the exact same things. The braces point out a block of code. A block is a group of one or more statements delimited by a set of curly braces. Anywhere we say a statement is executed, a block could have been executed The braces in Code B allow us to add more than one statement that should execute if the condition is true. In these examples, Code C will execute only the println statement if the condition is true. Notice the indentation of the code remember that the computer ignores whitespace like tabs Use braces even when not required if (sum > MAX) { delta = sum - MAX; System.out.println ("The sum is " + sum); } C

12 Michele Weigle - COMP 14 - Spring 2004
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"); These two code boxes produce the same output. Even though the indentation is incorrect, it's easier to tell what will be output when braces are used.

13 Michele Weigle - COMP 14 - Spring 2004
State.java Example

14 Michele Weigle - COMP 14 - Spring 2004
if Statement Gotcha if (score >= 90); grade = "A"; No matter the result of the condition, grade will be assigned "A". The semicolon after the if statement is a semantic error.

15 Michele Weigle - COMP 14 - Spring 2004
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"); apple orange pear Write a code fragment that will print the value of val if val is less than MAX. if (val < MAX) { System.out.println (val); }

16 Michele Weigle - COMP 14 - Spring 2004
Two-Way Selection

17 Michele Weigle - COMP 14 - Spring 2004
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 An else clause can be added to an if statement to make an if-else statement Remember that if we used curly braces, we could replace statement1 and statement 2 with block1 and block2 One or the other will be executed, but not both

18 Michele Weigle - COMP 14 - Spring 2004
The if-else Statement if (height <= MAX) { adjustment = 0; } else { adjustment = MAX-height;

19 Michele Weigle - COMP 14 - Spring 2004
Guessing.java Example Math.random() returns a random double between 0.0 and 1.0 Generate a random integer between 0-5 int num = (int) (Math.random() * 6); Generate a random integer between 1-5 int num = (int) (Math.random() * 5) + 1; 0 - 4 1-5

20 Michele Weigle - COMP 14 - Spring 2004
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 -25

21 Michele Weigle - COMP 14 - Spring 2004
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.

22 Michele Weigle - COMP 14 - Spring 2004
Nested if Statements The general syntax of a nested if statement is: if (condition1) { block1 } else if (condition2) { block2 else { block3

23 Michele Weigle - COMP 14 - Spring 2004
Nested if Statements if (hasFourLegs) { if (playsFetch) { System.out.println ("DOG"); } else { System.out.println ("CAT"); else if (hasWings) { System.out.println ("BIRD"); System.out.println ("FISH"); has four legs plays fetch doesn't play fetch Assume here that hasFourLegs, playsFetch, and hasWings are boolean variables Which if statements match to which else statements? "else" matches to closest unmatched "if" notice the indentation does not have four legs has wings doesn't have wings

24 Michele Weigle - COMP 14 - Spring 2004
Min.java Example

25 Michele Weigle - COMP 14 - Spring 2004
The switch Statement

26 Michele Weigle - COMP 14 - Spring 2004
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

27 Michele Weigle - COMP 14 - Spring 2004
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 ... } switch and case are reserved words If expression matches value2, control jumps to here Don't need braces in the statement list

28 Michele Weigle - COMP 14 - Spring 2004
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

29 Michele Weigle - COMP 14 - Spring 2004
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 Once inside a case, processing continues in order until a break statement is reached

30 Michele Weigle - COMP 14 - Spring 2004
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!”); default: System.out.println (“Sorry, Try Again”); }

31 GradeReport.java Example
Michele Weigle - COMP 14 - Spring 2004 GradeReport.java Example

32 Michele Weigle - COMP 14 - Spring 2004
Assume 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"); Questions What is printed if movieRating is 1 and movieName is "Gigli"? What is printed if movieRating is 5? Run away! Quickly! Save your money [Nothing]

33 Michele Weigle - COMP 14 - Spring 2004
if ((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.”); Questions What is printed with the following values of x and y: x = 5, y = -4 x = -9, y = 5 x = 5, y = 5 z=1 The sum is positive. The sum is negative. The sum is positive.

34 Suggested Practice Problems
Answers are in back of textbook Ch 4 (pg. 190): #1, 2, 5, 8, 11

35 Program 2 GUI Calculator
Michele Weigle - COMP 14 - Spring 2004 Program 2 GUI Calculator Due Wednesday, Feb 18 at 11:59pm Write a program that performs the operations of a calculator

36 Michele Weigle - COMP 14 - Spring 2004
Next Time in COMP 14 Loops Reading Assignment: Chapter 5 (pgs )


Download ppt "COMP 14 Introduction to Programming"

Similar presentations


Ads by Google