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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 177 Week 4: Conditional Execution 1 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 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 int x = 4; if( x < 5 ) System.out.println(“x is small!”); 3 The if part Any boolean expression Any single executable statement Anatomy of an if if( condition ) statement; 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 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 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 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 = != == 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 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 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 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 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 Anatomy of an if-else Two different outcomes if( condition ) statement1; else statement2; 14 else...
View Full Document

This note was uploaded on 02/23/2012 for the course CS 177 taught by Professor Staff during the Spring '08 term at Purdue University.

Page1 / 48

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

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online