Presentation is loading. Please wait.

Presentation is loading. Please wait.

The most important decision statement

Similar presentations


Presentation on theme: "The most important decision statement"— Presentation transcript:

1 The most important decision statement
The Java IF Statement The most important decision statement Copyright © Curt Hill

2 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 © Curt Hill

3 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 © Curt Hill

4 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 © Curt Hill

5 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 © Curt Hill

6 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 © Curt Hill

7 Two forms of if statement
if (boolean) statement if (boolean) statement else statement Copyright © Curt Hill

8 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 © Curt Hill

9 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 © Curt Hill

10 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 © Curt Hill

11 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 © Curt Hill

12 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 © Curt Hill

13 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 © Curt Hill

14 Barbara Copyright © Curt Hill


Download ppt "The most important decision statement"

Similar presentations


Ads by Google