The most important decision statement The Java IF Statement The most important decision statement Copyright © 1998-2010 Curt Hill
Flow of Control in Java Recall the flow statements in Java: Decisions If Switch Case Loops for while do Break Here we consider the if Copyright © 1998-2010 Curt Hill
Why? There are too many times when we need a conditional statement One set of statements is executed in one set of circumstances and another otherwise We also want our programs to be robust Unlikely to abnormally terminate regardless of what the user does Copyright © 1998-2010 Curt Hill
Example Suppose the following statement: avg = total/count; If count is zero the program will abnormally terminate with a divide by zero exception The solution is nesting this in an if: if(count > 0) avg = total/count; Copyright © 1998-2010 Curt Hill
Example Revisited The basic form is: if (cond) statement; The if is a reserved word The parentheses are required They show the limit of the condition Only a single statement is allowed A compound statement may be used Copyright © 1998-2010 Curt Hill
A Condition Any expression of type boolean Usually a comparison May include boolean methods May include boolean operators Unlike C/C++ must be a real boolean Copyright © 1998-2010 Curt Hill
Two forms of if statement if (boolean) statement if (boolean) statement else statement Copyright © 1998-2010 Curt Hill
The Else There is no THEN like in Pascal or BASIC The else is optional The statement to do if true immediately follows the parenthesized condition The else is optional Else is a reserved word and signals the start of the statement to execute if the condition is false The if without an else means else do nothing Copyright © 1998-2010 Curt Hill
Compound statements Either the then statement or the else statement may be replaced with a compound statement In any combination Even the compound statement of an if may create local variables Their scope is confined to the compound statement Copyright © 1998-2010 Curt Hill
Example Program Modify a picture by increasing contrast Make the brighter pixels brighter still Dim the pixels already less than average brightness This requires an if because pixel processing is not uniform Copyright © 1998-2010 Curt Hill
Basic Program is Same public static void main(String [] a){ String fileName; FileChooser.setMediaPath (“…/mediasources"); fileName=FileChooser.pickAFile(); Picture p1=new Picture(fileName); p1.explore(); process(p1); } Copyright © 1998-2010 Curt Hill
Process Method Beginning static void process(Picture p){ for(int y=0;y<p.getHeight();y++) for(int x=0;x<p.getWidth();x++){ Pixel px = p.getPixel(x,y); int red = px.getRed(); int green = px.getGreen(); int blue = px.getBlue(); // Now what? Copyright © 1998-2010 Curt Hill
Process Method End if(red + green + blue > 128*3){ // brighten red = red*3/2; green = green*3/2; blue = blue*3/2; } else { // Dim red = red*2/3; green = green*2/3; blue = blue*2/3; px.setRed(red); px.setGreen(green); px.setBlue(blue); } // end of fors Copyright © 1998-2010 Curt Hill
Barbara Copyright © 1998-2010 Curt Hill