Georgia Institute of Technology

Slides:



Advertisements
Similar presentations
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Advertisements

CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Decision Structures and Boolean Logic
Flow of Control Part 1: Selection
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 5 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
Conditionals-part41 Conditionals – part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Control statements Mostafa Abdallah
Georgia Institute of Technology Conditionals – part 2 Barb Ericson Georgia Institute of Technology August 2005.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Conditionals-Mod8-part21 Conditionals – part 2 Edge Detection Barb Ericson Georgia Institute of Technology May 2007.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Propositional Logic.
Chapter 2 Section 2 Absolute Value
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
Section 7.1 Logical Operators
Control Statements: Part 2
Chapter 6 More Conditionals and Loops
Boolean Expressions and If
Topics The if Statement The if-else Statement Comparing Strings
Intro to Logic Logical statements evaluate to one of two things:
Barb Ericson Georgia Institute of Technology August 2005
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
Topics The if Statement The if-else Statement Comparing Strings
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 1
Outline Altering flow of control Boolean expressions
Manipulating Pictures, Arrays, and Loops part 5
Two-Dimensional Arrays and Nested Loops – part 5
Two-Dimensional Arrays and Nested Loops – part 1
Workshop for Programming And Systems Management Teachers
Truth tables: Ways to organize results of Boolean expressions.
Truth tables: Ways to organize results of Boolean expressions.
Two-Dimensional Arrays and Nested Loops – part 2
Manipulating Pictures, Arrays, and Loops
Georgia Institute of Technology
Barb Ericson Georgia Institute of Technology May 2006
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Manipulating Pictures, Arrays, and Loops
Introduction to Programming
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
CSC 1051 – Data Structures and Algorithms I
Question 1a) What is printed by the following Java program? int s;
Chap 7. Advanced Control Statements in Java
Controlling Program Flow
Presentation transcript:

Georgia Institute of Technology Conditionals – part 2 Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level How to use conditionals with two possibilities How to do simple edge detection How to use ‘and’, ‘or’, ‘exclusive or’ and ‘not’ in a conditional What is De Morgan’s Law? Georgia Institute of Technology

Georgia Institute of Technology Truth Table Conditional Operand 1 Operand 2 Result And true false Or Exclusive Or An and conditional is only true when both operands are true. An or conditional is true as long as either operand is true. An exclusive or conditional is true only if one, but not both, operands are true. Georgia Institute of Technology

Georgia Institute of Technology Conditional Exercise When are the following true? When are they false? You can go out if your room is clean and you did your homework You can go out if your room is clean or you did your homework You can go out if either your room is clean or you did your homework but not if both of these is true (exclusive OR) Georgia Institute of Technology

Conditional Operators We can check if several things are true - And Using && (evaluation stops if the first item is false) Using & (to always evaluate both operands) We can check if at least one of several things are true - Or Using || (evaluation stops if the first item is true) Using | (to always evaluate both operands) We can check if only one and only one of the things is true – Exclusive Or Using ^ The conditional operators && and || are ‘short circuit’ operators. They stop evaluation early when the result is obvious from evaluating the first operand. See http://java.sun.com/docs/books/tutorial/java/nutsandbolts/relational.html for Sun’s tutorial on conditional operators. Georgia Institute of Technology

Georgia Institute of Technology Conditional Exercise What is the result from the following code? int x = 3; int y = 4; if (x < 4 && y > 5) System.out.println(“and is true”); else System.out.println(“and is false”); Georgia Institute of Technology

Georgia Institute of Technology Conditional Exercise What is the result from the following code? int x = 3; int y = 6; if (x < 4 && y > 5) System.out.println(“and is true”); else System.out.println(“and is false”); Georgia Institute of Technology

Georgia Institute of Technology Conditional Exercise What is the result from the following code? int x = 4; int y = 4; if (x < 4 || y > 5) System.out.println(“or is true”); else System.out.println(“or is false”); Georgia Institute of Technology

Georgia Institute of Technology Conditional Exercise What is the result from the following code? int x = 4; int y = 4; if (x <= 4 || y > 5) System.out.println(“or is true”); else System.out.println(“or is false”); Georgia Institute of Technology

Using && (And) and || (Or) Check that a value is in a range Is some value between 0 and 255 (inclusive) for valid pixel color values 0 <= x <= 255 is written as 0 <= x && x <= 255 // in Java or x >= 0 && x <= 255 // is the same Check if at least one of several things is true Is this black or white? True if either it is black or it is white Georgia Institute of Technology

Not Conditional Operator Use ! To change the value to the opposite !true = false !false = true A not conditional operator applied to a complex conditional changes it !(op1 && op2) = !op1 || !op2 !(op1 || op2) = !op1 && !op2 This is known as De Morgan’s Law See http://en.wikipedia.org/wiki/DeMorgan%27s_Law for information on DeMorgan’s Law Georgia Institute of Technology

De Morgan’s Law Exercise What is equivalent to the following? !(x > 4 && x < 8) !(y > 2 || y < 10) !(x == 2 && y == 4) !(y != 2 && x != 3) !(x == 3 || x == 5) !(y == 2 || y < 5) Georgia Institute of Technology

Georgia Institute of Technology

Exercise: Edge Detection Loop through all the pixels in the picture Calculate the average color for the current pixel and the pixel at the same x but y+1. Get the distance between the two averages If the absolute value of the distance is greater than some value turn the current pixel black Otherwise turn the current pixel white The image is from foal.jpg in mediasources after doing edge detection with a limit of 10. Georgia Institute of Technology

Edge Detection Algorithm To find areas of high contrast Try to loop from row = 0 to row = height – 1 Loop from x = 0 to x = width Get the pixel at the x and y (top pixel) Get the pixel at the x and (y + 1) bottom pixel Get the average of the top pixel color values Get the average of the bottom pixel color values If the absolute value of the difference between the averages is over a passed limit Turn the pixel black Otherwise turn the pixel white Georgia Institute of Technology

Use if and else for two possibilities Sometimes you want to do one thing if the expression is true and a different thing if it is false int x = 200; if (x < 128) System.out.println(“<128”); else System.out.println(“>=128”); false if (expression) else true Statement or block Statement or block statement Georgia Institute of Technology

Georgia Institute of Technology Exercise Have students walk through a flowchart of a loop with a conditional. dice < 6 false Say your name Set count to 0 true Clap your hands count times Jump count times count < 3 false true count = count + 1 Throw the dice Say, "all done! And the value of count" Georgia Institute of Technology

Edge Detection Exercise Write a method edgeDetection that takes an input limit And turns all pixels black where the absolute value of the difference between that pixel and the below pixel is greater than the passed limit And turns all pixels white where the absolute value of the difference between that pixel and the below pixel is less than or equal the passed limit Pixel has a getAverage() method that returns the average of the three colors at the pixel Georgia Institute of Technology

Testing Edge Detection String file = FileChooser.getMediaPath(“butterfly1.jpg”); Picture p = new Picture(file); p.explore(); p.edgeDetection(10); Georgia Institute of Technology

Georgia Institute of Technology Challenge Create another method for simple edge detection This time compare the current pixel with the one to the right (x+1) How do you need to change the nested loop? Do you get a different result? Georgia Institute of Technology

Georgia Institute of Technology Summary Use if and else if you have two possibilities to deal with if (test) { // statements to execute when the test is true } else // statements to execute when the test is false Complex conditionals Use ‘and’ to test for more than one thing being true Use ‘or’ to test if at least one thing is true Use ‘exclusive or’ to test that one and only one thing is true Use ‘not’ to change the result from true to false and false to true Georgia Institute of Technology