click here for complete presentation if statementsThe idea of an ifA very natural if-then sort of relationshipIf the condition is true, then do something For example: If I win a million dollars, Then I will yodel like an insane Swiss monkey Any statement that evaluates to a boolean is legal Examples:
An if with multiple statements if( condition ) statement1; statement2; ... statementN; } What if you need to do several things conditionally? Use braces to treat a group of statements like a single statement if( x == 4 ) System.out.println("I dislike 4"); System.out.println("Let us change x."); x = 10; } ComparisonThe most common condition you will find is a comparison between two things In Java, that comparison can be: Equals
Not Equals
if( x != 4) Greater Than (or Equal To)Just like less than or equal to, except the oppositeNote that the opposite of <= is > and the opposite of >= is < Thus, !( x <= y ) is equivalent to ( x > y ) |
If StatementsCredit: A Slides are modified with permission from Barry Wittman at Elizabethtown College |
|