Decision Statements

advertisement
Decision Structures: The If Statement,
Else-If Statement, and Relational
Operators
CS0007: Introduction to Computer Programming
Review
 Scope refers to…
 where in a program an entity can be accessed by name.
 Three kinds of comments in Java:
 Single-Line – begin with // and the entire line is ignored
 Multi-Line – begin with /* and end with */, and everything
between is ignored
 Documentation Comments – special comments that can be used to
make attractively formatted HTML files that document the source
code.
 Programming Style refers to…
 the way a programmer uses spaces, indentation, blank lines, and
punctuation characters.
 The standard input device is normally the…
 keyboard
Review
 The Java object that refers to the standard input device is…
 System.in
 The class that the Java API provides to allow us to take in
input as primitive types or strings is named…
 Scanner
Decision Structures
 So far our programs have been very sequential:
int a = 2, b = 3, c, d;
c = b + a;
d = b – a;
 All of these statements will be executed, in order, from top
to bottom
 This is what is called a sequence structure, because the
statements are executed in order, without branching.
 However, it is often the case that we require a program to
execute some statements only under certain circumstances.
 We can do this with decision structures.
Decision Structures
 In a decision structure’s simplest form certain statements are executed
only when a specific condition exists. If the condition does not exist, the
statements are not executed.
 It is said that the statements inside of the decision structure are conditionally
executed.
 We will go over many decision structures, some of which are more complex
than this.
Is it cold
outside?
No
Yes
Condition
Wear a coat.
False
True
Statement 1
Boolean
Expression
False
True
Statement 1
Wear a coat.
Statement 2
Statement 2
Wear a coat.
Statement 3
Statement 3
The if Statement
 The most basic decision structure in Java is the if statement.
if(BooleanExpression)
statement;
if(BooleanExpression) {
statement1;
statement2;
...
}
 BooleanExpression – a boolean expression.
 A boolean expression is one that is either true or false.
 If the boolean expression is true, the statement that follows will be executed
 In the multiple statement case if the boolean expression is true, all the statements in the block will be
executed.
 A block is a collection of statements that are organized to be together physically.
o In this case (and in most) the statements in the brackets are a block.
 Otherwise the statement is skipped
If Statement Example 1
 New Topics:
 If Statements
 Dead Code
Relational Operators
 Often we want to compare values in order to make a decision.
 We can do this in Java with relational operators
 Relational Operators determine whether a specific relationship exist between two
values.
 All relational operators resolve to either true or false.
Relational Operators (in
Order of Precedence)
Meaning
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
==
Equal to
!=
Not equal to
Relational Operators
 Let’s look at an example:
length < width
 If length is less than width the whole expression resolves to…
 true
 If length is greater than width the whole expression resolves to…
 false
 If length is equal to width the whole expression resolves to…
 false
 Another example:
length >= width
 If length is less than width the whole expression resolves to…
 false
 If length is greater than width the whole expression resolves to…
 true
 If length is equal to width the whole expression resolves to…
 true
Relational Operators Example 1
 New Topics:
 Relational Operators
Programming Style and the if
Statement
 Even if an if there is only one conditionally executed
statement, it is still acceptable to put brackets around it.
if(BooleanExpression){
statement;
}
 However, there are two rules you should always follow:
 The first conditionally executed statement should be on the
next line after the if statement.
 The conditionally executed statement should be indented one
level from the if statement.
 Note: There is NO semicolon after an if statement.
Flags
 A Flag is a boolean variable that signals when some condition
exists in a program.
 When a flag is set to true, it means some condition exists
 When a flag is set to false, it means some condition does not
exist.
if(score > 95)
highscore = true;
 Here, highscore is a flag indicating that the score is above 95.
 Right now, we don’t have any situations where these are terribly
useful, but for now, just know we can and will use them.
Comparing Characters
 You can also use relational operators on character data as well:
if(ch == 'A')
System.out.println("The character is A");
 Equal to and not equal to are the most natural for this data type,
but you can use any relational operators.
'A' < 'B'
 Resolves to true
'a' < 'B'
 Resolves to false
 Why?
 Remember, all characters are represented as Unicode numbers.
 What Java does when comparing characters is compare the Unicode values
Character Comparison Example
 New Topic:
 Character Comparison
The if-else Statement
 There is an expansion of the if statement called the if-else
statement.
if(BooleanExpression)
statement or block 1
else
statement or block 2
 Just like the if statement, BooleanExpression is evaluated.
 If it resolves to true, then statement or block 1 is
executed
 If it resolves to false, then statement or block 2 is
executed
if-else Flowchart
False
Boolean
Expression
True
Statement 4
Statement 1
Statement 5
Statement 2
Statement 6
Statement 3
else-if Example
 New Topic:
 else-if statement
Group Programming: Fraction to
Decimal
Download