{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

week04 - Week 4 Conditional Execution CS 177 1 Conditional...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 177 Week 4: Conditional Execution 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Conditional execution So far we have only considered Java programs that do one thing after another, in sequence Our programs have not had the ability to choose between different possibilities Now, they will! 2
Background image of page 2
The if-statement The if -statement: “x is small” will only print if x is less than 5 In this case, we know that it is, but x could come from user input or a file or int x = 4; if( x < 5 ) System.out.println(“x is small!”); 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The if part Any boolean expression Any single executable statement Anatomy of an if if( condition ) statement; 4
Background image of page 4
Conditions in the if Any expression that evaluates to a boolean is legal in an if -statement Examples: x == y mass > 21.75 Character.isDigit(gender) s.equals(“help”) && (z < 4) 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Comparison The most common condition you will find is a comparison between two things In Java , that comparison can be: == equals != does not equal < less than <= less than or equal to > greater than >= greater than or equal to 6
Background image of page 6
Equals You can use the == operator to compare any two things of the same type Different numerical types can be compared as well ( 3 == 3.0 ) Be careful with double types, 0.33333333 is not equal to 0.33333332 int x = 3; if ( x == 4 ) System.out.println(“This doesn’t print”); 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Not Equals Any place you could have used the == operator, you can use the != operator If == gives true, the != operator will always give false, and vice versa If you want to negate a condition, you can always use the ! as a not is the same as if ( x != 4 ) if ( !(x == 4) ) 8
Background image of page 8
= != == Remember, a single equal sign ( = ) is the assignment operator (think of a left- pointing arrow) A double equals ( == ) is a comparison operator if ( y = 6 ) //compiler error! if ( y == 6 ) //how to test if y is 6! 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Less Than (or Equal To) Inequality is very important in programming You may want to take an action as long as a value is below a certain threshold For example, you might want to keep bidding at an auction until the price is greater than what you can afford if ( x <= 4 ) if ( x < 4 ) // what is the difference? 10
Background image of page 10
Greater Than (or Equal To) Just like less than or equal to, except the opposite Note that the opposite of <= is > and the opposite of >= is < Thus, !( x <= y ) is equivalent to ( x > y ) !( x >= y ) is equivalent to ( x < y ) 11
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Either/Or Sometimes you have to make a decision If a condition is true, you go one way, if not, you go the other For example: If I pass CS177, Then I throw a party to celebrate Otherwise, I take it again and go to class next time 12
Background image of page 12
Exclusivity Notice the nature of this kind of condition Both outcomes cannot happen Either a party gets thrown or you take CS 177 again For these situations, we use the else construct 13
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Anatomy of an if-else Two different outcomes if( condition ) statement1; else statement2; 14
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}