Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 14: Control Flow: if, if-then May 26, 2000 Nick Vallidis.

Similar presentations


Presentation on theme: "COMP 14: Control Flow: if, if-then May 26, 2000 Nick Vallidis."— Presentation transcript:

1 COMP 14: Control Flow: if, if-then May 26, 2000 Nick Vallidis

2 Review zWe talked about input and output zWhat symbols do we use to force the order of operations? zWhat are the equality operators? zWhat are the relational operators? zWhat are the logical operators?

3 Let's try this again zThere are class methods and what I'll call object methods. zClass methods are called using the name of the class: zObject methods are called using an instantiated object: result = Math.abs(-17.2); String greeting = new String("Hello, "); greeting = greeting.concat("everyone");

4 What's the difference?  The difference is that class methods have static in front of them when they are defined

5 Today zControl Flow  if statement  if-else statement  nested if and if-else statements

6 Back to the cake zTo bake a cake we need: yingredients yrecipe zSimilarly for a program we need: ydata yalgorithm

7 Yep, time for the algorithm zWe've already talked some about representing algorithms: yassignment yexpressions ymethods zBut, we need to be able to control the flow of the program...

8 Control Flow zGenerally, Java statements are just executed in order, one after another zThis is pretty dull. We couldn't even make a calculator… zIt would be nice to be able to control which statements get executed (we need to make decisions!) p. 110

9 Good news! zTwo types of statements that modify flow: yconditional statements yrepetition statements (loops) zConditional Statements  if, if-else, switch zRepetition Statements  while, do, for p. 110

10 if, if-else zToday, we'll cover these two  We'll come back to switch at a later date zWe'll talk about loops next week

11 if  Here is the general form for an if statement: if (condition) statement; p. 111

12 if  Here is the general form for an if statement: if (condition) statement; if is a Java reserved word

13 if  Here is the general form for an if statement: if (condition) statement; if is a Java reserved word condition is a boolean expression It must evaluate to true or false

14 if  Here is the general form for an if statement: if (condition) statement; if is a Java reserved word condition is a boolean expression It must evaluate to true or false If the condition evaluates to true, then the statement is executed, otherwise it is skipped

15 if  Here is the general form for an if statement: if (condition) statement; This indent is VERY important. The compiler doesn't need it, but it helps people read the program

16 Example int age = 22; if (age < 18) System.out.println("Can't vote!"); System.out.println("Always printed");

17 if Control Flow Diagram condition evaluated false statement true if (condition) statement;

18 Another example int year = 2000; if (year == 2000) System.out.println("Sydney Olympics!"); System.out.println("Always printed");

19 Another example int age = 12; if ((age = 65)) System.out.println("Discount!"); System.out.println("Always printed");

20 if-else  Here is the general form for an if-else statement: if (condition) statement1; else statement2; p. 114

21 if-else  If the condition is true then statement1 is executed. If the condition is false then statement2 is executed. Never both! if (condition) statement1; else statement2;

22 false true if-else Diagram if (condition) statement1; else statement2; condition evaluated statement1 statement2

23 if-else example int age = 22; if (age < 18) System.out.println("Can't vote!"); else System.out.println("Can vote!"); System.out.println("Always printed");

24 Block Statements  What if we need to do use more than one statement in an if or if-else ?  You can use the braces ( { and } ) to make a block statement p. 115

25 Example int age = 22; boolean canVote = true; if (age < 18) { System.out.println("Can't vote!"); canVote = false; } System.out.println("Always printed");

26 General format { statement 1 ; statement 2 ;. statement n ; }

27 Recommendation zA block statement doesn't have to contain more than 1 statement zI recommend always using the block statement form with if and if-else: if (age < 18) { System.out.println("Can't vote!"); }

28 Nested if and if-else  The statements you execute could be another if or if-else !

29 Nested if and if-else if (age > 12) { if (age > 65) System.out.println("Discount"); else System.out.println("Normal price"); } else System.out.println("Child Discount");

30 Examples

31 Homework zRead 3.1-3.2, 3.4 again zAssignment P2 goes out today and is due on Thursday


Download ppt "COMP 14: Control Flow: if, if-then May 26, 2000 Nick Vallidis."

Similar presentations


Ads by Google