Computer Science 1026 Topic 10.docx

3 Pages
Unlock Document

Computer Science
Computer Science 1026A/B
Brian Langen

Topic 10: If Statement If Statement  Syntax: o If (test) { True  Execute the statements inside the if statement False  Ignore the statements inside the if statement }  Test: A Boolean with condition(s) that equate to true or false  Body of the statement: o When the Boolean test result is true, the statements in the body are executed. o When it is false, the statements in the body is ignored and execution falls through to the next statement  Brackets: Brackets around the statements to be executed if the condition is true are needed when there is more than one statement. However, it is safer to always use brackets even for a single statement in the body, and to line up the brackets for readability. If-Else statement  Syntax: o If (test) { True  Execute the statements under the if statement } Else { False  Execute the statements under the else statement }  If the condition is true, statements under the if statement is executed.  Otherwise (in this case since there are no else if statements), execute the statements under the else statement. Increase Blue Selective Method  Algorithm: o Loop through the pixels o If the current pixel is white (intensity of 255, 255, 255), decrease the red and green intensity in that pixel public void makeWhiteMoreBlue() { for (int x = 0; x < this.getWidth(); x++) { for (int y = 0; y < this.getHeight(); y++) { Pixel pixelObj = this.getPixel(x,y); int red = pixelObj.getRed(); int green = pixelObj.getGreen(); int blue = pixelObj.getBlue(); if (red == 255 && green == 255 && blue == 255) { pixelObj.setRed(200); pixelObj.setGreen(200); } } } } Multiple If Statement  Sometimes, more than one condition needs to be tested.  Instead of having an inefficient chain of ‘if’ statements, use an ‘if’, ‘else-if’, ‘else’ statement Inefficient Way Efficient Way If ( <= x && x <= ) { } If ( <= x && x <= ) { } If ( < x && x <= ) { } Else if (x <= ) { } If ( < x) { } Else { } Dangling Else  When there are more ‘if’ statements t
More Less

Related notes for Computer Science 1026A/B

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.