Presentation is loading. Please wait.

Presentation is loading. Please wait.

OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS

Similar presentations


Presentation on theme: "OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS"— Presentation transcript:

1 OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS
CS 201 OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS Copyright: Illinois Institute of Technology- George Koutsogiannakis

2 PREVIOUS TOPICS Static Variables and their usage.
Using Command Line for user input. Using the JOptionPane for user input.

3 NEW TOPICS FORMING CONDITIONS FOR SELECTION if and else SELECTIONS
RELATIONAL OPERATORS BOOLEAN EXPRESSIONS- TRUTH TABLES BOOLEAN OPERATIONS DE MORGANS LAWS if and else SELECTIONS If else SELECTIONS

4 Flow of Control Sequential Method calls Selection Looping
Execute instructions in order Method calls Transfer control to method, execute instructions in method, then return with or without a value Selection Execute different instructions depending on data Looping Repeat a set of instructions for different data

5 Equality Operators Used to determine if the values of two expressions are equal or not equal Result is true or false Equality operators Type (number of operands) Meaning = = binary is equal to != is not equal to

6 Example If int variable age holds the value 32:
( age == 32 ) evaluates to true ( age != 32 ) evaluates to false Use the equality operators only with primitive types and object references, not to compare object data! More on this later…

7 Common Error Trap Do not confuse the equality operator (==) with the assignment operator (=).

8 Type (number of operands)
Relational Operators Used to compare the values of two expressions Result is true or false Relational Operators Type (number of operands) Meaning < binary is less than <= is less than or equal to > is greater than >= is greater than or equal to

9 Example If int variable age holds value 32:
( age < 32 ) evaluates to false ( age <= 32 ) evaluates to true ( age > 32 ) evaluates to ( age >= 32 ) evaluates to

10 Evaluation to true or false
Suppose that we want to check if the value stored in memory location identified as : int myint has a certain value stored in it like the value 32, then the code for it is: if(myint==32) System.out.println(myint); myint=32; The above code prints out the value held in myint only if it is 32 (in other words if what is inside the parenthesis was evaluated to true). Otherwise, if it is not evaluated to true, the statement System.out… is not executed and instead the next statement is executed which assigns the value 32 to myint.

11 Logical Operations Logical Operations allow us to evaluate something as being either true or false. Remember that the data type that can have either a true or a false value is: boolean. Therefore quite often we are faced with the problem of having to make choices depending if some value was evaluated true or false.

12 Logical Operations For example consider the statement:
I am going to leave my house at 7:00 a.m. If the bus is outside my door I will take the bus Otherwise I will walk to the subway station. The decision is based on a variable being evaluated as true or false. Suppose we have a variable called bus of type boolean: boolean bus; The variable (identifier) bus can be set to have the value true stored in it if the bus is outside my door or the value false if the bus is not outside my door. I will have to evaluate bus to see what value it has before I make my decision to walk to the train station.

13 Logical Operations A part of Algebra called Boolean Algebra provides operators and operations to evaluate more complex logical decisions: What If we were to say for instance that I will take the bus if the bus is outside my door (variable bus=true) and at the same time it is a rainy day otherwise I will walk to the subway station. Now, I faced with fact that I have to evaluate the simultaneous true outcome of two conditions: bus=true rain=true If either of the conditions, or both, come out to false then I walk to the train station. The logical procedure that represents this kind of a decision in Boolean Algebra is called: AND Operation

14 Logical Operators Logical Operator Type (number of operands) Meaning !
Unary NOT && Binary AND || OR Operands must be boolean expressions!

15 Logical Operators The NOT operator ( ! ) evaluates to the inverse of the value of its operand. If the operand is true, the result will be false; and if the operand is false, the result will be true. The AND operator ( && ) takes two boolean expressions as operands; if both operands are true, the result will be true, otherwise it will be false. The OR operator ( || ) takes two boolean expressions as operands. If both operands are false, the result will be false; otherwise it will be true.

16 Truth Table a b !a a && b a || b true false
THE WORDS FOR TRUE AND FALSE CAN BE SUBSTITUTED WITH BINARY NUMBERS AND THE ABOVE OPARATIONS (!, &&, ||) CAN BE APPLIED TO BINARY NUMBERS.

17 BOOLEAN OPERATIONS-NOT
! MEANS INVERSE. THUS IF A=100100 C=!A RESULTS IN C=011011

18 BOOLEAN OPERATIONS-AND
Suppose A AND B REPRESENT A BINARY NUMBERS A= WHERE THE LEADING NUMBER IS THE MOST SIGNIFICANT DIGIT B= WHERE THE LEADING NUMBER IS THE MOST SIGNIFICANT DIGIT THE BOOLEAN OPERATION AND RESULTS C= A&&B C A B 1

