Download presentation
Presentation is loading. Please wait.
Published byBrice Williamson Modified over 9 years ago
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 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.
3
First Example if(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.
4
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.
5
Second Example if(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.
6
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.
7
Using braces In both of the examples above, the single statements can be replaced by a block of code enclosed in braces. if(condition) { statement1; statement2; … } else { … }
8
Conditions The 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.
9
Note for comparing float values In 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.
10
Examples of if statements It would be possible to write any number of fragments of code illustrating the models shown above. Here is the code for the Cup3 class It contains a method, increaseSeedCount(), to increase the number of seeds in a cup.
11
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; }
12
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:
13
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.