this section gives several examples of if statements. in this first example, if a certain condition...

13
6.1 The Syntax of if Statements and Arithmetic Conditions

Upload: brice-williamson

Post on 16-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following

6.1 The Syntax of if Statements and Arithmetic Conditions

Page 2: This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following

This section gives several examples of if statements.

In this first example, if a certain condition holds true, the single line of code immediately following it is executed.

If the condition does not hold true, then that line of code is not executed.

In either case, any lines of code in the program following that line of code are executed.

Page 3: This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following

First Exampleif(condition) statement;

Notice that the keyword if and the set of parentheses containing the condition do not form an independent line of code.

They should not be followed by a semicolon. If the condition is followed immediately by a

semicolon, this causes a logic error which is hard to see.

Page 4: This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following

First example, cont.The system tests the truth of the condition.If there is a semicolon immediately following

the parentheses, it doesn’t matter whether the result is true or false.

The semicolon signals the end of the if.The following line of code would not depend

on it and it would simply be executed.

Page 5: This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following

Second Exampleif(condition) statement1;else statement2;

The second example shows an if statement with 2 mutually exclusive alternatives.

In the course of execution, one or the other of the two statements has to be executed, but it is impossible for both to be executed.

Page 6: This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following

Second Example, cont.Like the line of code containing the if, the else does not form an independent line of code, and should not be followed by a semicolon.

If it is followed by a semicolon then any line of code following it would be executed unconditionally.

Page 7: This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following

Using bracesIn both of the examples above, the single statements

can be replaced by a block of code enclosed in braces.if(condition){ statement1; statement2; …}else{…}

Page 8: This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following

ConditionsThe conditions inside if statements are

frequently comparisons between numeric values. The comparison operators in Java are as follows

< less than<= less than or equal to> greater than>= greater than or equal to!= not equal to== equal to. Notice the difference

between this and assignment.

Page 9: This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following

Note for comparing float valuesIn a previous unit the point was made that

Java is particular in the way it handles floating point values.

It is important to keep this in mind when doing numeric comparisons, especially when mixing integer and floating point type variables.

If a variable contains an inexact binary representation of a decimal value, the result of the comparison may not be what you have in mind.

Page 10: This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following

Examples of if statementsIt would be possible to write any number of

fragments of code illustrating the models shown above.

Here is the code for the Cup3 classIt contains a method, increaseSeedCount(), to

increase the number of seeds in a cup.

Page 11: This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following

Examples of if statements, cont. public class Cup3 { private int seedCount;   public Cup3() { seedCount = 0; }   public Cup3(int initialCount) { seedCount = initialCount; }   public int getSeedCount() { return seedCount; }   public void setSeedCount(int newCount) { seedCount = newCount; }   public void increaseSeedCount(int addedNumber) { seedCount = seedCount + addedNumber; } }

Page 12: This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following

Examples of if statements, cont.Observe that in spite of the way things are

named using words like increase and added, it would be possible to send a negative parameter to the increaseSeedCount() method.

It is possible to use an if statement in the method code to prevent a negative value from being acted on.

A simple attempt to handle this might look like the following:

Page 13: This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following

Examples of if statements, cont.public void increaseSeedCount(int addedNumber){ if(addedNumber > 0) { seedCount = seedCount + addedNumber; }}This is not a complete solution. It is convenient that the if statement prevents an undesired

outcome. It is not convenient that a programmer could call the method

and not be warned that the code to increment was not executed.

Ways of addressing this problem will be taken up later.