19 BOOLEAN OPERATIONS-OR
C=A||B C A B 1

20 BOOLEAN OPERATIONS- NAND
C=!(A&&B) C A B 1

21 BOOLEAN OPERATIONS- NOR
C=!(A||B) C A B 1

22 BOOLEAN EXPRESSIONS D=(A&&B)||C&&!(A||C)
THE FOLLOWING IS A BOOLEAN EXPRESSION: D=(A&&B)||C&&!(A||C) WHERE A, B, C A ARE LOGICAL VARIABLES (THEY CAN BE BINARY NUMBERS OR TRUE OR FALSE VALUES OF VARIABLES) EVALUATION IS DONE LEFT TO RIGHT (PARENTHESIS FIRST). ANOTHER RULE IS THAT AND PRECEDES OR TRUTH TABLE SCAN HELP WITH THE EVALUATION. FIRST EVALUATE E=A&&B THEN F=A||C THEN G=!F NOW THE EXPRESSION IS D=E||C&&G NEXT H=C&&G LAST D=E||H

23 DE MORGAN ‘ S THEOREM IF BOOLEAN EXPRESSIONS ARE COMPLICATED:
USE THE EQUATIONS SHOWN IN THE NEXT SLIDES TO SIMPLIFY COMPLEX BOOLEAN EXPRESSIONS THE EQUATIONS ARE BASED ON DE MORGAN’S THEOREMS

24 Example using De Morgan’ s Laws
Suppose we want to simplify the expression: D=!(A||B)&&!(B||C) !(A||B)=!A&&!B !(B||C)=!B&&!C D= !A&&!B&&!B&&!C but !B&&!B=!B D=!A&&!B&&!C= !(A||B||C)

25 Equivalence of Expressions
DeMorgan's Laws: NOT( A AND B ) = ( NOT A ) OR ( NOT B ) !(A&&B) IS THE SAME AS : !A||!B NOT( A OR B ) = ( NOT A ) AND ( NOT B ) !(A||B) IS THE SAME AS : !A&&!B Thus, to find an equivalent expression: change && to || change || to && negate each operand expression

26 Negation of Equality and Relational Operators
Expression !( Expression ) a == b a != b a < b a >= b a <= b a > b a < b

27 Example These expressions are equivalent:
!( age > 18 && age < 65 ) !( age > 18 ) || !( age < 65 ) ( age <= 18 || age >= 65 )

28 Distributive Property in Boolean Expressions
i.e. A||( C&&D) = ( A ||C ) && A||D

29 Simple if Statement if ( condition )
Used when the program should perform an operation for one set of data, but do nothing for all other data. Syntax: if ( condition ) { // true block // executed if condition is true } next line of code Note: Curly braces are optional if the true block contains only one statement Code within the brackets is executed only when the condition evaluates to true. If the condition evaluates to false the code within the brackets is skipped and the next lien of code is executed.

30 Simple if Flow of Control
If the condition evaluates to true, the true block is executed. If the condition evaluates to false, the true block is skipped. See Text Example 5.2 TestGrade.java

31 EXAMPLE import javax.swing.JOptionPane; public class Selections { public static void main(String[] args) int x=10; Scanner scan=new Scanner(System.in); System.out.println(“Please enter an integer less than 10”); int y=scan.nextInt(); if(y>=x) JOptionPane.showMessageDialog(null, “You entered an integer greater than 10”); System.exit(0); } int z=x+y; JOptionPane.showMessageDialog(null, “Your new number is”+z);

32 If and else if (condition) { //execute the code inside the brackets }
execute the code within these brackets Now continue with execution of the next line of code Note: Gives you a way to test two possibilities. One or the other must be executed (but not both) before the next line of code (after the selections) is executed.

33 if/else Flow of Control
If the condition is true, the true block is executed. If the condition is false, the false block is executed. See Example 5.3 PassingGrade.java

34 Else if usage Start with an if if(condition) { execute this code }
else if(another_condition) else if (third_condition) Continue with the next line of code Note: one of the conditions could be true and therefore its code will be executed.The line of code after the selections is executed next. Note: Possibly none of the conditions could evaluate to false and therefore no selection code is executed. The line of code after the selections is executed next.

35 else if and else usage if(condition) { execute this code }
else if(another_condition) else if (third_condition) else Next line of code What is the difference between this and the previous slide code?

36 if/else if Flow of Control
If the first condition is true, the first true block is executed. The rest of the if/else statement is skipped. If the nth condition is true, the nth true block is executed. The rest of the if/else statement is skipped. If no condition is true, the false block is executed (optional). See Example 5.4 LetterGrade.java


Download ppt "OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS"

Similar presentations


Ads by Google